-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathelaser.cpp
87 lines (68 loc) · 1.69 KB
/
elaser.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
#include "elaser.h"
bool ELaser::finished_init = false;
ZSDL_Surface ELaser::laser_bullet[2];
ELaser::ELaser(ZTime *ztime_, int start_x, int start_y, int dest_x, int dest_y) : ZEffect(ztime_)
{
double &the_time = ztime->ztime;
const double bullet_speed = 300;
if(!finished_init)
{
killme = true;
return;
}
//which one to use this time?
bullet_i = rand() % 2;
//set the dx / dy
float mag;
dx = dest_x - start_x;
dy = dest_y - start_y;
mag = sqrt((dx * dx) + (dy * dy));
init_time = the_time;
final_time = init_time + (mag / bullet_speed);
dx /= (final_time - init_time);
dy /= (final_time - init_time);
//find angle
angle = 359 - AngleFromLoc(dx, dy);
//bullet_img = laser_bullet[bullet_i].GetImage(angle);
bullet_img = laser_bullet[bullet_i];
bullet_img.SetAngle(angle);
if(!bullet_img.GetBaseSurface())
{
killme = true;
return;
}
//shift the x and y's
x = sx = start_x - (bullet_img.GetBaseSurface()->w >> 1);
y = sy = start_y - (bullet_img.GetBaseSurface()->h >> 1);
}
void ELaser::Init()
{
int i;
char filename_c[500];
for(i=0;i<2;i++)
{
sprintf(filename_c, "assets/units/robots/laser/bullet_n%02d.png", i);
laser_bullet[i].LoadBaseImage(filename_c);
}
finished_init = true;
}
void ELaser::Process()
{
double &the_time = ztime->ztime;
//check
if(the_time >= final_time) killme = true;
else
{
//move
x = sx + (dx * (the_time - init_time));
y = sy + (dy * (the_time - init_time));
}
}
void ELaser::DoRender(ZMap &zmap, SDL_Surface *dest)
{
SDL_Rect from_rect, to_rect;
if(killme) return;
zmap.RenderZSurface(&bullet_img, x, y);
//if(zmap.GetBlitInfo(bullet_img, x, y, from_rect, to_rect))
// SDL_BlitSurface( bullet_img, &from_rect, dest, &to_rect);
}