Refactor the entity out.
This commit is contained in:
parent
cac7017563
commit
a7f6357e12
6 changed files with 126 additions and 111 deletions
59
main.cpp
59
main.cpp
|
@ -27,6 +27,7 @@
|
|||
#include <fmt/core.h>
|
||||
#include "fsm.hpp"
|
||||
#include "map.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "dbc.hpp"
|
||||
|
||||
using std::string;
|
||||
|
@ -52,64 +53,6 @@ std::array<sf::Color, 10> VALUES{
|
|||
sf::Color::Transparent, // white
|
||||
};
|
||||
|
||||
enum class EntityState {
|
||||
START, HUNTING, DEAD
|
||||
};
|
||||
|
||||
enum class EntityEvent {
|
||||
GO, HIT
|
||||
};
|
||||
|
||||
|
||||
class Entity : public DeadSimpleFSM<EntityState, EntityEvent> {
|
||||
public:
|
||||
Point location;
|
||||
int hp = 20;
|
||||
int damage = 10;
|
||||
|
||||
Entity(Point loc) : location(loc) {
|
||||
};
|
||||
|
||||
// disable copy
|
||||
Entity(Entity &e) = delete;
|
||||
|
||||
void move(Point loc) {
|
||||
location = loc;
|
||||
}
|
||||
|
||||
void event(EntityEvent ev) {
|
||||
switch(_state) {
|
||||
FSM_STATE(EntityState, START, ev);
|
||||
FSM_STATE(EntityState, HUNTING, ev);
|
||||
FSM_STATE(EntityState, DEAD, ev);
|
||||
}
|
||||
}
|
||||
|
||||
void START(EntityEvent ev) {
|
||||
state(EntityState::HUNTING);
|
||||
}
|
||||
|
||||
void HUNTING(EntityEvent ev) {
|
||||
switch(ev) {
|
||||
case EntityEvent::HIT:
|
||||
hp -= damage;
|
||||
break;
|
||||
default:
|
||||
state(EntityState::HUNTING);
|
||||
}
|
||||
|
||||
if(hp <= 0) {
|
||||
state(EntityState::DEAD);
|
||||
} else {
|
||||
state(EntityState::HUNTING);
|
||||
}
|
||||
}
|
||||
|
||||
void DEAD(EntityEvent ev) {
|
||||
state(EntityState::DEAD);
|
||||
}
|
||||
};
|
||||
|
||||
sf::SoundBuffer g_hit_buf;
|
||||
sf::Sound g_hit_sound;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue