The Table Engine
ranged_weapon_script.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <cmath>
3 
4 #include "ranged_weapon_script.h"
5 #include "gameobject.h"
6 #include "input.h"
7 #include "gameobjectfactory.h"
8 #include "transform.h"
9 #include "animation.h"
10 #include "scenemanager.h"
11 
13  m_shoot = false;
14  m_lastFireTimeInMs = -10000;
16  m_animationPlayed = false;
17 }
18 
20  if (m_owner->getTag() == "Bow") {
21  auto animation = m_owner->getComponent<Animation>();
22 
24  (int)SDL_GetTicks() - m_lastFireTimeInMs > 1000 / m_fireRatePerSecond) {
25  m_shoot = true;
26  animation->setFrame(0);
27  animation->play();
28  m_animationPlayed = false;
29  }
30  }
31 }
32 
34  auto input = m_owner->getComponent<Input>();
35  auto transform = m_owner->getComponent<Transform>();
36  auto animation = m_owner->getComponent<Animation>();
37 
38  int bowX = transform->getScreenPosition().x;
39  int bowY = transform->getScreenPosition().y;
40  int dx = input->m_mouseX - bowX;
41  int dy = input->m_mouseY - bowY;
42 
43  float angle = atan2(dy, dx) * (180.0f / M_PI);
44 
45  if (angle < 0) {
46  angle += 360.0f;
47  }
48 
49  animation->setAngle(angle);
50 
51  if (m_shoot) {
52  m_lastFireTimeInMs = SDL_GetTicks();
53 
54  auto arrow = GameObjectFactory::createArrow(dx, dy);
55  arrow->getComponent<Transform>()->setWorldPosition(transform->getWorldPosition());
56 
57  auto sceneTree = SceneManager::getInstance().getSceneTree();
58  sceneTree->addChild(arrow);
59  // static int arrowCount = 0;
60  // arrowCount++;
61 
62  m_shoot = false;
63  }
64 
65  if (animation->isPlaying() && animation->getCurrentFrame() == (animation->getNumFrames() - 1) && !m_animationPlayed) {
66  animation->pause();
67  animation->setFrame(0);
68  m_animationPlayed = true;
69  }
70 }
Defines the Animation class for handling sprite animations.
Component to handle animation of game objects. Derived from Texture.
Definition: animation.h:21
static GameObject * createArrow(int dx, int dy)
Creates an arrow object with specified dynamics.
std::string getTag()
Gets the tag identifier of the GameObject.
Definition: gameobject.cpp:32
T * getComponent()
Gets a component of the specified type.
Definition: gameobject.h:123
Component for handling and storing the state of input devices.
Definition: input.h:20
bool spacePressed
True if the space bar is pressed, false otherwise.
Definition: input.h:22
bool m_shoot
Flag to indicate whether the weapon should shoot.
RangedWeaponScript()
Constructs a RangedWeaponScript with default or initial settings.
void input() override
Processes input to handle shooting actions.
int m_lastFireTimeInMs
Timestamp of the last shooting action in milliseconds.
bool m_animationPlayed
Ensures that the shooting animation plays correctly.
void update() override
Updates the state of the weapon based on input and time constraints.
int m_fireRatePerSecond
Allowed number of shots per second to control shooting frequency.
static SceneManager & getInstance()
Retrieves the singleton instance of SceneManager.
SceneTree * getSceneTree()
Retrieves the current scene tree managing the game objects.
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 Input component for handling input operations in the game engine.
Defines the SceneManager class for managing scenes in the game.
Provides the Transform component for positioning and sizing game objects.