-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtsmovable.cpp
105 lines (79 loc) · 1.96 KB
/
rtsmovable.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// rtsmovable.cpp
#include <SFML/Graphics.hpp>
#include <math.h> // For use of Atan2()
#include <iostream>
#include "animationhelper.h"
#include "rtsmovable.h"
#define PRECISION 4
#define FRAMERATE 15
#define PI 3.14
#define des AnimationHelper::spriteDesc
#define dir AnimationHelper::spriteDir
#define col AnimationHelper::spriteColor
using namespace sf;
RTSMovable::RTSMovable(RenderWindow *window) : RTSDrawable (window){
this->window = window;
x = 0;
y = 0;
walkSpeed = 3;
runSpeed = 6;
targetX = 0;
targetY = 0;
direction = 0;
frame = false;
atTarget = false;
running = false;
lastFrame = des::Stand;
lastDir = dir::Down;
teamColor = col::Blue;
}
void RTSMovable::draw(int frameCount){
if(frameCount%FRAMERATE != 0){
window->draw(* AnimationHelper::Instance()->getSoldier(teamColor, lastFrame, lastDir, x, y));
return;
}
move();
}
void RTSMovable::move(){
atTarget = (x < targetX+PRECISION && x > targetX-PRECISION && y < targetY+PRECISION && y > targetY-PRECISION);
if(! atTarget){
double deltaX = targetX-x;
double deltaY = targetY-y;
direction = atan2(deltaY, deltaX);
double speed;
if(running){
speed = runSpeed;
} else {
speed = walkSpeed;
}
x+=speed*cos(direction);
y+=speed*sin(direction);
double dirDeg = direction * 180 / PI;
int dirDegInt = int(dirDeg)%360;
if(dirDegInt < 0){
dirDegInt+=360;
}
if(dirDegInt <= 45 || dirDegInt >= 315){
lastDir = dir::Right;
} else if (dirDegInt > 45 && dirDegInt <= 135){
lastDir = dir::Down;
} else if (dirDegInt > 135 && dirDegInt <= 225){
lastDir = dir::Left;
} else {
lastDir = dir::Up;
}
if(frame){
lastFrame = des::Walk1;
} else {
lastFrame = des::Walk2;
}
frame = !frame;
} else {
lastFrame = des::Stand;
}
window->draw(* AnimationHelper::Instance()->getSoldier(teamColor, lastFrame, lastDir, x, y));
}
void RTSMovable::setTarget(double targetX, double targetY){
this->targetX = targetX;
this->targetY = targetY;
}