The Table Engine
projectile_script.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "projectile_script.h"
4 #include "transform.h"
5 #include "health.h"
6 #include "gameobject.h"
7 #include "collide.h"
8 #include "scenemanager.h"
9 
11  float norm = sqrt(dx * dx + dy * dy);
12  float speed = 12;
13  m_dx = dx / norm * speed;
14  m_dy = dy / norm * speed;
15 }
16 
18  auto transform = m_owner->getComponent<Transform>();
19 
20  // Move the arrow
21  Vec2 position = transform->getWorldPosition();
22  position.x += m_dx;
23  position.y += m_dy;
24  transform->setWorldPosition(position);
25 
26  // Destroy the arrow if it goes off screen
27  // Vec2 screen_position = transform->getScreenPosition();
28  // if (screen_position.x < 0 || screen_position.x > 640 || screen_position.y < 0 || screen_position.y > 480) {
29  // m_owner->getSceneNode()->setDestroy(true);
30  // return;
31  // }
32 
33  // Destroy the arrow if it goes very far into the world
34  if (position.x < -1200 || position.x > 1200 || position.y < -1200 || position.y > 1200) {
36  return;
37  }
38 
39  // Check collision with enemies
40  auto sceneTree = SceneManager::getInstance().getSceneTree();
41  auto enemies = sceneTree->findGameObjectsByTag("Warrior");
42  auto collide = m_owner->getComponent<Collide>();
43  auto walls = sceneTree->findGameObjectsByTag("Wall");
44  for (auto wall: walls) {
45  if (collide->isColliding(wall->getComponent<Collide>())) {
47  break;
48  }
49  }
50  for (auto enemy : enemies) {
51  if (collide->isColliding(enemy->getComponent<Collide>())) {
53  enemy->getComponent<Health>()->updateHealth(-100);
54  // static int arrowCount = 0;
55  // arrowCount++;
56  // std::cout << "Arrow hit enemy " << arrowCount << std::endl;
57  if (enemy->getComponent<Health>()->getHealth() <= 0)
58  enemy->getSceneNode()->setDestroy(true);
59  // std::cout << "Enemy is dead" << std::endl;
60  break;
61  }
62  }
63 }
64 
Component to handle collisions between game objects.
Definition: collide.h:24
SceneNode * getSceneNode()
Gets the SceneNode associated with this GameObject.
Definition: gameobject.cpp:36
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
int getHealth()
Retrieves the current health of the game object.
Definition: health.cpp:21
float m_dx
Horizontal velocity component of the projectile.
float m_dy
Vertical velocity component of the projectile.
ProjectileScript(float dx, float dy)
Constructs a ProjectileScript with specified velocity components.
void update() override
Updates the position of the projectile based on its velocity.
static SceneManager & getInstance()
Retrieves the singleton instance of SceneManager.
SceneTree * getSceneTree()
Retrieves the current scene tree managing the game objects.
void setDestroy(bool destroy)
Sets the destruction state of this node.
Definition: scene.cpp:65
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 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 SceneManager class for managing scenes in the game.
A structure to represent 2D vectors.
Definition: vec2.h:16
float x
X coordinate of the vector.
Definition: vec2.h:17
Provides the Transform component for positioning and sizing game objects.