-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonster.cpp
137 lines (118 loc) · 3.83 KB
/
Monster.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
#include "stdafx.h"
#include "Monster.h"
#include "PublicMap.h"
#include "Playing.h"
#include <thread>
#include <mutex>
Monster::Monster(int level) : isSpawned(false), HP(100+5*level*level), speed(100*(1+level)), armor(level/5), lastPos(-1,-1), currentPos(-1,-1), monsterShape(10)
{
monsterShape.setFillColor(sf::Color::Red);
monsterShape.setOutlineColor(sf::Color::Blue);
monsterShape.setOutlineThickness(-armor);
monsterShape.setOrigin(monsterShape.getGlobalBounds().width/2,monsterShape.getGlobalBounds().height/2);
}
//does not check if it's a valid spawn, left to Playing class!
void Monster::Spawn(int row, int col)
{
monsterShape.setPosition(PublicMap::Instance()->GetLocation(row, col));
currentPos.x=col;
currentPos.y=row;
isSpawned=true;
isMoving=false;
moveTimer.restart();
}
void Monster::UnSpawn()
{
isSpawned=false;
}
//returns true if you've finished moving
bool Monster::Move(const sf::Vector2i& dir)
{
sf::Vector2f destination(PublicMap::Instance()->GetLocation(currentPos.y+dir.y,currentPos.x+dir.x));
//if you're within a pixel of where you're moving to, set the position there and finish moving.
if(abs(monsterShape.getPosition().x - destination.x) < 1.0f && abs(monsterShape.getPosition().y - destination.y) < 1.0f)
{
monsterShape.setPosition(destination);
lastPos=currentPos;
currentPos+=dir;
moveTimer.restart();
return true;
}
else
{
monsterShape.move(sf::Vector2f(dir)*(10*speed*moveTimer.restart().asSeconds()/MapObject::OBJECT_SIZE.x));
return false;
}
}
void Monster::Update()
{
if(isSpawned)
{
if(moveTimer.getElapsedTime().asSeconds()*speed > 60.0f && !isMoving)
{
moveTimer.restart();
isMoving=true;
}
if(isMoving && Move(FigureDirection()))
isMoving=false;
}
if(PublicMap::Instance()->GetType(currentPos.y, currentPos.x)==MapObject::ObjectType::exit)
{
UnSpawn();
++Playing::Instance()->monstersEscaped;
}
}
void Monster::Show(bool b)
{
}
void Monster::Attack(int dmg)
{
//fix this if things get too slow (unlikely)
attackQueue.push_back(dmg);
}
bool Monster::Kill()
{
//again, slow but probably not a big deal. alternative to this is to have preallocated queue, limiting number of attacks per update (probably not an issue either)
while(attackQueue.size())
{
HP-= (attackQueue.back()-armor);
attackQueue.pop_back();
if(HP<=0)
{
Die();
return true;
}
}
return false;
}
void Monster::Die()
{
//gives more points for higher levels, temporary until balance testing
Playing::Instance()->monstersKilled+=level;
isSpawned=false;
}
bool Monster::HandleEvents(const sf::Event& someEvent)
{
return false;
}
void Monster::Draw()
{
if(isSpawned)
Game::Instance()->mainWindow_.draw(monsterShape);
}
const sf::Vector2i Monster::FigureDirection()
{
if(((PublicMap::Instance()->GetType(currentPos.y+1,currentPos.x) == MapObject::ObjectType::path)
|| (PublicMap::Instance()->GetType(currentPos.y+1,currentPos.x) == MapObject::ObjectType::exit)) && sf::Vector2i(currentPos.x,currentPos.y+1)!=lastPos)
return sf::Vector2i(0,1);
if(((PublicMap::Instance()->GetType(currentPos.y-1,currentPos.x) == MapObject::ObjectType::path)
|| (PublicMap::Instance()->GetType(currentPos.y-1,currentPos.x) == MapObject::ObjectType::exit)) && sf::Vector2i(currentPos.x,currentPos.y-1)!=lastPos)
return sf::Vector2i(0,-1);
if(((PublicMap::Instance()->GetType(currentPos.y,currentPos.x-1) == MapObject::ObjectType::path)
|| (PublicMap::Instance()->GetType(currentPos.y,currentPos.x-1) == MapObject::ObjectType::exit)) && sf::Vector2i(currentPos.x-1,currentPos.y)!=lastPos)
return sf::Vector2i(-1,0);
if(((PublicMap::Instance()->GetType(currentPos.y,currentPos.x+1) == MapObject::ObjectType::path)
|| (PublicMap::Instance()->GetType(currentPos.y,currentPos.x+1) == MapObject::ObjectType::exit)) && sf::Vector2i(currentPos.x+1,currentPos.y)!=lastPos)
return sf::Vector2i(1,0);
return sf::Vector2i(0,0);
}