generated from sigonasr2/CPlusPlusProjectTemplate
Animation system setup
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
db0e06c579
commit
8b3114be03
@ -36,6 +36,18 @@ class Object{
|
|||||||
std::string name;
|
std::string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Animation{
|
||||||
|
public:
|
||||||
|
olc::Decal*spr;
|
||||||
|
std::vector<olc::vi2d> frames;
|
||||||
|
int frame=0;
|
||||||
|
bool flipped=false;
|
||||||
|
|
||||||
|
olc::vi2d getCurrentFrame() {
|
||||||
|
return frames[frame];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class SeasonsOfLoneliness : public olc::PixelGameEngine
|
class SeasonsOfLoneliness : public olc::PixelGameEngine
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -78,8 +90,22 @@ public:
|
|||||||
int oxygenQualityLevel=34;
|
int oxygenQualityLevel=34;
|
||||||
int plantState=0b10010110101000101010100110101010;
|
int plantState=0b10010110101000101010100110101010;
|
||||||
olc::SplashScreen splash;
|
olc::SplashScreen splash;
|
||||||
|
Animation current_playerAnim;
|
||||||
|
Animation playerAnim;
|
||||||
|
Animation playerAnimRight;
|
||||||
|
Animation playerAnimLeft;
|
||||||
|
Animation playerAnimDown;
|
||||||
|
Animation playerAnimWalkUp;
|
||||||
|
Animation playerAnimWalkDown;
|
||||||
|
Animation playerAnimWalkRight;
|
||||||
|
Animation playerAnimWalkLeft;
|
||||||
|
Animation playerAnimPlantUp;
|
||||||
|
Animation playerAnimPlantDown;
|
||||||
|
Animation playerAnimPlantRight;
|
||||||
|
Animation playerAnimPlantLeft;
|
||||||
|
|
||||||
olc::Decal*DOME_DECAL,*FOOD_METER_DECAL,*OXYGEN_METER_DECAL,*PLANT_DECAL;
|
olc::Decal*DOME_DECAL,*FOOD_METER_DECAL,*OXYGEN_METER_DECAL,*PLANT_DECAL,
|
||||||
|
*PLAYER_DECAL;
|
||||||
std::map<std::string,olc::Decal*> BASE_OBJECTS;
|
std::map<std::string,olc::Decal*> BASE_OBJECTS;
|
||||||
|
|
||||||
bool OnUserCreate() override
|
bool OnUserCreate() override
|
||||||
@ -93,6 +119,46 @@ public:
|
|||||||
FOOD_METER_DECAL=new olc::Decal(new olc::Sprite("assets/corn.png"));
|
FOOD_METER_DECAL=new olc::Decal(new olc::Sprite("assets/corn.png"));
|
||||||
OXYGEN_METER_DECAL=new olc::Decal(new olc::Sprite("assets/co2.png"));
|
OXYGEN_METER_DECAL=new olc::Decal(new olc::Sprite("assets/co2.png"));
|
||||||
PLANT_DECAL=new olc::Decal(new olc::Sprite("assets/plant.png"));
|
PLANT_DECAL=new olc::Decal(new olc::Sprite("assets/plant.png"));
|
||||||
|
PLAYER_DECAL=new olc::Decal(new olc::Sprite("assets/player.png"));
|
||||||
|
|
||||||
|
current_playerAnim.spr=PLAYER_DECAL;
|
||||||
|
playerAnim.spr=PLAYER_DECAL;
|
||||||
|
playerAnimRight.spr=PLAYER_DECAL;
|
||||||
|
playerAnimLeft.spr=PLAYER_DECAL;
|
||||||
|
playerAnimLeft.flipped=true;
|
||||||
|
playerAnimDown.spr=PLAYER_DECAL;
|
||||||
|
playerAnimWalkUp.spr=PLAYER_DECAL;
|
||||||
|
playerAnimWalkDown.spr=PLAYER_DECAL;
|
||||||
|
playerAnimWalkRight.spr=PLAYER_DECAL;
|
||||||
|
playerAnimWalkLeft.spr=PLAYER_DECAL;
|
||||||
|
playerAnimWalkLeft.flipped=true;
|
||||||
|
playerAnimPlantUp.spr=PLAYER_DECAL;
|
||||||
|
playerAnimPlantDown.spr=PLAYER_DECAL;
|
||||||
|
playerAnimPlantRight.spr=PLAYER_DECAL;
|
||||||
|
playerAnimPlantLeft.spr=PLAYER_DECAL;
|
||||||
|
playerAnimPlantLeft.flipped=true;
|
||||||
|
playerAnim.frames.push_back({64,0});
|
||||||
|
playerAnimRight.frames.push_back({32,0});
|
||||||
|
playerAnimLeft.frames.push_back({32,0});
|
||||||
|
playerAnimDown.frames.push_back({0,0});
|
||||||
|
playerAnimWalkUp.frames.push_back({0,96});
|
||||||
|
playerAnimWalkUp.frames.push_back({32,96});
|
||||||
|
playerAnimWalkUp.frames.push_back({64,96});
|
||||||
|
playerAnimWalkRight.frames.push_back({0,64});
|
||||||
|
playerAnimWalkLeft.frames.push_back({0,64});
|
||||||
|
playerAnimWalkRight.frames.push_back({32,64});
|
||||||
|
playerAnimWalkLeft.frames.push_back({32,64});
|
||||||
|
playerAnimWalkRight.frames.push_back({64,64});
|
||||||
|
playerAnimWalkLeft.frames.push_back({64,64});
|
||||||
|
playerAnimWalkDown.frames.push_back({0,32});
|
||||||
|
playerAnimWalkDown.frames.push_back({32,32});
|
||||||
|
playerAnimWalkDown.frames.push_back({64,32});
|
||||||
|
playerAnimPlantUp.frames.push_back({64,128});
|
||||||
|
playerAnimPlantRight.frames.push_back({32,128});
|
||||||
|
playerAnimPlantLeft.frames.push_back({32,128});
|
||||||
|
playerAnimPlantDown.frames.push_back({0,128});
|
||||||
|
current_playerAnim=playerAnimWalkDown;
|
||||||
|
|
||||||
BASE_OBJECTS["DOME"]=DOME_DECAL;
|
BASE_OBJECTS["DOME"]=DOME_DECAL;
|
||||||
BASE_OBJECTS["PLANT"]=PLANT_DECAL;
|
BASE_OBJECTS["PLANT"]=PLANT_DECAL;
|
||||||
BASE_OBJECTS["EXIT"]=NULL;
|
BASE_OBJECTS["EXIT"]=NULL;
|
||||||
@ -101,6 +167,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GetAnyKeyPress() override {
|
void GetAnyKeyPress() override {
|
||||||
|
if (!GetKey(olc::W).bPressed&&!GetKey(olc::A).bPressed&&!GetKey(olc::S).bPressed&&!GetKey(olc::D).bPressed&&
|
||||||
|
!GetKey(olc::UP).bPressed&&!GetKey(olc::RIGHT).bPressed&&!GetKey(olc::DOWN).bPressed&&!GetKey(olc::LEFT).bPressed) {
|
||||||
|
ActionButtonPress();
|
||||||
|
}
|
||||||
if (messageBoxVisible) {
|
if (messageBoxVisible) {
|
||||||
if (messageBoxCursor!=messageBoxRefText.length()) {
|
if (messageBoxCursor!=messageBoxRefText.length()) {
|
||||||
while (messageBoxCursor<messageBoxRefText.length()) {
|
while (messageBoxCursor<messageBoxRefText.length()) {
|
||||||
@ -119,6 +189,17 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ActionButtonPress() {
|
||||||
|
switch (GAME_STATE) {
|
||||||
|
case GAMEWORLD:{
|
||||||
|
if (PLAYER_COORDS[0]>=8&&PLAYER_COORDS[0]<12&&
|
||||||
|
PLAYER_COORDS[1]>=2&&PLAYER_COORDS[1]<6) {
|
||||||
|
cout<<"You are standing over plant "<<getPlantId((int)PLAYER_COORDS[0],(int)PLAYER_COORDS[1])<<" in state "<<getPlantStatus((int)PLAYER_COORDS[0],(int)PLAYER_COORDS[1]);
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool OnUserUpdate(float fElapsedTime) override
|
bool OnUserUpdate(float fElapsedTime) override
|
||||||
{
|
{
|
||||||
elapsedTime+=fElapsedTime;
|
elapsedTime+=fElapsedTime;
|
||||||
@ -129,6 +210,35 @@ public:
|
|||||||
if (GetKey(olc::F1).bPressed) {
|
if (GetKey(olc::F1).bPressed) {
|
||||||
ConsoleShow(olc::F1,false);
|
ConsoleShow(olc::F1,false);
|
||||||
}
|
}
|
||||||
|
if (GetKey(olc::D).bPressed||GetKey(olc::RIGHT).bPressed) {
|
||||||
|
changeAnimation(playerAnimWalkRight);
|
||||||
|
}
|
||||||
|
if (GetKey(olc::A).bPressed||GetKey(olc::LEFT).bPressed) {
|
||||||
|
changeAnimation(playerAnimWalkLeft);
|
||||||
|
}
|
||||||
|
if (GetKey(olc::W).bPressed||GetKey(olc::UP).bPressed) {
|
||||||
|
changeAnimation(playerAnimWalkUp);
|
||||||
|
}
|
||||||
|
if (GetKey(olc::S).bPressed||GetKey(olc::DOWN).bPressed) {
|
||||||
|
changeAnimation(playerAnimWalkDown);
|
||||||
|
}
|
||||||
|
if (!GetKey(olc::D).bHeld&&!GetKey(olc::RIGHT).bHeld&&
|
||||||
|
!GetKey(olc::A).bHeld&&!GetKey(olc::LEFT).bHeld&&
|
||||||
|
!GetKey(olc::S).bHeld&&!GetKey(olc::UP).bHeld&&
|
||||||
|
!GetKey(olc::W).bHeld&&!GetKey(olc::DOWN).bHeld) {
|
||||||
|
if (GetKey(olc::D).bReleased||GetKey(olc::RIGHT).bReleased) {
|
||||||
|
changeAnimation(playerAnimRight);
|
||||||
|
}
|
||||||
|
if (GetKey(olc::A).bReleased||GetKey(olc::LEFT).bReleased) {
|
||||||
|
changeAnimation(playerAnimLeft);
|
||||||
|
}
|
||||||
|
if (GetKey(olc::W).bReleased||GetKey(olc::UP).bReleased) {
|
||||||
|
changeAnimation(playerAnim);
|
||||||
|
}
|
||||||
|
if (GetKey(olc::S).bReleased||GetKey(olc::DOWN).bReleased) {
|
||||||
|
changeAnimation(playerAnimDown);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
drawGame();
|
drawGame();
|
||||||
// called once per frame
|
// called once per frame
|
||||||
@ -201,27 +311,45 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
current_playerAnim.frame=(current_playerAnim.frame+1>=current_playerAnim.frames.size())?0:current_playerAnim.frame+1;
|
||||||
|
|
||||||
if (CURRENT_CUTSCENE==NONE&&!messageBoxVisible) {
|
if (CURRENT_CUTSCENE==NONE&&!messageBoxVisible) {
|
||||||
|
bool changed=false;
|
||||||
if (GetKey(olc::D).bHeld||GetKey(olc::RIGHT).bHeld) {
|
if (GetKey(olc::D).bHeld||GetKey(olc::RIGHT).bHeld) {
|
||||||
PLAYER_COORDS[0]=clamp(PLAYER_COORDS[0]+MOVE_SPD,0.1,(double)MAP_WIDTH);
|
PLAYER_COORDS[0]=clamp(PLAYER_COORDS[0]+MOVE_SPD,0.1,(double)MAP_WIDTH);
|
||||||
ConsoleClear();
|
ConsoleClear();
|
||||||
cout<<"("<<PLAYER_COORDS[0]<<","<<PLAYER_COORDS[1]<<+")";
|
cout<<"("<<PLAYER_COORDS[0]<<","<<PLAYER_COORDS[1]<<+")";
|
||||||
|
if (!changed&&¤t_playerAnim!=&playerAnimWalkRight) {
|
||||||
|
changeAnimation(playerAnimWalkRight);
|
||||||
|
changed=true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (GetKey(olc::A).bHeld||GetKey(olc::LEFT).bHeld) {
|
if (GetKey(olc::A).bHeld||GetKey(olc::LEFT).bHeld) {
|
||||||
PLAYER_COORDS[0]=clamp(PLAYER_COORDS[0]-MOVE_SPD,0.1,(double)MAP_WIDTH);
|
PLAYER_COORDS[0]=clamp(PLAYER_COORDS[0]-MOVE_SPD,0.1,(double)MAP_WIDTH);
|
||||||
ConsoleClear();
|
ConsoleClear();
|
||||||
cout<<"("<<PLAYER_COORDS[0]<<","<<PLAYER_COORDS[1]<<+")";
|
cout<<"("<<PLAYER_COORDS[0]<<","<<PLAYER_COORDS[1]<<+")";
|
||||||
|
if (!changed&&¤t_playerAnim!=&playerAnimWalkLeft) {
|
||||||
|
changeAnimation(playerAnimWalkLeft);
|
||||||
|
changed=true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (GetKey(olc::W).bHeld||GetKey(olc::UP).bHeld) {
|
if (GetKey(olc::W).bHeld||GetKey(olc::UP).bHeld) {
|
||||||
PLAYER_COORDS[1]=clamp(PLAYER_COORDS[1]-MOVE_SPD,0.1,(double)MAP_HEIGHT);
|
PLAYER_COORDS[1]=clamp(PLAYER_COORDS[1]-MOVE_SPD,0.1,(double)MAP_HEIGHT);
|
||||||
ConsoleClear();
|
ConsoleClear();
|
||||||
cout<<"("<<PLAYER_COORDS[0]<<","<<PLAYER_COORDS[1]<<+")";
|
cout<<"("<<PLAYER_COORDS[0]<<","<<PLAYER_COORDS[1]<<+")";
|
||||||
|
if (!changed&&¤t_playerAnim!=&playerAnimWalkUp) {
|
||||||
|
changeAnimation(playerAnimWalkUp);
|
||||||
|
changed=true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (GetKey(olc::S).bHeld||GetKey(olc::DOWN).bHeld) {
|
if (GetKey(olc::S).bHeld||GetKey(olc::DOWN).bHeld) {
|
||||||
PLAYER_COORDS[1]=clamp(PLAYER_COORDS[1]+MOVE_SPD,0.1,(double)MAP_HEIGHT);
|
PLAYER_COORDS[1]=clamp(PLAYER_COORDS[1]+MOVE_SPD,0.1,(double)MAP_HEIGHT);
|
||||||
ConsoleClear();
|
ConsoleClear();
|
||||||
cout<<"("<<PLAYER_COORDS[0]<<","<<PLAYER_COORDS[1]<<+")";
|
cout<<"("<<PLAYER_COORDS[0]<<","<<PLAYER_COORDS[1]<<+")";
|
||||||
|
if (!changed&&¤t_playerAnim!=&playerAnimWalkDown) {
|
||||||
|
changeAnimation(playerAnimWalkDown);
|
||||||
|
changed=true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -394,7 +522,8 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
FillRectDecal({0,0},{WIDTH,HEIGHT},olc::Pixel(0,0,0,transparency));
|
FillRectDecal({0,0},{WIDTH,HEIGHT},olc::Pixel(0,0,0,transparency));
|
||||||
FillRectDecal({WIDTH/2-2,HEIGHT/2-2},{4,4},olc::WHITE);
|
//FillRectDecal({WIDTH/2-2,HEIGHT/2-2},{4,4},olc::WHITE);
|
||||||
|
DrawPartialDecal({WIDTH/2-16+(current_playerAnim.flipped?32:0),HEIGHT/2-16},current_playerAnim.spr,current_playerAnim.getCurrentFrame(),{32,32},{current_playerAnim.flipped?-1:1,1});
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawGameWorld() {
|
void DrawGameWorld() {
|
||||||
@ -419,7 +548,7 @@ public:
|
|||||||
for (auto&obj:OBJECTS) {
|
for (auto&obj:OBJECTS) {
|
||||||
if (obj.spr!=NULL) {
|
if (obj.spr!=NULL) {
|
||||||
if (obj.name.compare("PLANT")==0) {
|
if (obj.name.compare("PLANT")==0) {
|
||||||
DrawPartialDecal({(obj.x-PLAYER_COORDS[0])*32+WIDTH/2,(obj.y-PLAYER_COORDS[1])*32+HEIGHT/2},{32,32},obj.spr,{getPlantStatus(obj)*32,0},{32,32});
|
DrawPartialDecal({(obj.x-PLAYER_COORDS[0])*32+WIDTH/2,(obj.y-PLAYER_COORDS[1])*32+HEIGHT/2},{32,32},obj.spr,{getPlantStatus(obj.x,obj.y)*32,0},{32,32});
|
||||||
} else {
|
} else {
|
||||||
DrawDecal({(obj.x-PLAYER_COORDS[0])*32+WIDTH/2,(obj.y-PLAYER_COORDS[1])*32+HEIGHT/2},obj.spr);
|
DrawDecal({(obj.x-PLAYER_COORDS[0])*32+WIDTH/2,(obj.y-PLAYER_COORDS[1])*32+HEIGHT/2},obj.spr);
|
||||||
}
|
}
|
||||||
@ -430,12 +559,12 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int getPlantId(Object obj) {
|
int getPlantId(int x, int y) {
|
||||||
return ((int)obj.x-8)%4+((int)obj.y-2)*4;
|
return ((int)x-8)%4+((int)y-2)*4;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getPlantStatus(Object obj) {
|
int getPlantStatus(int x,int y) {
|
||||||
return plantState>>getPlantId(obj)*2&0b11;
|
return plantState>>getPlantId(x,y)*2&0b11;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawDialogBox(const olc::vi2d &pos, const olc::vi2d &size, olc::Pixel p = olc::WHITE, olc::Pixel p2 = olc::DARK_GREY, olc::Pixel p3 = olc::VERY_DARK_GREY) {
|
void DrawDialogBox(const olc::vi2d &pos, const olc::vi2d &size, olc::Pixel p = olc::WHITE, olc::Pixel p2 = olc::DARK_GREY, olc::Pixel p3 = olc::VERY_DARK_GREY) {
|
||||||
@ -531,6 +660,11 @@ public:
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeAnimation(Animation anim) {
|
||||||
|
current_playerAnim=anim;
|
||||||
|
current_playerAnim.frame=0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 16 KiB |
@ -3442,6 +3442,7 @@ namespace olc
|
|||||||
// Compare hardware input states from previous frame
|
// Compare hardware input states from previous frame
|
||||||
auto ScanHardware = [&](HWButton* pKeys, bool* pStateOld, bool* pStateNew, uint32_t nKeyCount)
|
auto ScanHardware = [&](HWButton* pKeys, bool* pStateOld, bool* pStateNew, uint32_t nKeyCount)
|
||||||
{
|
{
|
||||||
|
bool pressed=false;
|
||||||
for (uint32_t i = 0; i < nKeyCount; i++)
|
for (uint32_t i = 0; i < nKeyCount; i++)
|
||||||
{
|
{
|
||||||
pKeys[i].bPressed = false;
|
pKeys[i].bPressed = false;
|
||||||
@ -3450,7 +3451,7 @@ namespace olc
|
|||||||
{
|
{
|
||||||
if (pStateNew[i])
|
if (pStateNew[i])
|
||||||
{
|
{
|
||||||
GetAnyKeyPress();
|
pressed=true;
|
||||||
pKeys[i].bPressed = !pKeys[i].bHeld;
|
pKeys[i].bPressed = !pKeys[i].bHeld;
|
||||||
pKeys[i].bHeld = true;
|
pKeys[i].bHeld = true;
|
||||||
}
|
}
|
||||||
@ -3463,6 +3464,7 @@ namespace olc
|
|||||||
}
|
}
|
||||||
pStateOld[i] = pStateNew[i];
|
pStateOld[i] = pStateNew[i];
|
||||||
}
|
}
|
||||||
|
if (pressed) {GetAnyKeyPress();}
|
||||||
};
|
};
|
||||||
|
|
||||||
ScanHardware(pKeyboardState, pKeyOldState, pKeyNewState, 256);
|
ScanHardware(pKeyboardState, pKeyOldState, pKeyNewState, 256);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user