Add support for interactions with entities
This commit is contained in:
parent
3177ba2224
commit
d9f829f10a
4 changed files with 30 additions and 0 deletions
|
@ -71,6 +71,15 @@ void MapContext::movePlayerContinue(uint64_t time) {
|
|||
movePlayer(Model::Direction::WEST, time);
|
||||
}
|
||||
|
||||
void MapContext::interact(uint64_t time) {
|
||||
Model::Position<int> p = map.getEntityPosition(playerEntity) + playerEntity->getDirection();
|
||||
Model::Entity *target = map.getEntityAt(p);
|
||||
|
||||
if (!target)
|
||||
return;
|
||||
|
||||
target->interact(time);
|
||||
}
|
||||
|
||||
void MapContext::keyPressed(uint16_t key, uint64_t time) {
|
||||
switch (key) {
|
||||
|
@ -88,6 +97,10 @@ void MapContext::keyPressed(uint16_t key, uint64_t time) {
|
|||
|
||||
case SDL_SCANCODE_LEFT:
|
||||
movePlayer(Model::Direction::WEST, time);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_SPACE:
|
||||
interact(time);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,6 +52,7 @@ private:
|
|||
void movePlayer(Model::Direction dir, uint64_t time);
|
||||
void movePlayerContinue(uint64_t time);
|
||||
void keyPressed(uint16_t key, uint64_t time);
|
||||
void interact(uint64_t time);
|
||||
|
||||
Model::Position<float> getViewPosition(uint64_t time) {
|
||||
return map.getEntityPosition(playerEntity, time);
|
||||
|
|
|
@ -61,6 +61,10 @@ public:
|
|||
void setDirection(Direction dir) {
|
||||
direction = dir;
|
||||
}
|
||||
|
||||
void interact(uint64_t time) {
|
||||
std::fprintf(stderr, "Tried to interact with `%s' entity at %llu\n", name.c_str(), (unsigned long long)time);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -223,6 +223,18 @@ public:
|
|||
return tiles[layer][p.y*width + p.x];
|
||||
}
|
||||
|
||||
Entity * getEntityAt(const Position<int> &p) {
|
||||
auto it = positions.find(p);
|
||||
if (it == positions.end())
|
||||
return nullptr;
|
||||
|
||||
return *it->second.begin();
|
||||
}
|
||||
|
||||
Position<int> getEntityPosition(const Entity *entity) const {
|
||||
return entityStates.at(entity).position;
|
||||
}
|
||||
|
||||
Position<float> getEntityPosition(const Entity *entity, uint64_t time) const;
|
||||
bool moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end);
|
||||
void moveEntityTo(Entity *entity, Position<int> pos);
|
||||
|
|
Reference in a new issue