Refactored movement to use strictly the move functions to retain depth order

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 8cb3198549
commit 3d383dfedd
  1. BIN
      C++ProjectTemplate
  2. 289
      main.cpp

Binary file not shown.

@ -27,6 +27,7 @@
CurrentCutscene->AdvanceAction(); \
#define 액션 (CutsceneAction*)new
std::vector<Object*> OBJECTS;
using namespace olc;
@ -54,13 +55,16 @@ class Animation{
}
};
vd2d cameraPos = {0,0};
PixelGameEngine*GAME;
class Object{
private:
vd2d scale={1,1};
vd2d pos;
public:
int id;
Animation*spr;
vd2d pos;
int frameIndex=0;
int frameCount=0;
int animationSpd=12; //How many frames to wait between each frame. Setting to 0 pauses the animation.
@ -73,6 +77,11 @@ class Object{
int objArrElement; //Which element in the object array this object is located in. For sorting purposes.
bool temp=false; //If set to true, it's marked for deletion after cutscene handling.
//animationSpd is how long to wait before switching frames.
bool Collision(vd2d pos) {
GAME->SetDrawTarget(layer::COLLISION);
Pixel collisionData = GAME->GetDrawTarget()->GetPixel((int)pos.x-cameraPos.x,(int)pos.y-cameraPos.y);
return collisionData!=MAGENTA;
}
Object(int id,std::string name,vd2d pos,Animation*spr,vd2d scale={1,1},Pixel color=WHITE,int animationSpd=1,bool temp=false) {
this->spr=spr;
this->pos=pos;
@ -90,6 +99,107 @@ class Object{
vd2d GetScale() {
return scale;
}
vd2d GetPos() {
return pos;
}
void Move(vd2d move) {
if (move.y==0) {
pos+=move;
return;
} else {
if (move.y<0) {
if (objArrElement>0&&OBJECTS[objArrElement-1]->pos.y+OBJECTS[objArrElement-1]->originPoint.y>pos.y+originPoint.y+move.y) {
OBJECTS[objArrElement]=OBJECTS[objArrElement-1];
OBJECTS[objArrElement-1]=this;
OBJECTS[objArrElement]->objArrElement=objArrElement;
objArrElement--;
}
} else {
if (objArrElement<OBJECTS.size()-1&&OBJECTS[objArrElement+1]->pos.y+OBJECTS[objArrElement+1]->originPoint.y<pos.y+originPoint.y+move.y) {
OBJECTS[objArrElement]=OBJECTS[objArrElement+1];
OBJECTS[objArrElement+1]=this;
OBJECTS[objArrElement]->objArrElement=objArrElement;
objArrElement++;
}
}
pos+=move;
}
}
void SetPos(vd2d pos) {
Move(pos-this->pos);
}
void SmoothMove(vd2d move) {
const int wiggleRoom=5;
vd2d originPos = {pos.x+originPoint.x,pos.y-1+originPoint.y};
if (!Collision(originPos+move)) {
Move(move);
return;
} else
if (move.x>0) {
for (int i=0;i<wiggleRoom;i++) { //Search Up.
if (!Collision({originPos.x+move.x,originPos.y-i})) {
//There is potentially to move up-right here, so we will do so.
Move({0,-1});
return;
}
}
for (int i=0;i<wiggleRoom;i++) { //Search Down.
if (!Collision({originPos.x+move.x,originPos.y+i})) {
//There is potentially to move down-right here, so we will do so.
Move({0,1});
return;
}
}
} else
if (move.x<0) {
for (int i=0;i<wiggleRoom;i++) { //Search Up.
if (!Collision({originPos.x+move.x,originPos.y-i})) {
//There is potentially to move up-left here, so we will do so.
Move({0,-1});
return;
}
}
for (int i=0;i<wiggleRoom;i++) { //Search Down.
if (!Collision({originPos.x+move.x,originPos.y+i})) {
//There is potentially to move down-left here, so we will do so.
Move({0,1});
return;
}
}
} else
if (move.y>0) {
for (int i=0;i<wiggleRoom;i++) { //Search Left.
if (!Collision({originPos.x-i,originPos.y+move.y})) {
//There is potentially to move down-left here, so we will do so.
Move({-1,0});
return;
}
}
for (int i=0;i<wiggleRoom;i++) { //Search Right.
if (!Collision({originPos.x+i,originPos.y+move.y})) {
//There is potentially to move down-right here, so we will do so.
Move({1,0});
return;
}
}
} else
if (move.y<0) {
for (int i=0;i<wiggleRoom;i++) { //Search Left.
if (!Collision({originPos.x-i,originPos.y+move.y})) {
//There is potentially to move up-left here, so we will do so.
Move({-1,0});
return;
}
}
for (int i=0;i<wiggleRoom;i++) { //Search Right.
if (!Collision({originPos.x+i,originPos.y+move.y})) {
//There is potentially to move up-right here, so we will do so.
Move({1,0});
return;
}
}
}
}
};
enum class Resistance{
@ -117,17 +227,13 @@ namespace Battle{
class Move{
public:
std::string name;
int composition[4];
std::array<int,4>composition;
int baseDmg; //The base damage of the attack.
int randomDmg; //Additional random roll damage to add onto the base damage.
bool pctDamage; //Uses % damage for the base damage instead of flat damage.
std::vector<std::pair<Property,int>> properties; //The int is used to determine the chance of something occurring.
Move(std::string name,int baseDmg,int randomDmg,int composition[4]={},bool pctDamage=false,std::vector<std::pair<Property,int>> properties={})
:name(name),randomDmg(randomDmg),baseDmg(baseDmg),pctDamage(pctDamage),properties(properties){
for (int i=0;i<4;i++) {
this->composition[i]=composition[i];
}
}
Move(std::string name,int baseDmg,int randomDmg,std::array<int,4>composition,bool pctDamage=false,std::vector<std::pair<Property,int>> properties={})
:name(name),randomDmg(randomDmg),baseDmg(baseDmg),composition(composition),pctDamage(pctDamage),properties(properties){}
};
}
@ -212,7 +318,6 @@ public:
int EDITING_LAYER=layer::DYNAMIC;
int SELECTED_OBJ_ID = PLAYER;
int OBJ_DISPLAY_OFFSET = 0;
vd2d cameraPos = {0,0};
bool GAME_FLAGS[128]={};
Object* PLAYER_OBJ;
bool messageBoxVisible=false;
@ -242,10 +347,10 @@ public:
std::map<std::string,Decal*> SPRITES;
std::map<std::string,Animation*> ANIMATIONS;
std::map<int,Object*> OBJ_INFO;
std::vector<Object*> OBJECTS;
bool OnUserCreate() override
{
GAME=this;
for (int i=1;i<6;i++) {
CreateLayer();
EnableLayer(i,true);
@ -386,7 +491,7 @@ goes on a very long time, I hope you can understand this is only for testing pur
case ActionType::CREATE_OBJECTS:{
for (auto&obj:((CreateObjects*)CurrentCutscene->GetAction())->GetObjects()) {
obj->temp=true;
AddObjectToWorld(CurrentCutscene->AddCutsceneObject(new Object(obj->id,obj->name,obj->pos,obj->spr,obj->GetScale(),obj->color,obj->animationSpd,true)));
AddObjectToWorld(CurrentCutscene->AddCutsceneObject(new Object(obj->id,obj->name,obj->GetPos(),obj->spr,obj->GetScale(),obj->color,obj->animationSpd,true)));
}
CurrentCutscene->AdvanceAction();
}break;
@ -498,16 +603,16 @@ goes on a very long time, I hope you can understand this is only for testing pur
}
if (PlayerCanMove()) {
if (GetKey(I).bHeld) {
SmoothMove(PLAYER_OBJ,{0,-1});
PLAYER_OBJ->SmoothMove({0,-1});
}
if (GetKey(K).bHeld) {
SmoothMove(PLAYER_OBJ,{0,1});
PLAYER_OBJ->SmoothMove({0,1});
}
if (GetKey(J).bHeld) {
SmoothMove(PLAYER_OBJ,{-1,0});
PLAYER_OBJ->SmoothMove({-1,0});
}
if (GetKey(L).bHeld) {
SmoothMove(PLAYER_OBJ,{1,0});
PLAYER_OBJ->SmoothMove({1,0});
}
}
int selectedTileX=(GetMouseX()+cameraPos.x)/32;
@ -693,7 +798,7 @@ goes on a very long time, I hope you can understand this is only for testing pur
} else
if (EDITING_LAYER==layer::OBJECT&&GetMouse(1).bReleased) {
for (int i=0;i<OBJECTS.size();i++) {
if (OBJECTS[i]->pos==HIGHLIGHTED_TILE*32) {
if (OBJECTS[i]->GetPos()==HIGHLIGHTED_TILE*32) {
delete OBJECTS[i];
OBJECTS.erase(OBJECTS.begin()+i--);
}
@ -723,10 +828,10 @@ goes on a very long time, I hope you can understand this is only for testing pur
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>(y+yTileOffset)*32&&obj->pos.y+obj->originPoint.y<=(y+yTileOffset+1)*32) {
if (!obj->drawn&&obj->GetPos().y+obj->originPoint.y>(y+yTileOffset)*32&&obj->GetPos().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->GetScale(),obj->color);
DrawPartialDecal(obj->GetPos()-cameraPos,obj->spr->spr,{(obj->frameIndex%obj->spr->frames)*obj->spr->width,0},{obj->spr->width,obj->spr->spr->sprite->height},obj->GetScale(),obj->color);
}
}
for (int x=-1;x<WIDTH/32+2;x++) {
@ -1018,7 +1123,7 @@ goes on a very long time, I hope you can understand this is only for testing pur
for (int i=0;i<OBJECTS.size();i++) {
f.put('\n');
const std::string obj="OBJECT"+std::to_string(OBJECTS[i]->pos.x)+";"+std::to_string(OBJECTS[i]->pos.y)+";"+std::to_string(OBJECTS[i]->id);
const std::string obj="OBJECT"+std::to_string(OBJECTS[i]->GetPos().x)+";"+std::to_string(OBJECTS[i]->GetPos().y)+";"+std::to_string(OBJECTS[i]->id);
for (int i=0;i<obj.length();i++) {
f.put(obj[i]);
}
@ -1223,109 +1328,6 @@ goes on a very long time, I hope you can understand this is only for testing pur
CreateObjectInfo(NPC19_8,"npc20_8",{0,0},"player.png",32,{2,2},BLUE,Flag::NONE,Flag::NONE,0);
}
bool Collision(vd2d pos) {
SetDrawTarget(layer::COLLISION);
Pixel collisionData = GetDrawTarget()->GetPixel((int)pos.x-cameraPos.x,(int)pos.y-cameraPos.y);
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 SmoothMove(Object*obj,vd2d move) {
const int wiggleRoom=5;
vd2d originPos = {obj->pos.x+obj->originPoint.x,obj->pos.y-1+obj->originPoint.y};
if (!Collision(originPos+move)) {
Move(PLAYER_OBJ,move);
return;
} else
if (move.x>0) {
for (int i=0;i<wiggleRoom;i++) { //Search Up.
if (!Collision({originPos.x+move.x,originPos.y-i})) {
//There is potentially to move up-right here, so we will do so.
Move(PLAYER_OBJ,{0,-1});
return;
}
}
for (int i=0;i<wiggleRoom;i++) { //Search Down.
if (!Collision({originPos.x+move.x,originPos.y+i})) {
//There is potentially to move down-right here, so we will do so.
Move(PLAYER_OBJ,{0,1});
return;
}
}
} else
if (move.x<0) {
for (int i=0;i<wiggleRoom;i++) { //Search Up.
if (!Collision({originPos.x+move.x,originPos.y-i})) {
//There is potentially to move up-left here, so we will do so.
Move(PLAYER_OBJ,{0,-1});
return;
}
}
for (int i=0;i<wiggleRoom;i++) { //Search Down.
if (!Collision({originPos.x+move.x,originPos.y+i})) {
//There is potentially to move down-left here, so we will do so.
Move(PLAYER_OBJ,{0,1});
return;
}
}
} else
if (move.y>0) {
for (int i=0;i<wiggleRoom;i++) { //Search Left.
if (!Collision({originPos.x-i,originPos.y+move.y})) {
//There is potentially to move down-left here, so we will do so.
Move(PLAYER_OBJ,{-1,0});
return;
}
}
for (int i=0;i<wiggleRoom;i++) { //Search Right.
if (!Collision({originPos.x+i,originPos.y+move.y})) {
//There is potentially to move down-right here, so we will do so.
Move(PLAYER_OBJ,{1,0});
return;
}
}
} else
if (move.y<0) {
for (int i=0;i<wiggleRoom;i++) { //Search Left.
if (!Collision({originPos.x-i,originPos.y+move.y})) {
//There is potentially to move up-left here, so we will do so.
Move(PLAYER_OBJ,{-1,0});
return;
}
}
for (int i=0;i<wiggleRoom;i++) { //Search Right.
if (!Collision({originPos.x+i,originPos.y+move.y})) {
//There is potentially to move up-right here, so we will do so.
Move(PLAYER_OBJ,{1,0});
return;
}
}
}
}
void AddObjectToWorld(Object*obj) {
std::vector<Object*>::const_iterator it = OBJECTS.begin();
if (obj->id==PLAYER&&!obj->temp) {
@ -1333,7 +1335,7 @@ goes on a very long time, I hope you can understand this is only for testing pur
}
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) {
if (!inserted&&OBJECTS[i]->GetPos().y+OBJECTS[i]->originPoint.y>obj->GetPos().y+obj->originPoint.y) {
OBJECTS.insert(it,obj);
obj->objArrElement=i;
inserted=true;
@ -1353,7 +1355,7 @@ goes on a very long time, I hope you can understand this is only for testing pur
//printf("OBJECTS (%d):\n",OBJECTS.size());
for (int i=0;i<OBJECTS.size();i++) {
if (i!=OBJECTS[i]->objArrElement) {
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);
printf("%d :: Object %s - %d (%lf,%lf)\n",i,OBJECTS[i]->name.c_str(),OBJECTS[i]->objArrElement,OBJECTS[i]->GetPos().x,OBJECTS[i]->GetPos().y);
}
assert(i==OBJECTS[i]->objArrElement);
}
@ -1480,16 +1482,16 @@ goes on a very long time, I hope you can understand this is only for testing pur
bool MoveObjectTowardsPoint(Object*obj,vd2d targetPos,PriorityDirection dir,double moveSpd,bool secondRun=false) {
bool reachedPosition=true;
if (dir==HORZ_FIRST||dir==BOTH) {
if (obj->pos.x!=targetPos.x) {
if (obj->pos.x<targetPos.x) {
obj->pos.x+=moveSpd;
if (obj->pos.x>targetPos.x) {
obj->pos.x=targetPos.x;
if (obj->GetPos().x!=targetPos.x) {
if (obj->GetPos().x<targetPos.x) {
obj->Move({moveSpd,0});
if (obj->GetPos().x>targetPos.x) {
obj->SetPos({targetPos.x,obj->GetPos().y});
}
} else {
obj->pos.x-=moveSpd;
if (obj->pos.x<targetPos.x) {
obj->pos.x=targetPos.x;
obj->Move({-moveSpd,0});
if (obj->GetPos().x<targetPos.x) {
obj->SetPos({targetPos.x,obj->GetPos().y});
}
}
reachedPosition=false;
@ -1499,16 +1501,16 @@ goes on a very long time, I hope you can understand this is only for testing pur
}
}
if (dir==VERT_FIRST||dir==BOTH) {
if (obj->pos.y!=targetPos.y) {
if (obj->pos.y<targetPos.y) {
obj->pos.y+=moveSpd;
if (obj->pos.y>targetPos.y) {
obj->pos.y=targetPos.y;
if (obj->GetPos().y!=targetPos.y) {
if (obj->GetPos().y<targetPos.y) {
obj->Move({0,moveSpd});
if (obj->GetPos().y>targetPos.y) {
obj->SetPos({obj->GetPos().x,targetPos.y});
}
} else {
obj->pos.y-=moveSpd;
if (obj->pos.y<targetPos.y) {
obj->pos.y=targetPos.y;
obj->Move({0,-moveSpd});
if (obj->GetPos().y<targetPos.y) {
obj->SetPos({obj->GetPos().x,targetPos.y});
}
}
reachedPosition=false;
@ -1536,7 +1538,12 @@ goes on a very long time, I hope you can understand this is only for testing pur
std::vector<Entity*>{new Entity(
new Object(
NPC1_4,"Test Obj",{pos.x+20,pos.y+48},ANIMATIONS["player.png"]
),70,70,14,std::array<int,4>{0,0,0,0},0,std::vector<Battle::Move*>{}
),70,70,14,std::array<int,4>{0,0,0,0},0,std::vector<Battle::Move*>{
new Battle::Move("Test Move 1",30,5,std::array<int,4>{0,0,0,0}),
new Battle::Move("Test Move 2",30,5,std::array<int,4>{0,0,0,0}),
new Battle::Move("Test Move 3",30,5,std::array<int,4>{0,0,0,0}),
new Battle::Move("Test Move 4",30,5,std::array<int,4>{0,0,0,0}),
}
)}
,chance)
);

Loading…
Cancel
Save