Refactor animation data location.

pull/28/head
sigonasr2 1 year ago
parent 86867cd82c
commit 1e0e40c77b
  1. 6
      Crawler/Animation.h
  2. 1
      Crawler/Crawler.vcxproj
  3. 3
      Crawler/Crawler.vcxproj.filters
  4. 2
      Crawler/Monster.cpp
  5. 9
      Crawler/Monster.h
  6. BIN
      Crawler/assets/slime.png
  7. 118
      Crawler/main.cpp

@ -0,0 +1,6 @@
#pragma once
enum AnimationState{
WALK_S,WALK_E,WALK_N,WALK_W,
IDLE_S,IDLE_E,IDLE_N,IDLE_W
};

@ -129,6 +129,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Animation.h" />
<ClInclude Include="Monster.h" />
<ClInclude Include="olcPGEX_TransformedView.h" />
<ClInclude Include="olcPixelGameEngine.h" />

@ -33,6 +33,9 @@
<ClInclude Include="Monster.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Animation.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">

@ -1,5 +1,7 @@
#include "Monster.h"
extern std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
MonsterData::MonsterData(){}
MonsterData::MonsterData(int hp,int atk,float moveSpd,float size,MonsterStrategy strategy):
hp(hp),atk(atk),moveSpd(moveSpd),size(size),strategy(strategy){

@ -1,4 +1,6 @@
#include "olcPixelGameEngine.h"
#include "Animation.h"
#include "olcUTIL_Animate2D.h"
enum MonsterStrategy{
RUN_TOWARDS,
@ -12,6 +14,7 @@ struct MonsterData{
float moveSpd;//1.0=100%
float size;
MonsterStrategy strategy;
Animate2D::Animation<AnimationState>animation;
public:
MonsterData();
MonsterData(int hp,int atk,float moveSpd=1.0f,float size=1.0f,MonsterStrategy strategy=RUN_TOWARDS);
@ -45,4 +48,10 @@ struct Monster{
int GetAttack();
float GetMoveSpdMult();
float GetSizeMult();
};
struct MonsterSpawner{
vf2d pos;
int range;
std::vector<std::pair<MonsterData,vf2d>>monsters;
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 29 KiB

@ -5,15 +5,12 @@
#include "olcPGEX_TransformedView.h"
#include "olcUTIL_Animate2D.h"
#include "Monster.h"
#include "Animation.h"
const vi2d WINDOW_SIZE={24*8,24*8};
extern std::map<MonsterName,MonsterData>MONSTER_DATA;
enum AnimationState{
WALK_S,WALK_E,WALK_N,WALK_W,
IDLE_S,IDLE_E,IDLE_N,IDLE_W
};
std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
struct Player{
private:
@ -72,8 +69,8 @@ struct Player{
void Update(float fElapsedTime){
animation.UpdateState(internal_animState,fElapsedTime);
}
void AddAnimation(AnimationState state,Animate2D::FrameSequence frame){
animation.AddState(state,frame);
void AddAnimation(AnimationState state){
animation.AddState(state,ANIMATION_DATA[state]);
};
void UpdateAnimation(AnimationState animState){
animation.ChangeState(internal_animState,animState);
@ -119,61 +116,72 @@ public:
GFX_Pl_sheet.Load("assets/nico-warrior.png");
//Animations
InitializeAnimations();
player.AddAnimation(AnimationState::WALK_N);
player.AddAnimation(AnimationState::WALK_E);
player.AddAnimation(AnimationState::WALK_S);
player.AddAnimation(AnimationState::WALK_W);
player.AddAnimation(AnimationState::IDLE_N);
player.AddAnimation(AnimationState::IDLE_E);
player.AddAnimation(AnimationState::IDLE_S);
player.AddAnimation(AnimationState::IDLE_W);
view=TileTransformedView{GetScreenSize(),{1,1}};
player.SetPos({4*24,4*24});
player.UpdateAnimation(AnimationState::IDLE_S);
std::cout<<MONSTER_DATA[SLIME_BLUE].GetHealth()<<std::endl;
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
HandleUserInput(fElapsedTime);
player.Update(fElapsedTime);
UpdateCamera(fElapsedTime);
RenderWorld();
return true;
}
void InitializeAnimations(){
Animate2D::FrameSequence pl_walk_s{0.2};
pl_walk_s.AddFrame({&GFX_Pl_sheet,{vi2d{0,0}*24,{24,24}}});
pl_walk_s.AddFrame({&GFX_Pl_sheet,{vi2d{1,0}*24,{24,24}}});
pl_walk_s.AddFrame({&GFX_Pl_sheet,{vi2d{0,0}*24,{24,24}}});
pl_walk_s.AddFrame({&GFX_Pl_sheet,{vi2d{2,0}*24,{24,24}}});
ANIMATION_DATA[AnimationState::WALK_S]=pl_walk_s;
Animate2D::FrameSequence pl_walk_e{0.2};
pl_walk_e.AddFrame({&GFX_Pl_sheet,{vi2d{0,3}*24,{24,24}}});
pl_walk_e.AddFrame({&GFX_Pl_sheet,{vi2d{1,3}*24,{24,24}}});
pl_walk_e.AddFrame({&GFX_Pl_sheet,{vi2d{0,3}*24,{24,24}}});
pl_walk_e.AddFrame({&GFX_Pl_sheet,{vi2d{2,3}*24,{24,24}}});
ANIMATION_DATA[AnimationState::WALK_E]=pl_walk_e;
Animate2D::FrameSequence pl_walk_w{0.2};
pl_walk_w.AddFrame({&GFX_Pl_sheet,{vi2d{0,2}*24,{24,24}}});
pl_walk_w.AddFrame({&GFX_Pl_sheet,{vi2d{1,2}*24,{24,24}}});
pl_walk_w.AddFrame({&GFX_Pl_sheet,{vi2d{0,2}*24,{24,24}}});
pl_walk_w.AddFrame({&GFX_Pl_sheet,{vi2d{2,2}*24,{24,24}}});
ANIMATION_DATA[AnimationState::WALK_W]=pl_walk_w;
Animate2D::FrameSequence pl_walk_n{0.2};
pl_walk_n.AddFrame({&GFX_Pl_sheet,{vi2d{0,1}*24,{24,24}}});
pl_walk_n.AddFrame({&GFX_Pl_sheet,{vi2d{1,1}*24,{24,24}}});
pl_walk_n.AddFrame({&GFX_Pl_sheet,{vi2d{0,1}*24,{24,24}}});
pl_walk_n.AddFrame({&GFX_Pl_sheet,{vi2d{2,1}*24,{24,24}}});
ANIMATION_DATA[AnimationState::WALK_N]=pl_walk_n;
Animate2D::FrameSequence pl_idle_s;
pl_idle_s.AddFrame({&GFX_Pl_sheet,{vi2d{0,0}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_S]=pl_idle_s;
Animate2D::FrameSequence pl_idle_e;
pl_idle_e.AddFrame({&GFX_Pl_sheet,{vi2d{0,3}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_E]=pl_idle_e;
Animate2D::FrameSequence pl_idle_w;
pl_idle_w.AddFrame({&GFX_Pl_sheet,{vi2d{0,2}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_W]=pl_idle_w;
Animate2D::FrameSequence pl_idle_n;
pl_idle_n.AddFrame({&GFX_Pl_sheet,{vi2d{0,1}*24,{24,24}}});
player.AddAnimation(WALK_N,pl_walk_n);
player.AddAnimation(WALK_E,pl_walk_e);
player.AddAnimation(WALK_S,pl_walk_s);
player.AddAnimation(WALK_W,pl_walk_w);
player.AddAnimation(IDLE_N,pl_idle_n);
player.AddAnimation(IDLE_E,pl_idle_e);
player.AddAnimation(IDLE_S,pl_idle_s);
player.AddAnimation(IDLE_W,pl_idle_w);
view=TileTransformedView{GetScreenSize(),{1,1}};
player.SetPos({4*24,4*24});
player.UpdateAnimation(IDLE_S);
std::cout<<MONSTER_DATA[SLIME_BLUE].GetHealth()<<std::endl;
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
HandleUserInput(fElapsedTime);
player.Update(fElapsedTime);
UpdateCamera(fElapsedTime);
RenderWorld();
return true;
ANIMATION_DATA[AnimationState::IDLE_N]=pl_idle_n;
}
void HandleUserInput(float fElapsedTime){
@ -184,7 +192,7 @@ public:
} else {
player.SetX(WORLD_SIZE.x*24-12);
}
player.UpdateAnimation(WALK_E);
player.UpdateAnimation(AnimationState::WALK_E);
setIdleAnimation=false;
}
if(GetKey(LEFT).bHeld){
@ -194,7 +202,7 @@ public:
player.SetX(12);
}
if(setIdleAnimation){
player.UpdateAnimation(WALK_W);
player.UpdateAnimation(AnimationState::WALK_W);
}
setIdleAnimation=false;
}
@ -205,7 +213,7 @@ public:
player.SetY(12);
}
if(setIdleAnimation){
player.UpdateAnimation(WALK_N);
player.UpdateAnimation(AnimationState::WALK_N);
}
setIdleAnimation=false;
}
@ -216,7 +224,7 @@ public:
player.SetY(WORLD_SIZE.y*24-12);
}
if(setIdleAnimation){
player.UpdateAnimation(WALK_S);
player.UpdateAnimation(AnimationState::WALK_S);
}
setIdleAnimation=false;
}
@ -224,68 +232,68 @@ public:
if(GetKey(UP).bReleased){
player.SetLastReleasedMovementKey(UP);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(WALK_E);
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(WALK_S);
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(WALK_W);
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(GetKey(RIGHT).bReleased){
player.SetLastReleasedMovementKey(RIGHT);
if(GetKey(UP).bHeld){
player.UpdateAnimation(WALK_N);
player.UpdateAnimation(AnimationState::WALK_N);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(WALK_S);
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(WALK_W);
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(GetKey(LEFT).bReleased){
player.SetLastReleasedMovementKey(LEFT);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(WALK_E);
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(WALK_S);
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(UP).bHeld){
player.UpdateAnimation(WALK_N);
player.UpdateAnimation(AnimationState::WALK_N);
}
}
if(GetKey(DOWN).bReleased){
player.SetLastReleasedMovementKey(DOWN);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(WALK_E);
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(UP).bHeld){
player.UpdateAnimation(WALK_N);
player.UpdateAnimation(AnimationState::WALK_N);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(WALK_W);
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(setIdleAnimation){
switch(player.GetLastReleasedMovementKey()){
case UP:{
player.UpdateAnimation(IDLE_N);
player.UpdateAnimation(AnimationState::IDLE_N);
}break;
case DOWN:{
player.UpdateAnimation(IDLE_S);
player.UpdateAnimation(AnimationState::IDLE_S);
}break;
case LEFT:{
player.UpdateAnimation(IDLE_W);
player.UpdateAnimation(AnimationState::IDLE_W);
}break;
case RIGHT:{
player.UpdateAnimation(IDLE_E);
player.UpdateAnimation(AnimationState::IDLE_E);
}break;
default:{
player.UpdateAnimation(IDLE_S);
player.UpdateAnimation(AnimationState::IDLE_S);
}
}
}

Loading…
Cancel
Save