forked from agateau/pixelwheels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRacer.java
More file actions
289 lines (247 loc) · 8.86 KB
/
Copy pathRacer.java
File metadata and controls
289 lines (247 loc) · 8.86 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright 2017 Aurélien Gâteau <mail@agateau.com>
*
* This file is part of Pixel Wheels.
*
* Tiny Wheels 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/>.
*/
package com.agateau.tinywheels;
import com.agateau.tinywheels.sound.AudioClipper;
import com.agateau.tinywheels.sound.AudioManager;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
/**
* A racer
*/
public class Racer extends GameObjectAdapter implements Collidable, Disposable {
private final Assets mAssets;
private final GameWorld mGameWorld;
private final Vehicle mVehicle;
private final VehicleRenderer mVehicleRenderer;
private final GroundCollisionHandlerComponent mGroundCollisionHandlerComponent;
private final SpinningComponent mSpinningComponent;
private final LapPositionComponent mLapPositionComponent;
private final AudioComponent mAudioComponent;
private final Array<Component> mComponents = new Array<Component>();
private final Array<Collidable> mCollidableComponents = new Array<Collidable>();
private Pilot mPilot;
// State
private Bonus mBonus;
interface Component {
void act(float delta);
}
private class PilotSupervisorComponent implements Component {
@Override
public void act(float delta) {
if (mLapPositionComponent.hasFinishedRace() || mSpinningComponent.isActive()
|| mGroundCollisionHandlerComponent.getState() != GroundCollisionHandlerComponent.State.NORMAL) {
mVehicle.setAccelerating(false);
} else {
mPilot.act(delta);
}
}
}
public Racer(Assets assets, AudioManager audioManager, GameWorld gameWorld, Vehicle vehicle) {
mAssets = assets;
mGameWorld = gameWorld;
mLapPositionComponent = new LapPositionComponent(gameWorld.getMapInfo(), vehicle);
mSpinningComponent = new SpinningComponent(vehicle);
mVehicle = vehicle;
mVehicle.setRacer(this);
mVehicle.setCollisionInfo(CollisionCategories.RACER,
CollisionCategories.WALL
| CollisionCategories.RACER | CollisionCategories.RACER_BULLET
| CollisionCategories.FLAT_OBJECT);
mVehicleRenderer = new VehicleRenderer(assets, mVehicle);
mGroundCollisionHandlerComponent = new GroundCollisionHandlerComponent(
assets,
mGameWorld,
this,
mLapPositionComponent);
PilotSupervisorComponent supervisorComponent = new PilotSupervisorComponent();
mAudioComponent = new AudioComponent(mAssets.soundAtlas, audioManager, this);
addComponent(mLapPositionComponent);
addComponent(mVehicle);
addComponent(mGroundCollisionHandlerComponent);
addComponent(mSpinningComponent);
addComponent(supervisorComponent);
addComponent(new BonusSpotHitComponent(this));
addComponent(mAudioComponent);
if (GamePlay.instance.createSpeedReport) {
Probe probe = new Probe("speed.dat");
mVehicle.setProbe(probe);
addComponent(probe);
}
}
private void addComponent(Component component) {
mComponents.add(component);
if (component instanceof Collidable) {
mCollidableComponents.add((Collidable)component);
}
}
public Pilot getPilot() {
return mPilot;
}
public void setPilot(Pilot pilot) {
mPilot = pilot;
}
public Vehicle getVehicle() {
return mVehicle;
}
public Bonus getBonus() {
return mBonus;
}
public LapPositionComponent getLapPositionComponent() {
return mLapPositionComponent;
}
public AudioComponent getAudioComponent() {
return mAudioComponent;
}
public void spin() {
if (mSpinningComponent.isActive()) {
return;
}
mSpinningComponent.start();
looseBonus();
}
@Override
public void beginContact(Contact contact, Fixture otherFixture) {
for (Collidable collidable : mCollidableComponents) {
collidable.beginContact(contact, otherFixture);
}
}
@Override
public void endContact(Contact contact, Fixture otherFixture) {
for (Collidable collidable : mCollidableComponents) {
collidable.endContact(contact, otherFixture);
}
}
@Override
public void preSolve(Contact contact, Fixture otherFixture, Manifold oldManifold) {
Object other = otherFixture.getBody().getUserData();
mAudioComponent.onCollision();
if (other instanceof Racer) {
contact.setEnabled(false);
Racer racer2 = (Racer)other;
Body body1 = getVehicle().getBody();
Body body2 = racer2.getVehicle().getBody();
float x1 = body1.getWorldCenter().x;
float y1 = body1.getWorldCenter().y;
float x2 = body2.getWorldCenter().x;
float y2 = body2.getWorldCenter().y;
final float k = 4;
body1.applyLinearImpulse(k * (x1 - x2), k * (y1 - y2), x1, y1, true);
body2.applyLinearImpulse(k * (x2 - x1), k * (y2 - y1), x2, y2, true);
}
for (Collidable collidable : mCollidableComponents) {
collidable.preSolve(contact, otherFixture, oldManifold);
}
}
@Override
public void postSolve(Contact contact, Fixture otherFixture, ContactImpulse impulse) {
for (Collidable collidable : mCollidableComponents) {
collidable.postSolve(contact, otherFixture, impulse);
}
}
@Override
public void dispose() {
for (Racer.Component component : mComponents) {
if (component instanceof Disposable) {
((Disposable) component).dispose();
}
}
}
@Override
public void act(float delta) {
for (Racer.Component component : mComponents) {
component.act(delta);
}
if (mBonus != null) {
mBonus.act(delta);
}
}
public void selectBonus() {
float normalizedRank = mGameWorld.getRacerNormalizedRank(this);
Array<BonusPool> pools = mGameWorld.getBonusPools();
float totalCount = 0;
for (BonusPool pool : pools) {
totalCount += pool.getCountForNormalizedRank(normalizedRank);
}
// To avoid allocating an array of the counts for each normalized rank, we subtract counts
// from pick, until it is less than 0, at this point we are on the selected pool
float pick = MathUtils.random(0f, totalCount);
BonusPool pool = null;
for (int idx = 0; idx < pools.size; ++idx) {
pool = pools.get(idx);
pick -= pool.getCountForNormalizedRank(normalizedRank);
if (pick < 0) {
break;
}
}
if (pool == null) {
pool = pools.get(pools.size - 1);
}
mBonus = pool.obtain();
mBonus.onPicked(this);
}
public void triggerBonus() {
if (mBonus == null) {
return;
}
mBonus.trigger();
}
/**
* Called by bonuses when they are done
*/
public void resetBonus() {
mBonus = null;
}
/**
* Called when something bad happens to the racer, causing her to loose her bonus
*/
public void looseBonus() {
if (mBonus != null) {
mBonus.onOwnerHit();
}
}
@Override
public void draw(Batch batch, int zIndex) {
mVehicleRenderer.draw(batch, zIndex);
}
@Override
public void audioRender(AudioClipper clipper) {
mAudioComponent.render(clipper);
}
@Override
public float getX() {
return mVehicle.getX();
}
@Override
public float getY() {
return mVehicle.getY();
}
public VehicleRenderer getVehicleRenderer() {
return mVehicleRenderer;
}
public void markRaceFinished() {
mLapPositionComponent.markRaceFinished();
}
}