-
Notifications
You must be signed in to change notification settings - Fork 0
/
foe.m
169 lines (130 loc) · 4.57 KB
/
foe.m
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
% Authors: Alejandro R. Mosteo, Danilo Tardioli, Eduardo Montijano
% Copyright 2018-9999 Monmostar
% Licensed under GPLv3 https://www.gnu.org/licenses/gpl-3.0.en.html
%
classdef foe < i_body & i_drawable & i_killable
% TODO: split bomb & missile into different classes
properties(Constant)
vy_max = 4;
default_accel = 9.8;
dying_len = 2;
missile_min_spd = 12;
missile_max_spd = 18;
BOMB = 1
MISL = 2
size = 3;
bombY = ([0 1 2 3 4 4 3 2 1 0]' - 1.5)*foe.size/2;
bombX = [0 1 1 0.2 1 -1 -0.2 -1 -1 0]'*foe.size/2;
mslX = [0 1 2 1 -2 -4 -4 -2 0];
mslY = [-1 -1 0 1 1 2 -2 -1 -1];
marker = {'rx', 'kv'}
end
properties
% x, y % Those are inherited from i_body
vx, vy
ay
kind
alive = true
dying = 0
spriteX
spriteY
T
h_foe; % Drawers
h_fill;
end
methods(Access=public)
function this = foe(period, kind, difficulty)
this.T = period;
if nargin < 2
kind = mod(tic, 2) + 1;
end
if nargin < 3
difficulty = 0.5;
end
this.ay = this.default_accel + difficulty*3;
this.kind = kind;
this.id = rand;
this.h_foe = drawer();
this.h_fill = drawer();
switch kind
case this.BOMB
fprintf('Incoming bomb!\n')
this.x = (rand*saudefense.W-saudefense.W/2)*(1 - saudefense.OS);
this.y = saudefense.H + 2.5;
this.vy = -1;
this.vx = 0;
this.spriteX = this.bombX;
this.spriteY = this.bombY;
case this.MISL
fprintf('Incoming missile!\n')
this.x = round(rand)*saudefense.W - saudefense.W/2;
this.y = rand*saudefense.H/2*(1-difficulty*0.9) + saudefense.H/2;
% Go to the opposite side
tx = rand*saudefense.W/2*(-sign(this.x));
h = sqrt((tx - this.x)^2 + this.y^2);
spd = this.missile_min_spd + rand*(this.missile_max_spd - this.missile_min_spd);
cs = (tx - this.x)/h;
sn = this.y/h;
this.vx = cs * spd;
this.vy = sn * spd;
% Drawing override
cs = 0;
sn = 1;
rot = [cs -sn; sn cs];
sprite = [this.mslX' this.mslY']*rot;
this.spriteX = sprite(:,1)';
this.spriteY = sprite(:,2)';
end
end
function hit = check_hit(this, fx, ~, ~)
% Firing angle not used right now (vertical fire assumed)
hit = this.alive && abs(this.x - fx)<=this.size/2;
end
function die(this)
fprintf('Destroyed!\n');
this.alive = false;
this.dying = this.dying_len;
this.vy = this.vy / 2;
this.vx = this.vx / 2;
% fix Y offset
this.y = this.y + this.size*1/2;
end
function draw(this, fig, scale)
if this.alive
this.h_fill.fill(fig, (this.spriteX + this.x)*scale, ...
(this.spriteY + this.y)*scale, ...
'w');
this.h_foe.plot(fig, ...
(this.spriteX + this.x)*scale, ...
(this.spriteY + this.y)*scale, ...
'Color', [0 0 0]);
this.h_fill.show;
else
this.h_fill.show(false);
this.h_foe.plot(fig, this.x*scale, this.y*scale, ...
'Marker', 'x', 'Color', [1 0 0]);
end
end
function points = score(this)
points = this.kind;
end
function done = update(this, ~)
this.x = this.x + this.vx*this.T;
if abs(this.x) > saudefense.W/2
this.alive = false;
end
if this.kind == this.BOMB || this.dying > 0
this.vy = this.vy + this.ay*this.T;
end
this.y = this.y - this.vy*this.T;
if this.y < 0
this.y = 0;
end
if this.dying > 0
this.dying = this.dying - this.T;
end
done = ~this.alive && this.dying<=0;
done = done || this.y == 0;
end
end
end