-
Notifications
You must be signed in to change notification settings - Fork 0
/
securitycam.cpp
151 lines (117 loc) · 3.93 KB
/
securitycam.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
#include "securitycam.h"
SecurityCamera::SecurityCamera(int x, int y, int width, int height, int objID, Player* _thePlayer) : super(x, y, width, height, objID)
{
//The security camera object is actually just its view-cone!
// The camera itself is just on the map.
visionConeSide = new Animation(1, 1);
visionConeSide->Frames[0] = 95*2;
visionConeUp = new Animation(1, 1);
visionConeUp->Frames[0] = 95*2;
visionConeDown = new Animation(1, 1);
visionConeDown->Frames[0] = 25*2;
thePlayer = _thePlayer;
alertTime = 0;
//Security Camera VisionCones are 64x32: 1x3 on the GBA's Object Size Map
Shape = 1;
Size = 3;
currentAnimation = visionConeSide;
// NOTE: This initializes Alpha Blending on the GBA:
REG_BLDCNT = BLD_BUILD(
BLD_OBJ, // Top Layers
BLD_BG1 | BLD_BG2 | BLD_BG3, // Bottom Layers
BLD_STD); // Standard blending mode
BFN_SET(REG_BLDCNT, BLD_STD, BLD_MODE);
// 50/50 Blend
REG_BLDALPHA = BLDA_BUILD(15,7);
Draw();
}
void SecurityCamera::Draw()
{
// I dont use super::Draw() here because I need to override the main object drawing bit down there.
//Convert world coordinates to screen coordinates - object needs to know scrolling offset.
ScreenPositionX = (int)Position.X - ScrollX;
ScreenPositionY = (int)Position.Y - ScrollY;
// Objects are given an ID on spawning - all objects are spawned through an object creator.
// The object creator won't allow an ID greater of 128.
if(facingLeft == true)
{
SetObject(objectID, ATTR0_SHAPE(Shape) | ATTR0_8BPP | ATTR0_REG | ATTR0_BLEND | ATTR0_Y(ScreenPositionY),
ATTR1_SIZE(Size) | ATTR1_X(ScreenPositionX) | ATTR1_HFLIP,
ATTR2_ID(currentAnimation->Frames[currentAnimation->CurrentFrame]));
}
else
{
SetObject(objectID, ATTR0_SHAPE(Shape) | ATTR0_8BPP | ATTR0_REG | ATTR0_BLEND | ATTR0_Y(ScreenPositionY),
ATTR1_SIZE(Size) | ATTR1_X(ScreenPositionX),
ATTR2_ID(currentAnimation->Frames[currentAnimation->CurrentFrame]));
}
if(direction == 3 || direction == 2)
{
//Facing down/up, size and shape swapped.
SetObject(objectID, ATTR0_SHAPE(Size) | ATTR0_8BPP | ATTR0_REG | ATTR0_BLEND | ATTR0_Y(ScreenPositionY),
ATTR1_SIZE(Shape) | ATTR1_X(ScreenPositionX) | ATTR1_HFLIP,
ATTR2_ID(currentAnimation->Frames[currentAnimation->CurrentFrame]));
}
// Don't go off-screen!
if(ScreenPositionX < -63 || ScreenPositionX > 272 || ScreenPositionY < 0 || ScreenPositionY > 160)
{
SetObject(objectID, ATTR0_SHAPE(Shape) | ATTR0_8BPP | ATTR0_HIDE | ATTR0_REG | ATTR0_Y(32),
ATTR1_SIZE(Size) | ATTR1_X(32),
ATTR2_ID(currentAnimation->Frames[currentAnimation->CurrentFrame]));
}
}
void SecurityCamera::Update()
{
CheckForPlayer();
super::Update();
}
void SecurityCamera::CheckForPlayer()
{
if(alertTime > 0)
{
alertTime--;
}
if(alertTime == 0)
{
AlertState = 0;
}
// If we're really far from the player, don't bother.
if(fabs(Position.X - thePlayer->Position.X) > (double)64 || fabs(Position.Y - thePlayer->Position.Y) > (double)16)
{
return;
}
// Checking a few tiles in a straight line ahead
double HalfHeight = Height/(double)2;
for(int i = 0; i < 8; i++)
{
double checkPoint = (double)(i*8);
if(facingLeft)
{
checkPoint = -checkPoint;
}
// If we've hit solid ground or full shadow, we should do nothing.
if(thePlayer->StealthState == 2)
{
break;
}
else
{
//Check for player at this position.
if(Position.X + checkPoint > thePlayer->Position.X && Position.Y + HalfHeight > thePlayer->Position.Y && Position.X + checkPoint < thePlayer->Position.X + thePlayer->Width && Position.Y + HalfHeight < thePlayer->Position.Y + thePlayer->Height)
{
// In half-shadow, we simply become suspicious.
if(thePlayer->StealthState == 1 || thePlayer->StealthState == 3)
{
AlertState = 1;
alertTime = 120;
}
else
{
AlertState = 2;
alertTime = 240;
}
return;
}
}
}
}