The Table Engine
player_input_script.cpp
Go to the documentation of this file.
1 // player_input_script.cpp
2 #include "player_input_script.h"
3 
4 #include "input.h"
5 #include "transform.h"
6 #include "texture.h"
7 #include "collide.h"
8 #include "health.h"
9 #include "gameobject.h"
10 #include "gameobjectfactory.h"
11 #include "scenemanager.h"
12 #include "health.h"
13 #include "animationsmanager.h"
14 
15 #include <iostream>
16 #include <iostream>
17 #include <string>
18 
20  m_lastFireTimeInMs = -10000;
21 }
22 
24  return m_keysCollected;
25 }
26 
28  auto input = m_owner->getComponent<Input>();
29  auto transform = m_owner->getComponent<Transform>();
30  auto texture = m_owner->getComponent<Texture>();
31  auto collide = m_owner->getComponent<Collide>();
32  auto health = m_owner->getComponent<Health>();
33  auto animations = dynamic_cast<AnimationsManager*>(texture);
34 
35  // Handle movement
36  bool isMoving = false;
37 
38  if (input->leftPressed) {
39  isMoving = true;
40  }
41  if (input->rightPressed) {
42  isMoving = true;
43  }
44  if (input->upPressed) {
45  isMoving = true;
46  }
47  if (input->downPressed) {
48  isMoving = true;
49  }
50 
51  // Update animation state based on movement and health
52  AnimationState newState = m_currentState;
53  if (health->getHealth() <= 0) {
54  newState = DEATH;
55  }
56  else if (isMoving) {
57  newState = RUN;
58  }
59  else {
60  newState = IDLE;
61  }
62 
63  // Change animation if state changed
64  if (newState != m_currentState && animations) {
65  m_currentState = newState;
66  animations->setCurrentAnimation(static_cast<size_t>(m_currentState));
67  }
68 
69 
70  Vec2 position = transform->getWorldPosition();
71 
72  if (input->leftPressed) {
73  position.x -= m_playerSpeed;
74  }
75  if (input->rightPressed) {
76  position.x += m_playerSpeed;
77  }
78  if (input->upPressed) {
79  position.y -= m_playerSpeed;
80  }
81  if (input->downPressed) {
82  position.y += m_playerSpeed;
83  }
84 
85  // clamp positions to level
86  int levelWidth = 640;
87  int levelHeight = 640;
88  bool isDemo = SceneManager::getInstance().isDemo;
89 
90  if (isDemo && position.x < -levelWidth)
91  position.x = -levelWidth;
92  else if (!isDemo && position.x < -levelWidth + 632)
93  position.x = -levelWidth + 632;
94  if (isDemo && position.x > levelWidth - 48)
95  position.x = levelWidth - 48;
96  else if (!isDemo && position.x > levelWidth + 124)
97  position.x = levelWidth + 124;
98  if (isDemo && position.y < -levelHeight)
99  position.y = -levelHeight;
100  else if (!isDemo && position.y < -levelHeight + 640)
101  position.y = -levelHeight + 640;
102  if (isDemo && position.y > levelHeight - 48)
103  position.y = levelHeight - 48;
104  else if (!isDemo && position.y > levelHeight + 116)
105  position.y = levelHeight + 116;
106 
107  float dx = position.x - transform->getWorldPosition().x;
108  float dy = position.y - transform->getWorldPosition().y;
109 
110  transform->updateWorldPosition(dx, dy);
111 
112  // move the player's bow with the player
113  if (!m_owner->getChildren().empty()) {
114  auto bow = m_owner->getChildren()[0];
115  Vec2 bowOffset(16, 8); // Adjust these values to position the bow correctly
116  bow->getComponent<Transform>()->setWorldPosition(position.x + bowOffset.x, position.y + bowOffset.y);
117  }
118 
119  texture->setFlipHorizontal(dx < 0);
120 
121  // prevent hitting walls
123  for (auto wall : walls) {
124  auto wallCollide = wall->getComponent<Collide>();
125  collide->preventCollision(wallCollide, dx, dy);
126  }
127 
128  // prevent hitting tables
129  auto tables = SceneManager::getInstance().getSceneTree()->findGameObjectsByTag("Table");
130  for (auto table : tables) {
131  auto tableCollide = table->getComponent<Collide>();
132  collide->preventCollision(tableCollide, dx, dy);
133  }
134 
135  // collect keys
137  for (auto key : keys) {
138  auto keyCollide = key->getComponent<Collide>();
139  if (collide->isColliding(keyCollide)) {
140  key->getSceneNode()->setDestroy(true);
141  m_keysCollected++;
142  }
143  }
144 
145  // health
146  for (auto enemy : SceneManager::getInstance().getSceneTree()->findGameObjectsByTag("Warrior")) {
147  auto enemyCollide = enemy->getComponent<Collide>();
148  if (collide->isColliding(enemyCollide)) {
149  health->updateHealth(-10);
150  }
151  }
152 
153 }
Manages a collection of animations for a game entity, with functionality to play, pause,...
Component to handle collisions between game objects.
Definition: collide.h:24
T * getComponent()
Gets a component of the specified type.
Definition: gameobject.h:123
std::vector< GameObject * > getChildren()
Gets all child GameObjects.
Definition: gameobject.cpp:44
A component for managing health of game objects.
Definition: health.h:22
Component for handling and storing the state of input devices.
Definition: input.h:20
int getKeysCollected()
Retrieves the number of keys collected by the player.
AnimationState m_currentState
Current animation state of the player.
void update() override
Updates player state based on input.
AnimationState
Defines the possible animation states for the player.
@ RUN
Player is moving.
@ DEATH
Player has died.
@ IDLE
Player is standing still.
PlayerInputScript()
Constructor for PlayerInputScript. Initializes the script with default values for player speed and fi...
int m_lastFireTimeInMs
Timestamp of the last fire to regulate shooting frequency.
float m_playerSpeed
Speed at which the player moves.
int m_keysCollected
Number of keys the player has collected.
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
virtual void input()
Virtual method for handling input. Should be overridden by derived classes.
Definition: script.cpp:21
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
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 Input component for handling input operations in the game engine.
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.