-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grenade.cpp
82 lines (70 loc) · 1.96 KB
/
Grenade.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
#include "Grenade.h"
#include "Character.h"
#include "Map.h"
#include <iostream>
#include <cmath>
#include <allegro5/allegro_primitives.h>
Grenade::Grenade(int xPos, int yPos, WeaponProperties weaponProperties, int direction, Character * watcher):
Weapon(xPos, yPos, weaponProperties, watcher),velocity(6),direction(direction),timer(40){}
void Grenade::Update()
{
bool changeDir = false;
if (destroyThis)
{
delete this;
return;
}
if (std::abs(xPos-xPosOrig) > GetProperties().GetRange()|| std::abs(yPos-yPosOrig) > GetProperties().GetRange())
{
delete this;
return;
}
int **grid = Map::GetInstance()->GetGrid();
if (grid[GetX()/BOX_PIXEL_WIDTH][GetY()/BOX_PIXEL_WIDTH] == 1)
{
direction = (direction == 1000 || direction == 10) ? (direction / 10) : (direction * 10); // switch direction 1000-100 10-1
changeDir = true;
}
switch (direction)
{
case 1000: // x+
xPos += velocity;
break;
case 100: // x-
xPos -= velocity;
break;
case 10: // y+ (this is actually down)
yPos += velocity;
break;
case 1: // y- (this is actually up)
yPos -= velocity;
break;
default:
std::cerr << "Invalid grenade direction set\n";
}
if (changeDir)
velocity -= 2;
timer--;
if (timer == 0)
{
Detonate();
}
}
void Grenade::Detonate()
{
destroyThis = true;
}
void Grenade::Action( Character * character)
{
if (character->Visit(*this))
{
destroyThis = true;
}
}
void Grenade::Draw(ALLEGRO_BITMAP *buffer, int midX, int midY)
{
al_set_target_bitmap(buffer);
al_draw_filled_circle (midX+GetX(), midY+GetY(), 2, al_map_rgb (0,255,0));
if (destroyThis)
al_draw_filled_circle (midX+GetX(), midY+GetY(), GetProperties().GetRadius(), al_map_rgb (255,0,0));
}