This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion.cpp
186 lines (142 loc) · 3.1 KB
/
motion.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "motion.h"
#define PI 3.14159265
Motion::Motion()
{
x=0;
}
float Motion::getX() const
{
return x;
}
void Motion::setX(float value)
{
x = value;
}
void Motion::setReverseDirection(bool value)
{
if(value){
direction=-1;
}
else{
direction=1;
}
}
bool Motion::reverseDirection() const
{
bool reverseDirection = false;
if(direction==-1){
reverseDirection=true;
}
return reverseDirection;
}
LinearMotion::LinearMotion(float angle)
{
this->angle=(angle*PI/180.0);
}
sf::Vector2f LinearMotion::getNext(float speed)
{
sf::Vector2f newPos;
newPos.x= speed*sin(angle);
newPos.y= speed*cos(angle)*direction;
x+=speed;
return newPos;
}
SinMotion::SinMotion(int scale)
{
this->scale=scale;
x=0.01;
}
sf::Vector2f SinMotion::getNext(float speed)
{
sf::Vector2f newPos;
double sin1 =sin((x+1)/15.0), sin2 = sin(x/15.0);
newPos.x = (sin1- sin2)*scale;
newPos.y = 2*direction;
x+=1;
return newPos;
}
SimpleMotion::SimpleMotion()
{
}
sf::Vector2f SimpleMotion::getNext(float speed)
{
x+=speed;
sf::Vector2f newPos;
newPos.x= 0;
newPos.y= speed*direction;
return newPos;
}
FollowerMotion::FollowerMotion(sf::Sprite *owner,sf::Sprite *target)
{
setOwner(owner);
setTarget(target);
}
sf::Vector2f FollowerMotion::getNext(float speed)
{
sf::Vector2f next(0,speed*direction);
//std::cout<< target->getGlobalBounds().width <<std::endl;
if(owner->getGlobalBounds().height/2+owner->getPosition().y < target->getGlobalBounds().height/2+target->getPosition().y != reverseDirection()
&& speed < fabs( (owner->getPosition().x+owner->getGlobalBounds().width/2) - (target->getPosition().x+target->getGlobalBounds().width/2)) ){
if(owner->getGlobalBounds().width/2+owner->getPosition().x < target->getGlobalBounds().width/2+target->getPosition().x){
next.x=speed;
}
else {
next.x=-speed;
}
}
return next;
}
sf::Sprite *FollowerMotion::getTarget() const
{
return target;
}
void FollowerMotion::setTarget(sf::Sprite *value)
{
target = value;
}
sf::Sprite *FollowerMotion::getOwner() const
{
return owner;
}
void FollowerMotion::setOwner(sf::Sprite *value)
{
owner = value;
}
namespace MotionFactory {
Motion *createSimpleMotion()
{
return new SimpleMotion();
}
Motion *createLinearMotion(float angle)
{
return new LinearMotion(angle);
}
Motion *createSinMotion(int scale)
{
return new SinMotion(scale);
}
Motion *createFollowerMotion(sf::Sprite *owner, sf::Sprite *target)
{
return new FollowerMotion(owner, target);
}
Motion *createBossMotion(int scale, int yMovement)
{
return new BossMotion(scale, yMovement);
}
}
BossMotion::BossMotion(int scale, int yMovement)
{
this->scale=scale;
this->yMovement=yMovement;
}
sf::Vector2f BossMotion::getNext(float speed)
{
sf::Vector2f newPos(0,0);
float sin1 =sin((x+1)/15.0), sin2 = sin(x/15.0);
newPos.x = (sin1- sin2)*scale;
if(x<yMovement){
newPos.y = speed*direction;
}
x+=1;
return newPos;
}