-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmyScene.ts
126 lines (109 loc) · 3 KB
/
myScene.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
// import duckengine and duck for types
import DuckEngine, { Duck } from '../../dist';
export default class MainScene extends DuckEngine.Scene {
public myRect!: Duck.TypeClasses.GameObjects.GameObject<'color'>;
public myCircle!: Duck.TypeClasses.GameObjects.GameObject<'color'>;
public gameObjects!: Duck.TypeClasses.Misc.Group<
Duck.TypeClasses.GameObjects.GameObject<'color'>
>;
public myCamera!: Duck.TypeClasses.Cameras.Camera;
public myParticle!: Duck.TypeClasses.GameObjects.Particles.Particle;
public myParticleEmitter!: Duck.TypeClasses.GameObjects.Particles.ParticleEmitter;
public myInput!: Duck.TypeClasses.Input.KeyboardInput;
private mySpeed = 250;
constructor(game: Duck.TypeClasses.Game) {
super('main', game);
}
public create() {
// gameobjects
this.myRect = this.add.gameobject.rect(0, 0, 15, 15, '#ffffff');
this.myCircle = this.add.gameobject.circle(50, 50, 10, '#ffffff');
this.gameObjects = this.add.group<
Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>
>('gameObjects', [this.myRect, this.myCircle]);
// colliders and hitboxes
this.myRect.physics.addHitbox();
this.myCircle.physics.addHitbox();
this.myRect.physics.addCollider([this.myCircle]);
// camera
this.myCamera = this.add.mainCamera();
this.myCamera.setFOV(1.1);
this.myCamera.setFOVSmooth(50, 0.1, this.myCamera.defaultFOV);
this.myCamera.startFollow(this.myRect, 0.1, 0.1);
// particles
this.myParticle = this.add.particle(
'circle',
0,
0,
5,
this.tools.color.randomWithAlpha(0.5)
);
this.myParticleEmitter = this.add.particleEmitter(
this.myParticle,
[0, 10],
[0, 10],
50
);
this.myParticleEmitter.emit();
this.myParticleEmitter.keepEmitting(100, 100);
this.myParticleEmitter.float([-200, 200], [-100, -300]);
// input
this.myInput = this.add.input().createKeyboardInput();
this.myInput.addKeys([
{
keyCode: 87, // w
descriptor: 'W',
},
{
keyCode: 83, // s
descriptor: 'S',
},
{
keyCode: 65, // a
descriptor: 'A',
},
{
keyCode: 68, // d
descriptor: 'D',
},
{
keyCode: 16, // shift
descriptor: 'SPRINT',
keyDown: () => {
this.mySpeed = 350;
},
keyUp: () => {
this.mySpeed = 250;
},
},
]);
}
public update() {
if (this.myInput.inputs.W.state) {
this.myRect.setVelocity('y', -this.mySpeed);
}
if (this.myInput.inputs.S.state) {
this.myRect.setVelocity('y', this.mySpeed);
}
if (this.myInput.inputs.A.state) {
this.myRect.setVelocity('x', -this.mySpeed);
}
if (this.myInput.inputs.D.state) {
this.myRect.setVelocity('x', this.mySpeed);
}
// offload particles
this.myParticleEmitter.offloadBounds({
x: 0,
y: 0,
w: this.game.canvas.width,
h: this.game.canvas.height,
});
this.myParticleEmitter.offloadMaxAge(10);
this.myParticleEmitter.offloadMaxAmount(100);
if (this.myRect.isCollidingGroup([this.myCircle])) {
this.myRect.setFillColor('#ff0000');
} else {
this.myRect.setFillColor('#2185d1');
}
}
}