-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcharacter.ts
197 lines (177 loc) · 6.33 KB
/
character.ts
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
197
namespace kodu {
export type CharacterState = {
x: number;
y: number;
id: string;
bdefn: string;
};
export enum ImpulseType {
Exclusive, // Doesn't blend with any other movement.
Ambient, // Can be blended with other movement.
Default // Default movement if no other impulses present.
}
interface Impulse {
type: ImpulseType;
direction: Vec2;
magnitude: number;
}
export class Character extends ActorComponent {
kel: Kelpie;
feeling: Kelpie;
body: Body;
bdefn: BrainDefn;
prog: Program;
destroyed: boolean;
impulseQueue: Impulse[];
bumps: Character[];
public get x() { return this.kel.x; }
public set x(v: number) { this.kel.x = v; }
public get y() { return this.kel.y; }
public set y(v: number) { this.kel.y = v; }
public get pos(): Vec2 { return mkVec2(this.x, this.y); }
public set pos(v: Vec2) { this.x = v.x; this.y = v.y; }
constructor(stage: Stage, x: number, y: number, public defn: CharacterDefn, bdefn: BrainDefn) {
super(stage, "character");
let icon = icons.get(defn.id);
this.kel = new Kelpie(icon);
this.kel.x = x;
this.kel.y = y;
this.kel.z = 0;
this.kel.data["kind"] = "character";
this.kel.data["component"] = this;
this.kel.onUpdate = (dt) => this.kelUpdate(dt);
this.bdefn = bdefn.clone();
this.destroyed = false;
this.impulseQueue = [];
this.bumps = [];
const physics = this.stage.get<Physics>("physics");
if (physics) {
this.body = new Body(this.kel);
this.body.mass = this.defn.defaults.mass;
this.body.friction = this.defn.defaults.friction;
this.body.restitution = this.defn.defaults.restitution;
this.body.bumpCanMove = this.defn.defaults.bumpCanMove;
physics.addBody(this.body);
}
stage.radar.add(this.kel);
}
public destroy() {
this.destroyed = true;
if (this.prog) {
this.prog.done = true;
}
this.stage.notify("character:destroying", this);
const physics = this.getPhysics();
if (physics) {
physics.removeBody(this.body);
this.body = null;
}
this.kel.destroy();
this.kel = null;
if (this.feeling) {
this.feeling.destroy();
this.feeling = null;
}
this.stage.remove(this);
}
public showFeeling(feeling: string) {
if (this.feeling) {
this.feeling.destroy();
this.feeling = null;
}
const icon = icons.get(feeling, true);
if (icon) {
this.feeling = new Kelpie(icon);
this.feeling.z = this.kel.z;
}
}
public moveBy(x: number, y: number) {
this.x += x;
this.y += y;
}
public queueImpulse(direction: Vec2, magnitude: number, type: ImpulseType) {
this.impulseQueue.push({
direction,
magnitude,
type
});
}
public addBump(char: Character) {
this.bumps.push(char);
}
public nextDirection(): Vec2 | null {
const v = this.computeImpulses();
if (!v) { return null; }
return Vec2.Normal(v);
}
getGameMode(): GameMode { return this.stage.get<GameMode>("gameMode"); }
getPhysics(): Physics { return this.stage.get<Physics>("physics"); }
update(dt: number) {
}
think() {
if (!this.destroyed && this.prog) {
this.prog.execute();
}
if (!this.destroyed) {
this.applyImpulses();
this.bumps = [];
}
}
computeImpulses(): Vec2 {
if (!this.impulseQueue.length) { return null; }
let finalDir = mkVec2();
const exclusiveOnly = this.impulseQueue.some(elem => elem.type === ImpulseType.Exclusive);
const allowDefault = this.impulseQueue.length === 1;
let impulseCount = 0;
for (const impulse of this.impulseQueue) {
if (exclusiveOnly && impulse.type !== ImpulseType.Exclusive) { continue; }
if (!allowDefault && impulse.type === ImpulseType.Default) { continue; }
const direction = impulse.direction;
const magnitude = impulse.magnitude;
finalDir = Vec2.Add(finalDir, Vec2.Scale(direction, magnitude));
impulseCount += 1;
if (exclusiveOnly) { break; }
}
if (impulseCount) {
return Vec2.Scale(finalDir, 1 / impulseCount);
}
return null;
}
applyImpulses() {
const v = this.computeImpulses();
if (v) {
this.body.vx += v.x;
this.body.vy += v.y;
}
this.impulseQueue = [];
}
kelUpdate(dt: number) {
const t = control.millis() / 100;
if (this.feeling) {
this.feeling.y = this.kel.top - Math.abs(Math.sin(t)) * 3;
this.feeling.x = this.kel.right;
}
}
notify(event: string, parm: any) {
if (event === "save") {
const savedGame = parm as SavedGame;
const state: CharacterState = {
x: this.x,
y: this.y,
id: this.defn.id,
bdefn: this.bdefn.toObj()
};
savedGame.chars.push(state);
} else if (event === "gameModeChanged") {
if (parm === GameMode.Edit) {
if (this.prog) {
this.prog.destroy();
this.prog = undefined;
}
} else if (parm === GameMode.Play) {
this.prog = new Program(this);
}
}
}
}
}