The Table Engine
enemy_ai_script.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "enemy_ai_script.h"
4 #include "scenemanager.h"
5 #include "gameobject.h"
6 #include "transform.h"
7 #include "health.h"
8 #include "collide.h"
9 #include "texture.h"
10 
11 // Random speed between 2 and 4 float
13  float val = (float) rand() / RAND_MAX;
14  m_speed = 2 + val * 2;
15 }
16 
18  auto transform = m_owner->getComponent<Transform>();
19  auto texture = m_owner->getComponent<Texture>();
20  auto collide = m_owner->getComponent<Collide>();
21 
22  // move the enemy towards the player
23  auto players = SceneManager::getInstance().getSceneTree()->findGameObjectsByTag("Player");
24  if (players.empty()) return;
25  auto player = players[0];
26 
27  float dx = player->getComponent<Transform>()->getWorldPosition().x - transform->getWorldPosition().x;
28  float dy = player->getComponent<Transform>()->getWorldPosition().y - transform->getWorldPosition().y;
29 
30  float norm = sqrt(dx * dx + dy * dy);
31  dx = dx / norm * m_speed;
32  dy = dy / norm * m_speed;
33 
34  // enemy faces the player
35  texture->setFlipHorizontal(dx < 0);
36 
37  // prevent the enemy from moving through other enemies
38  auto enemies = SceneManager::getInstance().getSceneTree()->findGameObjectsByTag("Warrior");
39  for (auto enemy : enemies) {
40  if (enemy == m_owner) continue;
41  auto enemyCollide = enemy->getComponent<Collide>();
42  collide->preventCollision(enemyCollide, dx, dy);
43  }
44 
45  // prevent hitting walls
47  for (auto wall : walls) {
48  auto wallCollide = wall->getComponent<Collide>();
49  collide->preventCollision(wallCollide, dx, dy);
50  }
51 
52  // prevent hitting tables
54  for (auto table : tables) {
55  auto tableCollide = table->getComponent<Collide>();
56  collide->preventCollision(tableCollide, dx, dy);
57  }
58 
59  // prevent the enemy from moving through player
60  // auto playerCollide = player->getComponent<Collide>();
61  // collide->preventCollision(playerCollide, dx, dy);
62 
63  transform->updateWorldPosition(dx, dy);
64 }
65 
67  // place a health bar above the enemy
68  auto transform = m_owner->getComponent<Transform>();
69  Vec2 screenPos = transform->getScreenPosition();
70  Vec2 screenSize = transform->getScreenSize();
71 
72  auto health = m_owner->getComponent<Health>();
73  int health_width = (int) (20.0 * health->getHealth() / 200);
74  SDL_Rect rect = {(int) (screenPos.x + screenSize.x / 2) - 10, (int) (screenPos.y + screenSize.y / 2) - 8, health_width, 6};
75  SDL_SetRenderDrawColor(SceneManager::getInstance().getRenderer(), 255, 0, 0, 255);
76  SDL_RenderFillRect(SceneManager::getInstance().getRenderer(), &rect);
77  // draw a border around the health bar
78  rect = {(int) (screenPos.x + screenSize.x / 2) - 10, (int) (screenPos.y + screenSize.y / 2) - 8, 20, 6};
79  SDL_SetRenderDrawColor(SceneManager::getInstance().getRenderer(), 0, 0, 0, 255);
80  SDL_RenderDrawRect(SceneManager::getInstance().getRenderer(), &rect);
81 }
Component to handle collisions between game objects.
Definition: collide.h:24
void update() override
Updates the behavior of the enemy each frame.
void render() override
Renders the enemy AI behavior.
EnemyAIScript()
Constructs a new Enemy AI Script object.
T * getComponent()
Gets a component of the specified type.
Definition: gameobject.h:123
A component for managing health of game objects.
Definition: health.h:22
static SceneManager & getInstance()
Retrieves the singleton instance of SceneManager.
SceneTree * getSceneTree()
Retrieves the current scene tree managing the game objects.
std::vector< GameObject * > findGameObjectsByTag(std::string tag)
Finds GameObjects by a tag.
Definition: scene.cpp:114
GameObject * m_owner
Pointer to the GameObject that owns this script.
Definition: script.h:25
Component for handling textures in the game engine.
Definition: texture.h:20
Component for handling the transformation of game objects.
Definition: transform.h:23
Vec2 getWorldPosition()
Get the world space position of the Transform.
Definition: transform.cpp:34
Defines the Collide class, a component for managing collision detection and response.
Defines the Health class, a component for managing the health of game objects.
Defines the SceneManager class for managing scenes in the game.
A structure to represent 2D vectors.
Definition: vec2.h:16
float y
Y coordinate of the vector.
Definition: vec2.h:18
float x
X coordinate of the vector.
Definition: vec2.h:17
Defines the Texture component for managing textures within the Game Engine, encapsulating SDL texture...
Provides the Transform component for positioning and sizing game objects.