-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreeps.hx
198 lines (173 loc) · 4.91 KB
/
Creeps.hx
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
import Settings;
import Interface;
class Gobo extends MovieClip {
public function new() {
super();
}
}
class RoutePoint {
public var x:Float;
public var y:Float;
public var n:Int;
public function new(x:Float,y:Float,n:Int=0) {
this.x= x;
this.y= y;
this.n= n;
}
}
class Route {
var route:Array<RoutePoint>;
public function new() {
route= new Array();
}
public function add_point(p:RoutePoint) {
// todo: handle duplicate n (replace old routepoint?)
route.push(p);
route.sort(sort_me);
}
private function sort_me(a:RoutePoint,b:RoutePoint) {
if(a.n==b.n) { return 0; }
else if(a.n>b.n) { return 1; }
else { return -1; }
}
public function next(p:RoutePoint) {
if(p.n==route.length) {
// this means we are at the end of the route: return to the start
return route[0];
} else {
return route[p.n];
}
}
}
class Creep extends Sprite {
public var hp:Int;
var maxhp:Int;
public var speed:Int;
var goal:RoutePoint;
var route:Route;
var frames:Int;
var animation_frames:Int;
public var dead:Bool;
public var value:Int;
var info:CreepInfo;
var mc:MovieClip;
var healthbar:Sprite;
public function new(r:Route,mhp=50,val=1,s=1) {
super();
maxhp= mhp;
hp= maxhp;
speed= s;
value= val;
frames= 0;
animation_frames= 0;
route= r;
dead= false;
y= 0; // top of screen
x= 5.5*Settings.tilesize;
goal= new RoutePoint(x,2.5*Settings.tilesize);
mc= new Gobo();
healthbar= new Sprite();
mouseChildren= false; // the mouse_out event can't work without this
this.addChild(mc);
mc.stop();
this.addChild(healthbar);
//graphics.lineStyle(3,0xff0000);
//graphics.beginFill(0x0000ff);
//graphics.drawCircle(0,0,10);
//graphics.endFill();
mc.x= -Settings.tilesize/2 + 1;
mc.y= -Settings.tilesize/2 + 1;
draw_healthbar();
this.addEventListener(flash.events.Event.ENTER_FRAME,enter_frame);
this.addEventListener(MouseEvent.MOUSE_OVER,mouse_over);
this.addEventListener(MouseEvent.MOUSE_OUT,mouse_out);
flash.Lib.current.addChild(this);
}
public function delete() {
flash.Lib.current.removeChild(this);
}
function draw_healthbar() {
var ts= Settings.tilesize;
var percent= hp/maxhp;
healthbar.graphics.lineStyle(1,0x000000);
healthbar.graphics.beginFill(0xffffff);
healthbar.graphics.drawRect(-ts/3,-ts/12,2*ts/3,ts/6);
healthbar.graphics.lineStyle();
healthbar.graphics.beginFill(0x00ff00);
healthbar.graphics.drawRect(-ts/3,-ts/12+1,(2*ts/3)*percent,ts/6-2);
healthbar.graphics.endFill();
}
public function fire(dmg:Int) {
hp -= dmg;
if (hp <= 0) {
this.dead= true;
} else {
draw_healthbar();
}
}
function enter_frame(e:flash.events.Event) {
frames+= 1;
if(frames==speed) {
move_to_goal();
frames= 0;
animation_frames+= 1;
if(animation_frames==13*speed) {
// steps to next frame on the timeline and loops back round to frame 1 when it hits the end
mc.gotoAndStop(mc.currentFrame==mc.totalFrames ? 1 : mc.currentFrame+1);
animation_frames= 0;
}
}
}
function mouse_over(e:MouseEvent) {
info= new CreepInfo(this);
}
function mouse_out(e:MouseEvent) {
info.delete();
}
function move_to_goal() {
var csf= Settings.creep_speedfactor;
if(x>goal.x) x-=csf;
else if(x<goal.x) x+=csf;
if(y>goal.y) y-=csf;
else if(y<goal.y) y+=csf;
if(is_at_goal()) {
goal= route.next(goal);
}
}
public function set_goal(x:Float, y:Float) {
goal.x= x;
goal.y= y;
}
public function is_at_goal() {
var csf= Settings.creep_speedfactor;
if(Math.abs(x-goal.x)<csf && Math.abs(y-goal.y)<csf) { return true; }
else { return false; }
}
}
class CreepInfo {
var infobox:Txt;
var creep:Creep;
public function new(c:Creep) {
creep= c;
var ts= Settings.tilesize;
var x= ts*6.5;
var y_base= ts*9;
var s= Settings.fontsize_small;
infobox= new Txt(x,y_base,"Super Evil Thing!",s);
infobox.addline("Health: "+Std.string(creep.hp));
infobox.addline("Speed: "+Std.string(creep.speed));
infobox.addline("Gold: "+Std.string(creep.value));
creep.addEventListener(flash.events.Event.ENTER_FRAME,enter_frame);
}
function enter_frame(e:Event) {
infobox.update("Health: "+Std.string(creep.hp),1);
}
public function delete() {
creep.removeEventListener(flash.events.Event.ENTER_FRAME,enter_frame);
infobox.delete();
}
}