-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.cpp
111 lines (100 loc) · 2.94 KB
/
Block.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
// game engine includes
#include "GraphicsManager.h"
#include "WorldManager.h"
#include "ResourceManager.h"
#include "LogManager.h"
#include "GameManager.h"
#include "MapManager.h"
// IHOP includes
#include "Block.h"
#include "EventCollision.h"
#include "Hero.h"
// constructor
Block::Block(df::Position pos){
// Dragonfly managers needed for this method.
df::LogManager &log_manager = df::LogManager::getInstance();
df::ResourceManager &resource_manager = df::ResourceManager::getInstance();
df::WorldManager &world_manager = df::WorldManager::getInstance();
// Setup "block" sprite.
df::Sprite *p_temp_sprite = resource_manager.getSprite("block");
if (!p_temp_sprite) {
log_manager.writeLog("Block::Block(): Warning! Sprite '%s' not found",
"block");
}
else {
setSprite(p_temp_sprite);
setSpriteSlowdown(0);
}
// set attributes
setType(BLOCK_TYPE);
setSolidness(df::SOFT);
setAltitude(2);
setPosition(pos);
// set slowdown
move_slowdown = 0;
move_countdown = move_slowdown;
}
// handle events
int Block::eventHandler(df::Event *p_e){
// if collision event
if (p_e->getType() == DF_COLLISION_EVENT){
eventCollision(static_cast <df::EventCollision *> (p_e));
}
return 0;
}
// if collision event
int Block::eventCollision(df::EventCollision *p_e){
// if collider is hero then start falling down screen
if (p_e->getObject1()->getType().compare(HERO_TYPE) == 0 ||
p_e->getObject2()->getType().compare(HERO_TYPE) == 0){
setYVelocity(FALL_VELOCITY);
return 1;
}
// if collided with shelf then stop falling
else if (p_e->getObject1()->getType().compare("Shelf") == 0 ||
p_e->getObject2()->getType().compare("Shelf") == 0){
setYVelocity(0.0);
return 1;
}
// if collided with block then go to block collision function
else if (p_e->getObject1()->getType().compare(BLOCK_TYPE) == 0 &&
p_e->getObject2()->getType().compare(BLOCK_TYPE) == 0){
// send to block collsion based on which object this isnt
if (p_e->getObject1() == this){
return blockCollision(static_cast <Block *>(p_e->getObject2()));
}
else{
return blockCollision(static_cast <Block *>(p_e->getObject1()));
}
}
// if collided with bottom shelf then stop and change to at bottom shelf
else if (p_e->getObject1()->getType().compare("BottomShelf") == 0 ||
p_e->getObject2()->getType().compare("BottomShelf") == 0){
MapManager &mm = MapManager::getInstance();
mm.addStackToCounter();
setYVelocity(0.0);
setIsAtBottomShelf();
return 1;
}
return 0;
}
// if collision with block
int Block::blockCollision(Block *p_e){
// if is falling then stop falling
if (getYVelocity() > 0.0){
setYVelocity(0.0);
}
// if it is stationary then start falling
else {//if(!isAtBottomShelf){
setYVelocity(FALL_VELOCITY);
}
return 1;
}
// set block to is at bottom shelf
void Block::setIsAtBottomShelf(bool new_isAtBottomShelf){
isAtBottomShelf = new_isAtBottomShelf;
}
// get if the block is at the bottom shelf
bool Block::blockIsAtBottomShelf(){
return isAtBottomShelf;
}