-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAvatar.dart
141 lines (136 loc) · 4.56 KB
/
Avatar.dart
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
part of BigIsland;
// Big Island video game source code file
// Copyright (C) 2012 Severin Ibarluzea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Big Island video game source code file
// Copyright (C) 2012 Severin Ibarluzea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
class Avatar extends GameObject {
int currentAnimation = Animation.WALK;
int currentOrientation = 2;
//0 - forward,1 - left, 2 - down, 3 - right
num currentFrame = 0;
Vec2 velocity;
bool _attacking = false;
Vec2 attackDirection;
num damage=25,armor=1;
int attackType = 0;
int attackTime = 12;
int currentAttackTime = 0;
int timeSinceHealthChange = 0;
num health = 100;
bool alive = true;
num speed = 1;
bool speaking = false;
num sayTime = 0;
String speech = "";
num attackRadius = 32;
void set attacking(bool b){
_attacking = b;
currentAnimation = b ? Animation.AnimationTypes[attackType] : Animation.WALK;
}
bool get attacking => _attacking;
Animation animation;
Animation weaponAnimation;
Avatar(properties):super(properties,0,0){
velocity = new Vec2(0,0);
//this.tags.add("avatar");
//addTag(this,"avatar");
}
void hurt(num damage,Vec2 direction){
fireTagEvent("hit");
health -= damage * armor;
if (damage > 10){
this.velocity.add(direction.multiplyScalar(damage/2));
if (this.velocity.lengthSq() > 8*8){
this.velocity.normalize().multiplyScalar(8);
}
}
timeSinceHealthChange = 120;
if (alive && health <= 0){
currentFrame = 0;
currentAnimation = Animation.DEATH;
currentOrientation = 0;
tagEvents["corpse"]["init"](this);
this.tags.add("corpse");
addTag(this,"corpse");
fireTagEvent("die");
tagEvents["corpse"]["init"](this);
alive = false;
for (int i = this.tags.length-1;i>=0;i--){
String tag = this.tags[i];
if (removalOnDeath.containsKey(tag) && removalOnDeath[tag]){
this.tags.removeRange(i, 1);
for (int u = tagMap[tag].length-1;u>=0;u--){
if (tagMap[tag][u] == this){
tagMap[tag].removeRange(u,1);
break;
}
}
}
}
}
}
void say(String text,[int time = 300]){
speaking = true;
speech = text;
sayTime = time;
}
void render(html.CanvasRenderingContext2D c){
c.save();
c.translate(x,y);
debugRender(c);
animation.render(c,currentAnimation,currentOrientation,(currentFrame/5).toInt());
if (weaponAnimation!=null){
weaponAnimation.render(c,currentAnimation,currentOrientation,(currentFrame/5).toInt());
}
//Draw Health Bar
//OPTIMIZE
if (timeSinceHealthChange>=0){
timeSinceHealthChange--;
c.globalAlpha = timeSinceHealthChange/120;
final num healthBarSize = 50.0;
c.fillStyle = "#f00";
c.fillRect(-healthBarSize/2, -25, healthBarSize, 5);
c.fillStyle = "#0f0";
c.fillRect(-healthBarSize/2, -25, health / 100.0 * healthBarSize, 5);
}
if (speaking){
c.globalAlpha *= .75;
c.font = "14px Arial";
var bubbleWidth = c.measureText(speech).width + 20;
c.strokeStyle = "#000";
c.fillStyle = "#fff";
c.fillRect(8, -12 - 30, bubbleWidth, 30);
c.strokeRect(8, -12 - 30, bubbleWidth, 30);
c.fillStyle = "#000";
c.fillText(speech, bubbleWidth/2 + 6, -22);
c.globalAlpha /= .75;
}
c.restore();
}
}