This repository was archived by the owner on Sep 29, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathgame_object.cpp
More file actions
74 lines (61 loc) · 2.05 KB
/
Copy pathgame_object.cpp
File metadata and controls
74 lines (61 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "game_object.h"
#include "service_locator.h"
#include "ezolib.h"
GameObject::GameObject(TextureRect texrect, sf::Vector2f position, unsigned int level) :
representation(texrect, position, level)
{}
void GameObject::updatePosition(sf::Time elapsedTime)
{
sf::Vector2f acceleration(resultantForce.x / mass, resultantForce.y / mass);
//calculate velocity
sf::Vector2f newVelocity = velocity + acceleration*elapsedTime.asSeconds();
float newVelocityLen = ezo::vecLength(newVelocity.x, newVelocity.y);
if(newVelocityLen > maxVelocity)
velocity = {newVelocity.x/newVelocityLen*maxVelocity, newVelocity.y/newVelocityLen*maxVelocity};
else
velocity = newVelocity;
oldPosition = representation.getPosition();
representation.setPosition(representation.getPosition() + velocity*elapsedTime.asSeconds());
}
void GameObject::addForce(sf::Vector2f f)
{
resultantForce += f;
}
void GameObject::hit(unsigned int _hp)
{
if(_hp > hp)
destroy();
else
hp -= _hp;
}
void GameObject::destroy()
{
servLoc.getRender()->removeObj(&representation);
exist = false;
}
void GameObject::calculateAngle(sf::Time elapsedTime, sf::Vector2f target, bool neg)
{
const float maxDelta = this->deltaAngle * elapsedTime.asSeconds();
float newAngle = (float)-(atan2(representation.getPosition().x - target.x, representation.getPosition().y - target.y));
newAngle = ezo::radToDeg(newAngle);
if(neg) newAngle -= 180.f;
if(newAngle < 0) newAngle += 360;
float dAngle = newAngle - angle;
if(dAngle > 180) dAngle -= 360;
else if(dAngle < -180) dAngle += 360;
if(dAngle > maxDelta) dAngle = maxDelta;
else if(dAngle < -maxDelta) dAngle = -maxDelta;
angle += dAngle;
if(angle < 0.f) angle += 360.f;
else if(angle > 360.f) angle -= 360.f;
representation.setRotation(angle);
}
void GameObject::setAngle(float angle)
{
this->angle = angle;
representation.setRotation(angle);
}
void GameObject::update(sf::Time elapsedTime)
{
updatePosition(elapsedTime);
}