SHOW:
|
|
- or go back to the newest paste.
1 | package com.me.game.screens; | |
2 | ||
3 | import com.badlogic.gdx.Game; | |
4 | import com.badlogic.gdx.Gdx; | |
5 | import com.badlogic.gdx.InputAdapter; | |
6 | import com.badlogic.gdx.InputMultiplexer; | |
7 | import com.badlogic.gdx.Screen; | |
8 | import com.badlogic.gdx.Input.Keys; | |
9 | import com.badlogic.gdx.graphics.GL20; | |
10 | import com.badlogic.gdx.graphics.OrthographicCamera; | |
11 | import com.badlogic.gdx.graphics.g2d.Sprite; | |
12 | import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
13 | import com.badlogic.gdx.math.MathUtils; | |
14 | import com.badlogic.gdx.math.Vector2; | |
15 | import com.badlogic.gdx.math.Vector3; | |
16 | import com.badlogic.gdx.physics.box2d.Body; | |
17 | import com.badlogic.gdx.physics.box2d.BodyDef; | |
18 | import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; | |
19 | import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; | |
20 | import com.badlogic.gdx.physics.box2d.ChainShape; | |
21 | import com.badlogic.gdx.physics.box2d.FixtureDef; | |
22 | import com.badlogic.gdx.physics.box2d.World; | |
23 | import com.badlogic.gdx.utils.Array; | |
24 | import com.badlogic.gdx.utils.FloatArray; | |
25 | import com.me.game.entities.Car; | |
26 | ||
27 | public class Play implements Screen { | |
28 | ||
29 | private World world; | |
30 | private Box2DDebugRenderer debugRenderer; | |
31 | private OrthographicCamera camera; | |
32 | private SpriteBatch batch; | |
33 | private final float timeStep = 1 / 60f; | |
34 | private final int velocityIterations = 8, positionIterations = 3; | |
35 | private Body ground; | |
36 | private Car car; | |
37 | private Array<Body> tmpBodies = new Array<Body>(); | |
38 | private boolean drawing; | |
39 | - | private FloatArray coords = new FloatArray(); |
39 | + | private FloatArray coords = new FloatArray(); |
40 | - | private Vector3 tmp = new Vector3(); |
40 | + | private Vector3 tmp = new Vector3(); |
41 | - | |
41 | + | |
42 | @Override | |
43 | public void render(float delta) { | |
44 | Gdx.gl.glClearColor(0, 0, 0, 1); | |
45 | Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
46 | - | |
46 | + | |
47 | - | |
47 | + | |
48 | // camera.position.set(box.getPosition().x, box.getPosition().y, 0); | |
49 | - | // camera.position.set(box.getPosition().x, box.getPosition().y, 0); |
49 | + | |
50 | - | |
50 | + | |
51 | ||
52 | - | |
52 | + | |
53 | batch.begin(); | |
54 | world.getBodies(tmpBodies); | |
55 | for(Body body : tmpBodies) | |
56 | if(body.getUserData() != null && body.getUserData() instanceof Sprite) { | |
57 | Sprite sprite = (Sprite) body.getUserData(); | |
58 | sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2); | |
59 | sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees); | |
60 | sprite.draw(batch); | |
61 | } | |
62 | batch.end(); | |
63 | debugRenderer.render(world, camera.combined); | |
64 | } | |
65 | ||
66 | @Override | |
67 | public void resize(int width, int height) { | |
68 | camera.viewportWidth = width / 50; | |
69 | camera.viewportHeight = height / 50; | |
70 | camera.update(); | |
71 | } | |
72 | ||
73 | @Override | |
74 | public void show() { | |
75 | batch = new SpriteBatch(); | |
76 | - | batch = new SpriteBatch(); |
76 | + | |
77 | debugRenderer = new Box2DDebugRenderer(); | |
78 | camera = new OrthographicCamera(); | |
79 | ||
80 | - | |
80 | + | |
81 | final FixtureDef fixtureDef = new FixtureDef(), wheelFixtureDef = new FixtureDef(); | |
82 | ||
83 | - | |
83 | + | |
84 | fixtureDef.density = 5; | |
85 | fixtureDef.friction = .4f; | |
86 | fixtureDef.restitution = .3f; | |
87 | ||
88 | - | |
88 | + | |
89 | wheelFixtureDef.friction = 5000; | |
90 | wheelFixtureDef.restitution = .4f; | |
91 | car = new Car(world, fixtureDef, wheelFixtureDef, 0, 3, 3, 1); | |
92 | Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() { | |
93 | @Override | |
94 | public boolean keyDown(int keycode) { | |
95 | switch(keycode) { | |
96 | case Keys.ESCAPE: | |
97 | ((Game) Gdx.app.getApplicationListener()).setScreen(new Levels()); | |
98 | - | ((Game) Gdx.app.getApplicationListener()) |
98 | + | |
99 | - | .setScreen(new Levels()); |
99 | + | |
100 | return false; | |
101 | } | |
102 | ||
103 | @Override | |
104 | - | |
104 | + | |
105 | if(drawing) { | |
106 | tmp.x = screenX; | |
107 | tmp.y = screenY; | |
108 | - | tmp.x = screenX; |
108 | + | camera.unproject(tmp); |
109 | - | tmp.y = screenY; |
109 | + | coords.add(tmp.x); |
110 | - | camera.unproject(tmp); |
110 | + | coords.add(tmp.y); |
111 | - | coords.add(tmp.x); |
111 | + | return true; |
112 | - | coords.add(tmp.y); |
112 | + | |
113 | - | return true; |
113 | + | |
114 | - | } |
114 | + | |
115 | - | return false; |
115 | + | |
116 | @Override | |
117 | - | |
117 | + | |
118 | - | |
118 | + | |
119 | return true; | |
120 | } | |
121 | ||
122 | @Override | |
123 | public boolean keyTyped(char character) { | |
124 | - | |
124 | + | switch(character) { |
125 | - | @Override |
125 | + | case 'c': |
126 | - | public boolean keyTyped(char character) { |
126 | + | if(drawing) // drawing was enabled, so this will disable it and create the ChainShape |
127 | - | switch(character) { |
127 | + | createChain(); |
128 | - | case 'c': |
128 | + | drawing = !drawing; // toggle |
129 | - | if(drawing) // drawing was enabled, so this will disable it and create the ChainShape |
129 | + | return true; |
130 | - | createChainShape(); |
130 | + | default: |
131 | - | drawing = !drawing; // toggle |
131 | + | return false; |
132 | - | return true; |
132 | + | |
133 | - | default: |
133 | + | |
134 | - | return false; |
134 | + | |
135 | - | } |
135 | + | public Body createChain() { |
136 | - | } |
136 | + | ChainShape shape = new ChainShape(); |
137 | coords.shrink(); // reduce inner array to make sure it's not longer than needed | |
138 | shape.createChain(coords.items); | |
139 | ||
140 | fixtureDef.shape = shape; | |
141 | - | |
141 | + | // SETUP THE OTHER FIXTURE PROPERTIES HERE |
142 | ||
143 | Body body = world.createBody(bodyDef); | |
144 | - | groundShape.createChain(new Vector2[] {new Vector2(-50000, 0), new Vector2(50000, 0)} ); |
144 | + | body.createFixture(fixtureDef); |
145 | - | |
145 | + | return body; |
146 | - | |
146 | + | |
147 | ||
148 | }, car)); | |
149 | // GROUND | |
150 | bodyDef.type = BodyType.StaticBody; | |
151 | - | |
151 | + | |
152 | ||
153 | // ground shape | |
154 | ChainShape groundShape = new ChainShape(); | |
155 | - | |
155 | + | groundShape.createChain(new Vector2[] {new Vector2(-50000, 0), new Vector2(50000, 0)}); |
156 | - | |
156 | + | |
157 | // fixture definition | |
158 | fixtureDef.shape = groundShape; | |
159 | fixtureDef.friction = 10; | |
160 | fixtureDef.restitution = 0f; | |
161 | ||
162 | ground = world.createBody(bodyDef); | |
163 | ground.createFixture(fixtureDef); | |
164 | groundShape.dispose(); | |
165 | ||
166 | } | |
167 | ||
168 | @Override | |
169 | public void hide() { | |
170 | dispose(); | |
171 | } | |
172 | - | public ChainShape createChainShape() { |
172 | + | |
173 | - | ChainShape shape = new ChainShape(); |
173 | + | |
174 | - | coords.shrink(); // reduce inner array to make sure it's not longer than needed |
174 | + | |
175 | - | shape.createChain(coords.items); |
175 | + | |
176 | - | return shape; |
176 | + | |
177 | - | } |
177 | + | |
178 | - | |
178 | + | |
179 | } | |
180 | ||
181 | @Override | |
182 | public void dispose() { | |
183 | world.dispose(); | |
184 | debugRenderer.dispose(); | |
185 | } | |
186 | ||
187 | } |