-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartSprite.cpp
153 lines (123 loc) · 3.39 KB
/
smartSprite.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
#include "smartSprite.h"
#include "renderContext.h"
#include "multisprite.h"
#include "player.h"
#include <cmath>
#include "gameData.h"
#include <random>
#include <functional>
#include "vector2f.h"
#include "explodingSprite.h"
SmartSprite::SmartSprite(const std::string& name, const Vector2f& pos, int w, int h):
MultiSprite(name,Gamedata::getInstance().getXmlInt("world/width")),
currentMode(NORMAL),
playerPosition(pos),
playerWidth(w),
playerHeight(h),
//player(p),//safeDistance = 300
safeDistance(Gamedata::getInstance().getXmlInt("Crab/safeDistance")),
explosion(nullptr)
{
int num = rand()%2 ? 1 : -1;
if( num == -1){
setVelocityX( -fabs( getVelocityX() ) );
}
else
setVelocityX( fabs( getVelocityX() ) );
}
void SmartSprite::setPlayerPosition(const Vector2f& pos){
playerPosition = pos;
}
void SmartSprite::goLeft(){
setVelocityX(-abs(getVelocityX()));
}
void SmartSprite::goRight(){
setVelocityX(fabs(getVelocityX()));
}
SmartSprite::SmartSprite(const SmartSprite& s):
MultiSprite(s),
currentMode(s.currentMode),
playerPosition(s.playerPosition),
playerWidth(s.playerWidth),
playerHeight(s.playerHeight),
//player(s.player),
safeDistance(s.safeDistance),
explosion(s.explosion)
{}
SmartSprite::~SmartSprite(){
delete explosion;
}
SmartSprite& SmartSprite::operator=(const SmartSprite& s) {
MultiSprite::operator=(s);
currentMode = (s.currentMode);
playerPosition = (s.playerPosition);
playerWidth = (s.playerWidth);
playerHeight = (s.playerHeight);
safeDistance = (s.safeDistance);
explosion = (s.explosion);
return *this;
}
void SmartSprite::explode() {
if ( !explosion ) {
Sprite sprite(getName(), getPosition(), getVelocity(), images[currentFrame]);
explosion = new ExplodingSprite(sprite);
printf("smartsprite explode\n");
}
}
void SmartSprite::draw() const{
if ( explosion ){
explosion->draw();
}
else {
images[currentFrame]->draw(getX(), getY(), getScale());
}
}
bool SmartSprite::isExploding(){
if(explosion) return true;
else return false;
}
void SmartSprite::update(Uint32 ticks){
if ( explosion ) {
//printf("we explodinggg\n");
explosion->update(ticks);
if ( explosion->chunkCount() == 0 ) {
delete explosion;
explosion = NULL;
}
return;
}
MultiSprite::update(ticks);
//Vector2f pos = player.getPosition();
float spriteX = getX() + getImage()->getWidth()/3;
float playerX = playerPosition[0] + playerWidth/3;
float spriteY = getY() + getImage()->getHeight()/3;
float playerY = playerPosition[1] + playerHeight/3;
float distanceToPlayer = hypot((spriteX-playerX),(spriteY-playerY));
//std::cout << distanceToPlayer << std::endl;
if(currentMode == NORMAL){
if(safeDistance > distanceToPlayer){
//if the crab's safe distance is greater than the distance to player
//then it needs to evade
//std::cout <<"normal to EVADE" << std::endl;
currentMode = EVADE;
}
}
else if(currentMode == EVADE){
if(safeDistance < distanceToPlayer){
//if the crab's safe distance this less than the distance to the distance to
//the player, revert back to normal
//td::cout <<"EVADE to normal " << std::endl;
currentMode = NORMAL;
}
else{
if(spriteX < playerX){
//std::cout <<"im evadinggggg - go left" << std::endl;
goLeft();
}
else if(spriteX > playerX){
//std::cout <<"im evadinggggg - go r" << std::endl;
goRight();
}
}
}
}