-
Notifications
You must be signed in to change notification settings - Fork 1
/
door.cpp
175 lines (144 loc) · 4.08 KB
/
door.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
#include "door.h"
Door:: Door()
: Entity()
{
openSpeed = 0.0;
closeSpeed = 0.0;
window = Window::getInstance();
resourceMgr = ResourceMgr:: getInstance();
opening = false;
resourceMgr->loadSound( "./data/audio/doorSound.wav", "door" );
}
Door:: Door( string name, Point uL, int width, int height,
double speed, Point openLoc)
: Entity( DOOR, name, 100, false, uL, width, height, SOLID, RECTANGLE, true, true )
{
this->openSpeed = speed;
this->closeSpeed = speed;
this->openLoc = openLoc;
this->closeLoc = uL;
if(uL.getX() == openLoc.getX())
{
if(uL.getY() > openLoc.getY())
dir = DOWN;
else
dir = UP;
}
else
{
if(uL.getX() > openLoc.getX())
dir = LEFT;
else
dir = RIGHT;
}
this->dir = dir;
window = Window::getInstance();
resourceMgr = ResourceMgr:: getInstance();
opening = false;
img = "./data/images/door.png";
resourceMgr->loadImage( img, img );
resourceMgr->loadSound( "./data/audio/doorSound.wav", "door" );
}
void Door:: open()
{
if ( !opening )
{
//Play sound effect
resourceMgr->playSound( "door" );
}
frameOpened = window->getFrameNumber();
opening = true;
}
void Door:: close()
{
opening = false;
}
void Door:: draw()
{
if ( dir == LEFT or dir == RIGHT )
resourceMgr->drawImage( img, upperLeft.getX(), upperRight.getX(), lowerLeft.getY(), upperRight.getY(), 0, getWidth()/ 128.0, 0, getHeight() / 32.0 );
else
resourceMgr->drawImage( img, upperRight.getX(), lowerLeft.getX(), upperLeft.getY(), lowerLeft.getY(), 0, getWidth() / 128.0, 0, getHeight() / 32.0 );
}
Door:: ~Door()
{
window->release();
}
void Door:: setPos( Point uL )
{
upperLeft.setX( uL.getX() );
upperLeft.setY( uL.getY() );
lowerLeft.setX( upperLeft.getX() );
lowerLeft.setY( upperLeft.getY() - height );
upperRight.setX( upperLeft.getX() + width );
upperRight.setY( upperLeft.getY () );
lowerRight.setX( upperRight.getX() );
lowerRight.setY( lowerLeft.getY() );
center.setX(upperLeft.getX() + width / 2.0);
center.setY(upperLeft.getY() - height / 2.0);
}
void Door:: onAlways()
{
if ( opening )
{
// Open to the right
if ( dir == RIGHT )
{
moveBy( openSpeed * window->timedMovement(), 0 );
if ( openLoc.getX() <= upperLeft.getX() )
setPos( openLoc );
}
// Open to the left
else if ( dir == LEFT )
{
moveBy( -openSpeed * window->timedMovement(), 0 );
if ( openLoc.getX() >= upperLeft.getX() )
setPos( openLoc );
}
// Open upward
else if ( dir == UP )
{
moveBy( 0, openSpeed * window->timedMovement() );
if ( openLoc.getY() <= upperLeft.getY() )
setPos( openLoc );
}
// Open downward
else if ( dir == DOWN )
{
moveBy( 0, -openSpeed * window->timedMovement() );
if ( openLoc.getY() >= upperLeft.getY() )
setPos( openLoc );
}
}
else
{
// Open to the right
if ( dir == RIGHT )
{
moveBy( -openSpeed * window->timedMovement(), 0 );
if ( closeLoc.getX() >= upperLeft.getX() )
setPos( closeLoc );
}
// Open to the left
else if ( dir == LEFT )
{
moveBy( openSpeed * window->timedMovement(), 0 );
if ( closeLoc.getX() <= upperLeft.getX() )
setPos( closeLoc);
}
// Open upward
else if ( dir == UP )
{
moveBy( 0, -openSpeed * window->timedMovement() );
if ( closeLoc.getY() >= upperLeft.getY() )
setPos( closeLoc );
}
// Open downward
else if ( dir == DOWN )
{
moveBy( 0, openSpeed * window->timedMovement() );
if ( closeLoc.getY() <= upperLeft.getY() )
setPos( closeLoc );
}
}
}