|
|
|
@ -8,6 +8,7 @@ |
|
|
|
|
#include "references.h" |
|
|
|
|
#include "states.h" |
|
|
|
|
#include "flags.h" |
|
|
|
|
#include <assert.h> |
|
|
|
|
|
|
|
|
|
#define WIDTH 256 |
|
|
|
|
#define HEIGHT 224 |
|
|
|
@ -76,16 +77,20 @@ class Object{ |
|
|
|
|
bool drawn=false; |
|
|
|
|
int disableFlag; |
|
|
|
|
int enableFlag; |
|
|
|
|
int objArrElement; //Which element in the object array this object is located in. For sorting purposes.
|
|
|
|
|
//animationSpd is how long to wait before switching frames.
|
|
|
|
|
Object(int id,std::string name,vd2d pos,Animation*spr,vd2d scale={1,1},Pixel color=WHITE,int animationSpd=1) { |
|
|
|
|
this->spr=spr; |
|
|
|
|
this->pos=pos; |
|
|
|
|
this->id=id; |
|
|
|
|
this->name=name; |
|
|
|
|
this->scale=scale; |
|
|
|
|
this->color=color; |
|
|
|
|
this->animationSpd=animationSpd; |
|
|
|
|
this->originPoint={spr->width/2,spr->spr->sprite->height-4}; |
|
|
|
|
SetScale(scale); |
|
|
|
|
} |
|
|
|
|
void SetScale(vd2d scale) { |
|
|
|
|
this->scale=scale; |
|
|
|
|
this->originPoint={spr->width/2*scale.x,(spr->spr->sprite->height-4)*scale.y}; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -114,6 +119,7 @@ public: |
|
|
|
|
int SELECTED_OBJ_ID = PLAYER; |
|
|
|
|
int OBJ_DISPLAY_OFFSET = 0; |
|
|
|
|
bool GAME_FLAGS[128]={}; |
|
|
|
|
Object* PLAYER_OBJ; |
|
|
|
|
|
|
|
|
|
bool MOUSE_PRESSED_DOWN=false,MOUSE_DOWN=false,MOUSE_RELEASED=false; //TODO Implement Mouse things.
|
|
|
|
|
|
|
|
|
@ -122,8 +128,8 @@ public: |
|
|
|
|
std::vector<std::vector<TILE*>> MAP3; |
|
|
|
|
std::vector<std::vector<TILE*>> MAP4; |
|
|
|
|
std::vector<std::vector<TILE*>> MAP5; //Collision Layer
|
|
|
|
|
std::map<Reference,Decal*> SPRITES; |
|
|
|
|
std::map<Reference,Animation*> ANIMATIONS; |
|
|
|
|
std::map<std::string,Decal*> SPRITES; |
|
|
|
|
std::map<std::string,Animation*> ANIMATIONS; |
|
|
|
|
std::map<int,Object*> OBJ_INFO; |
|
|
|
|
std::vector<Object*> OBJECTS; |
|
|
|
|
|
|
|
|
@ -146,7 +152,7 @@ public: |
|
|
|
|
GAME_FLAGS[Flag::TEST_FLAG2]=false; |
|
|
|
|
GAME_FLAGS[Flag::TEST_FLAG3]=true; |
|
|
|
|
|
|
|
|
|
MAP_ONETT=new Map("map0","map0_2","map0_3","map0_4","map0_5",SPRITES[TILESET1]); |
|
|
|
|
MAP_ONETT=new Map("map0","map0_2","map0_3","map0_4","map0_5",SPRITES["terrainmap.png"]); |
|
|
|
|
|
|
|
|
|
CURRENT_MAP=MAP_ONETT; |
|
|
|
|
//OBJ_INFO["PLAYER"]=PLAYER_ANIMATION;
|
|
|
|
@ -213,23 +219,23 @@ public: |
|
|
|
|
} |
|
|
|
|
if (OBJECTS.size()>0) { |
|
|
|
|
if (GetKey(I).bHeld) { |
|
|
|
|
if (!Collision({OBJECTS[0]->pos.x+OBJECTS[0]->originPoint.x,OBJECTS[0]->pos.y-1+OBJECTS[0]->originPoint.y})) { |
|
|
|
|
OBJECTS[0]->pos.y-=1; |
|
|
|
|
if (!Collision({PLAYER_OBJ->pos.x+PLAYER_OBJ->originPoint.x,PLAYER_OBJ->pos.y-1+PLAYER_OBJ->originPoint.y})) { |
|
|
|
|
Move(PLAYER_OBJ,{0,-1}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (GetKey(K).bHeld) { |
|
|
|
|
if (!Collision({OBJECTS[0]->pos.x+OBJECTS[0]->originPoint.x,OBJECTS[0]->pos.y+1+OBJECTS[0]->originPoint.y})) { |
|
|
|
|
OBJECTS[0]->pos.y+=1; |
|
|
|
|
if (!Collision({PLAYER_OBJ->pos.x+PLAYER_OBJ->originPoint.x,PLAYER_OBJ->pos.y+1+PLAYER_OBJ->originPoint.y})) { |
|
|
|
|
Move(PLAYER_OBJ,{0,1}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (GetKey(J).bHeld) { |
|
|
|
|
if (!Collision({OBJECTS[0]->pos.x-1+OBJECTS[0]->originPoint.x,OBJECTS[0]->pos.y+OBJECTS[0]->originPoint.y})) { |
|
|
|
|
OBJECTS[0]->pos.x-=1; |
|
|
|
|
if (!Collision({PLAYER_OBJ->pos.x-1+PLAYER_OBJ->originPoint.x,PLAYER_OBJ->pos.y+PLAYER_OBJ->originPoint.y})) { |
|
|
|
|
Move(PLAYER_OBJ,{-1,0}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (GetKey(L).bHeld) { |
|
|
|
|
if (!Collision({OBJECTS[0]->pos.x+1+OBJECTS[0]->originPoint.x,OBJECTS[0]->pos.y+OBJECTS[0]->originPoint.y})) { |
|
|
|
|
OBJECTS[0]->pos.x+=1; |
|
|
|
|
if (!Collision({PLAYER_OBJ->pos.x+1+PLAYER_OBJ->originPoint.x,PLAYER_OBJ->pos.y+PLAYER_OBJ->originPoint.y})) { |
|
|
|
|
Move(PLAYER_OBJ,{1,0}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -378,7 +384,7 @@ public: |
|
|
|
|
GAME_STATE=GameState::OBJ_SELECT; |
|
|
|
|
} |
|
|
|
|
if (EDITING_LAYER==layer::OBJECT&&GetMouse(0).bPressed) { |
|
|
|
|
OBJECTS.push_back(CreateObject(SELECTED_OBJ_ID,HIGHLIGHTED_TILE*32)); |
|
|
|
|
AddObjectToWorld(CreateObject(SELECTED_OBJ_ID,HIGHLIGHTED_TILE*32)); |
|
|
|
|
} else |
|
|
|
|
if (EDITING_LAYER==layer::OBJECT&&GetMouse(1).bReleased) { |
|
|
|
|
for (int i=0;i<OBJECTS.size();i++) { |
|
|
|
@ -412,7 +418,7 @@ public: |
|
|
|
|
for (int y=-1;y<HEIGHT/32+2;y++) { |
|
|
|
|
int yTileOffset = cameraPos.y/32; |
|
|
|
|
for (auto&obj:OBJECTS) { |
|
|
|
|
if (!obj->drawn&&obj->pos.y+obj->originPoint.y*obj->scale.y>(y+yTileOffset)*32&&obj->pos.y+obj->originPoint.y*obj->scale.y<=(y+yTileOffset+1)*32) { |
|
|
|
|
if (!obj->drawn&&obj->pos.y+obj->originPoint.y>(y+yTileOffset)*32&&obj->pos.y+obj->originPoint.y<=(y+yTileOffset+1)*32) { |
|
|
|
|
obj->drawn=true; |
|
|
|
|
SetDrawTarget(layer::DYNAMIC); |
|
|
|
|
DrawPartialDecal(obj->pos-cameraPos,obj->spr->spr,{(obj->frameIndex%obj->spr->frames)*obj->spr->width,0},{obj->spr->width,obj->spr->spr->sprite->height},obj->scale,obj->color); |
|
|
|
@ -423,23 +429,23 @@ public: |
|
|
|
|
if (x+xTileOffset>=0&&x+xTileOffset<MAP_WIDTH&&y+yTileOffset>=0&&y+yTileOffset<MAP_HEIGHT) { |
|
|
|
|
if(MAP5[y+yTileOffset][x+xTileOffset]->tileX*MAP5[y+yTileOffset][x+xTileOffset]->tileY!=225) { |
|
|
|
|
SetDrawTarget(layer::COLLISION); |
|
|
|
|
DrawPartialSprite({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES[TILESET1]->sprite,{MAP5[y+yTileOffset][x+xTileOffset]->tileX*32,MAP5[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
DrawPartialSprite({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"]->sprite,{MAP5[y+yTileOffset][x+xTileOffset]->tileX*32,MAP5[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
} |
|
|
|
|
if(MAP4[y+yTileOffset][x+xTileOffset]->tileX*MAP4[y+yTileOffset][x+xTileOffset]->tileY!=225) { |
|
|
|
|
SetDrawTarget(layer::BACKGROUND); |
|
|
|
|
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES[TILESET1],{MAP4[y+yTileOffset][x+xTileOffset]->tileX*32,MAP4[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP4[y+yTileOffset][x+xTileOffset]->tileX*32,MAP4[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
} |
|
|
|
|
if(MAP3[y+yTileOffset][x+xTileOffset]->tileX*MAP3[y+yTileOffset][x+xTileOffset]->tileY!=225) { |
|
|
|
|
SetDrawTarget(layer::GROUND); |
|
|
|
|
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES[TILESET1],{MAP3[y+yTileOffset][x+xTileOffset]->tileX*32,MAP3[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP3[y+yTileOffset][x+xTileOffset]->tileX*32,MAP3[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
} |
|
|
|
|
if(MAP2[y+yTileOffset][x+xTileOffset]->tileX*MAP2[y+yTileOffset][x+xTileOffset]->tileY!=225) { |
|
|
|
|
SetDrawTarget(layer::DYNAMIC); |
|
|
|
|
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES[TILESET1],{MAP2[y+yTileOffset][x+xTileOffset]->tileX*32,MAP2[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP2[y+yTileOffset][x+xTileOffset]->tileX*32,MAP2[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
} |
|
|
|
|
if(MAP[y+yTileOffset][x+xTileOffset]->tileX*MAP[y+yTileOffset][x+xTileOffset]->tileY!=225) { |
|
|
|
|
SetDrawTarget(layer::HIGH); |
|
|
|
|
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES[TILESET1],{MAP[y+yTileOffset][x+xTileOffset]->tileX*32,MAP[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP[y+yTileOffset][x+xTileOffset]->tileX*32,MAP[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -454,7 +460,7 @@ public: |
|
|
|
|
}break; |
|
|
|
|
case GameState::TILE_SELECT:{ |
|
|
|
|
//14x14 pixels per tile.
|
|
|
|
|
DrawDecal({0,0},SPRITES[TILESET1],{TILEMAP_EDITOR_DRAW_MULT,TILEMAP_EDITOR_DRAW_MULT}); |
|
|
|
|
DrawDecal({0,0},SPRITES["terrainmap.png"],{TILEMAP_EDITOR_DRAW_MULT,TILEMAP_EDITOR_DRAW_MULT}); |
|
|
|
|
DrawRectDecal(SELECTED_TILE*(TILEMAP_EDITOR_TILESIZE),{TILEMAP_EDITOR_TILESIZE,TILEMAP_EDITOR_TILESIZE},RED); |
|
|
|
|
}break; |
|
|
|
|
case GameState::OBJ_SELECT:{ |
|
|
|
@ -570,7 +576,7 @@ public: |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (enabled) { |
|
|
|
|
OBJECTS.push_back(CreateObject(id,{x,y})); |
|
|
|
|
AddObjectToWorld(CreateObject(id,{x,y})); |
|
|
|
|
} |
|
|
|
|
printf("Object %s Loaded.\n",OBJ_INFO[id]->name.c_str()); |
|
|
|
|
} else { |
|
|
|
@ -679,7 +685,8 @@ public: |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Decal*CreateSprite(std::string spriteName) { |
|
|
|
|
return new Decal(new Sprite("assets/"+spriteName)); |
|
|
|
|
SPRITES[spriteName] = new Decal(new Sprite("assets/"+spriteName)); |
|
|
|
|
return SPRITES[spriteName]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Object*CreateObject(int id,vd2d pos) { |
|
|
|
@ -687,8 +694,10 @@ public: |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Object*CreateObjectInfo(Reference ref,std::string name,vd2d pos,std::string spriteFileName,int sprWidth,vd2d scale={1,1},Pixel tint=WHITE,int enableFlag=Flag::NONE,int disableFlag=Flag::NONE,int animationDelay=1) { |
|
|
|
|
ANIMATIONS[ref] = new Animation(SPRITES[ref]=CreateSprite("player.png"),32); |
|
|
|
|
Object*newObj = new Object(ref,name,pos,ANIMATIONS[ref],scale,tint,animationDelay); |
|
|
|
|
if (!ANIMATIONS.count(spriteFileName)) { |
|
|
|
|
ANIMATIONS[spriteFileName] = new Animation(SPRITES[spriteFileName]=CreateSprite(spriteFileName),32); |
|
|
|
|
} |
|
|
|
|
Object*newObj = new Object(ref,name,pos,ANIMATIONS[spriteFileName],scale,tint,animationDelay); |
|
|
|
|
newObj->disableFlag=disableFlag; |
|
|
|
|
newObj->enableFlag=enableFlag; |
|
|
|
|
OBJ_INFO[ref]=newObj; |
|
|
|
@ -697,7 +706,7 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SetupAnimations() { |
|
|
|
|
SPRITES[TILESET1] = CreateSprite("terrainmap.png"); |
|
|
|
|
CreateSprite("terrainmap.png"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void SetupObjectInfo() { |
|
|
|
@ -870,6 +879,62 @@ public: |
|
|
|
|
return collisionData!=MAGENTA; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Move(Object*obj,vd2d move) { |
|
|
|
|
if (move.y==0) { |
|
|
|
|
obj->pos+=move; |
|
|
|
|
return; |
|
|
|
|
} else { |
|
|
|
|
if (move.y<0) { |
|
|
|
|
if (obj->objArrElement>0&&OBJECTS[obj->objArrElement-1]->pos.y+OBJECTS[obj->objArrElement-1]->originPoint.y>obj->pos.y+obj->originPoint.y+move.y) { |
|
|
|
|
OBJECTS[obj->objArrElement]=OBJECTS[obj->objArrElement-1]; |
|
|
|
|
OBJECTS[obj->objArrElement-1]=obj; |
|
|
|
|
OBJECTS[obj->objArrElement]->objArrElement=obj->objArrElement; |
|
|
|
|
obj->objArrElement--; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (obj->objArrElement<OBJECTS.size()-1&&OBJECTS[obj->objArrElement+1]->pos.y+OBJECTS[obj->objArrElement+1]->originPoint.y<obj->pos.y+obj->originPoint.y+move.y) { |
|
|
|
|
OBJECTS[obj->objArrElement]=OBJECTS[obj->objArrElement+1]; |
|
|
|
|
OBJECTS[obj->objArrElement+1]=obj; |
|
|
|
|
OBJECTS[obj->objArrElement]->objArrElement=obj->objArrElement; |
|
|
|
|
obj->objArrElement++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
obj->pos+=move; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AddObjectToWorld(Object*obj) { |
|
|
|
|
std::vector<Object*>::const_iterator it = OBJECTS.begin(); |
|
|
|
|
if (obj->id==PLAYER) { |
|
|
|
|
PLAYER_OBJ=obj; |
|
|
|
|
} |
|
|
|
|
bool inserted=false; |
|
|
|
|
for (int i=0;i<OBJECTS.size();i++) { |
|
|
|
|
if (!inserted&&OBJECTS[i]->pos.y+OBJECTS[i]->originPoint.y>obj->pos.y+obj->originPoint.y) { |
|
|
|
|
OBJECTS.insert(it,obj); |
|
|
|
|
obj->objArrElement=i; |
|
|
|
|
inserted=true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
it++; |
|
|
|
|
} |
|
|
|
|
if (!inserted) { |
|
|
|
|
obj->objArrElement=OBJECTS.size(); |
|
|
|
|
OBJECTS.push_back(obj); |
|
|
|
|
} else { |
|
|
|
|
for (int i=0;i<OBJECTS.size();i++) { |
|
|
|
|
OBJECTS[i]->objArrElement=i; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
printf("OBJECTS (%d):\n",OBJECTS.size()); |
|
|
|
|
for (int i=0;i<OBJECTS.size();i++) { |
|
|
|
|
printf("%d :: Object %s - %d (%lf,%lf)\n",i,OBJECTS[i]->name.c_str(),OBJECTS[i]->objArrElement,OBJECTS[i]->pos.x,OBJECTS[i]->pos.y); |
|
|
|
|
assert(i==OBJECTS[i]->objArrElement); |
|
|
|
|
} |
|
|
|
|
printf("\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool TabHeld(){ |
|
|
|
|
return GetKey(TAB).bHeld; |
|
|
|
|