-
Notifications
You must be signed in to change notification settings - Fork 1
/
player_controller.dart
197 lines (166 loc) · 5.82 KB
/
player_controller.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
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
part of game_client;
/**
* A controller for player entities. Also handles to local player.
*/
class PlayerController extends EntityController implements stagexl.Animatable {
final double _acceleration = 200.0;
final double _rotationSpeed = 5.0;
bool accelerate = false;
bool isLocalPlayer = false;
Movable get _movable => entity;
stagexl_particle.ParticleEmitter _particleEmitter;
stagexl_particle.ParticleEmitter get particleEmitter => _particleEmitter;
stagexl.TextField _displayNameTextField;
stagexl.Sprite _model;
var particleConfig = {
"maxParticles": 20,
"duration": 0.1,
"lifeSpan": 0.9,
"lifespanVariance": 0.4,
"startSize": 10,
"startSizeVariance": 20,
"finishSize": 70,
"finishSizeVariance": 0,
"shape": "circle",
"emitterType": 0,
"location": {
"x": 0,
"y": 0
},
"locationVariance": {
"x": 0,
"y": 0
},
"speed": 100,
"speedVariance": 10,
"angle": 270,
"angleVariance": 10,
"gravity": {
"x": 0,
"y": 0
},
"radialAcceleration": 20,
"radialAccelerationVariance": 0,
"tangentialAcceleration": 10,
"tangentialAccelerationVariance": 0,
"minRadius": 0,
"maxRadius": 100,
"maxRadiusVariance": 0,
"rotatePerSecond": 0,
"rotatePerSecondVariance": 0,
"compositeOperation": "source-over",
"startColor": {
"red": 1,
"green": 0.75,
"blue": 0,
"alpha": 1
},
"finishColor": {
"red": 1,
"green": 0,
"blue": 0,
"alpha": 0
}
};
PlayerController(entity) : super(entity){
_particleEmitter = new stagexl_particle.ParticleEmitter(particleConfig);
_particleEmitter.setEmitterLocation(0, -5);
_particleEmitter.stop(false);
}
configureInputControls(stagexl.Stage stage){
stage.onKeyDown.listen((stagexl.KeyboardEvent ke){
switch(ke.keyCode)
{
case html.KeyCode.LEFT:
this.rotateLeft();
break;
case html.KeyCode.RIGHT:
this.rotateRight();
break;
case html.KeyCode.UP:
accelerate = true;
break;
}
});
stage.onKeyUp.listen((stagexl.KeyboardEvent ke){
switch(ke.keyCode)
{
case html.KeyCode.LEFT:
this.stopRotation();
break;
case html.KeyCode.RIGHT:
this.stopRotation();
break;
case html.KeyCode.UP:
accelerate = false;
break;
}
});
}
_createSprite(Entity entity){
_model = new stagexl.Sprite();
RenderHelper.applySpaceship(_model.graphics, entity.radius);
sprite.addChild(_model);
if(entity.displayName != null && entity.displayName != "")
{
_displayNameTextField = new stagexl.TextField();
_displayNameTextField.text = entity.displayName;
_displayNameTextField.textColor = stagexl.Color.LightBlue;
_displayNameTextField.y = entity.radius;
_displayNameTextField.x = - _displayNameTextField.textWidth / 2.0;
_displayNameTextField.width = _displayNameTextField.textWidth;
sprite.addChild(_displayNameTextField);
}
}
updateSprite(){
sprite.x = _entity.position.x;
sprite.y = _entity.position.y;
_model.rotation = _entity.orientation;
}
bool advanceTime(num dt){
num angle = (this.entity.orientation / Math.PI / 2 * 360 - 90) % 360;
particleConfig["angle"] = angle;
_particleEmitter.updateConfig(particleConfig);
_particleEmitter.setEmitterLocation(this.entity.position.x, this.entity.position.y);
if(accelerate){
_particleEmitter.start();
_accelerate(new Vector2(0.0, _acceleration));
}
else
{
_particleEmitter.stop(false);
_accelerate(new Vector2.zero());
}
_particleEmitter.advanceTime(dt);
return true;
}
void rotateLeft(){
_movable.rotationSpeed = -_rotationSpeed;
}
void rotateRight(){
_movable.rotationSpeed = _rotationSpeed;
}
void stopRotation(){
_movable.rotationSpeed = 0.0;
}
void _accelerate(Vector2 direction){
//duplicate code in [GameServer]
//TODO: this can most propably be calculated in a simpler way. do it!
Vector3 acceleration3 =
new Matrix4.identity().
rotateZ(_entity.orientation).
translate(direction.x, direction.y).
getTranslation();
_movable.acceleration = new Vector2(acceleration3.x, acceleration3.y);
}
updateFromServer(Entity entity){
// we exclude orientation and rotationSpeed from the update, so that we
// can maintain smooth rotations on the client
if(this.isLocalPlayer && _movable.canMove){
Movable movable = entity as Movable;
movable.orientation = null;
movable.rotationSpeed = null;
}
super.updateFromServer(entity);
}
}