Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. public class Player extends Sprite {
  2. public static final float scale = 12;
  3. public static final float moveSpeed = 110;
  4. public Body b2body;
  5.  
  6. public Player(World world) {
  7. super(new Texture("playercut.jpg"));
  8. setBounds(0,0,getWidth() / (FishAdventureMain.PPM * Player.scale), getHeight() / (FishAdventureMain.PPM * Player.scale));
  9. BodyDef bdef = new BodyDef();
  10. PolygonShape shape = new PolygonShape();
  11. FixtureDef fdef = new FixtureDef();
  12. bdef.type = BodyDef.BodyType.DynamicBody;
  13. bdef.position.set(650/FishAdventureMain.PPM, 200 / FishAdventureMain.PPM);
  14. b2body = world.createBody(bdef);
  15. shape.setAsBox(getWidth() / 3, getHeight() / 5);
  16. fdef.shape = shape;
  17. b2body.createFixture(fdef).setUserData("head");
  18. }
  19.  
  20. public void update() {
  21. super.setPosition(b2body.getPosition().x - getWidth()/2, (b2body.getPosition().y - getHeight()/2) + (float)Math.sin(System.currentTimeMillis() * 0.009) * 0.04f);
  22. }
  23.  
  24. public class AbstractGameObj {
  25. protected final float moveSpeed = 90;
  26. private Sprite sprite;
  27. protected World world;
  28. protected Body body;
  29. protected Fixture fixture;
  30. protected Vector2 position;
  31.  
  32. public AbstractGameObj(World world, Sprite sprite, Vector2 position, float scaleFactor1, float scaleFactor2) {
  33. this.world = world;
  34. this.sprite = sprite;
  35. this.position = position;
  36.  
  37. BodyDef bdef = new BodyDef();
  38. PolygonShape shape = new PolygonShape();
  39. FixtureDef fdef = new FixtureDef();
  40. bdef.position.set(position);
  41. bdef.type = BodyDef.BodyType.KinematicBody;
  42. body = world.createBody(bdef);
  43. shape.setAsBox(sprite.getWidth() / scaleFactor1, sprite.getHeight() / scaleFactor2);
  44. fdef.shape = shape;
  45. fixture = body.createFixture(fdef);
  46. }
  47. public void update(float dt) {
  48. sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getWidth() / 2);
  49. body.setLinearVelocity(moveSpeed *dt, 0);
  50. }
  51. public void onCollision() {
  52.  
  53. }
  54. public Sprite getSprite() { return this.sprite; }
  55.  
  56. public class Coconut extends AbstractGameObj{
  57.  
  58. public Coconut(World world, Sprite sprite, Vector2 position, float scaleFactor1, float scaleFactor2) {
  59. super(world, sprite, position, scaleFactor1, scaleFactor2);
  60. fixture.setUserData(this);
  61. }
  62.  
  63. @Override
  64. public void onCollision() {
  65. body.setTransform(100, 100, body.getAngle());
  66. }
  67.  
  68. public class B2WorldCreator {
  69.  
  70. private Sprite cocoSprite1;
  71. private Sprite cocoSprite2;
  72.  
  73. private Array<Coconut> cocoNuts;
  74.  
  75. public B2WorldCreator(World world) {
  76. //Border bottom
  77. BodyDef bdef = new BodyDef();
  78. PolygonShape shape = new PolygonShape();
  79. FixtureDef fdef = new FixtureDef();
  80.  
  81. bdef.type = BodyDef.BodyType.StaticBody;
  82. bdef.position.set(0, 0);
  83. shape.setAsBox(FishAdventureMain.V_WIDTH / FishAdventureMain.PPM, 1 / FishAdventureMain.PPM);
  84. fdef.shape = shape;
  85. world.createBody(bdef).createFixture(fdef);
  86.  
  87. //Border Top
  88. bdef.type = BodyDef.BodyType.StaticBody;
  89. bdef.position.set(0, FishAdventureMain.V_HEIGHT / FishAdventureMain.PPM);
  90. shape.setAsBox(FishAdventureMain.V_WIDTH / FishAdventureMain.PPM, 1 / FishAdventureMain.PPM);
  91. fdef.shape = shape;
  92. world.createBody(bdef).createFixture(fdef);
  93.  
  94. //Border left
  95. bdef.type = BodyDef.BodyType.StaticBody;
  96. bdef.position.set(FishAdventureMain.V_WIDTH / FishAdventureMain.PPM, FishAdventureMain.V_HEIGHT / FishAdventureMain.PPM);
  97. shape.setAsBox(1 / FishAdventureMain.PPM, FishAdventureMain.V_HEIGHT / FishAdventureMain.PPM);
  98. fdef.shape = shape;
  99. world.createBody(bdef).createFixture(fdef);
  100.  
  101. //Border kind of middle
  102. bdef.type = BodyDef.BodyType.StaticBody;
  103. bdef.position.set(200 / FishAdventureMain.PPM, 0);
  104. shape.setAsBox(1 / FishAdventureMain.PPM, FishAdventureMain.V_HEIGHT / FishAdventureMain.PPM);
  105. fdef.shape = shape;
  106. world.createBody(bdef).createFixture(fdef);
  107.  
  108. //Coconuts
  109. cocoNuts = new Array<Coconut>();
  110. cocoSprite1 = new Sprite(new Texture("coconut.jpg"));
  111. cocoSprite1.setBounds(0,0, cocoSprite1.getWidth() / FishAdventureMain.PPM/10, cocoSprite1.getHeight() / FishAdventureMain.PPM/10);
  112. cocoSprite2 = new Sprite(new Texture("coconut.jpg"));
  113. cocoSprite2.setBounds(0,0, cocoSprite2.getWidth() / FishAdventureMain.PPM/10, cocoSprite2.getHeight() / FishAdventureMain.PPM/10);
  114. cocoNuts.add(new Coconut(world, cocoSprite1, new Vector2(-300 / FishAdventureMain.PPM, 2), 1.8f, 1.8f));
  115. cocoNuts.add(new Coconut(world, cocoSprite2, new Vector2(-1800 / FishAdventureMain.PPM, 2), 1.8f, 1.8f));
  116.  
  117. }
  118. public void updateCocoNuts(float dt) {
  119. for (Coconut nut : cocoNuts) {
  120. nut.update(dt);
  121. }
  122. }
  123. public void showCocoNuts(SpriteBatch sb) {
  124. for (Coconut nut : cocoNuts) {
  125. nut.getSprite().draw(sb);
  126. }
  127. }
  128.  
  129. public class PlayScreen extends InputAdapter implements Screen {
  130. private FishAdventureMain main;
  131. private OrthographicCamera gameCam;
  132. private Viewport gamePort;
  133. private Hud hud;
  134. private ButtonController bcont;
  135. private Texture background;
  136. private World world;
  137. private Box2DDebugRenderer renderer;
  138. private Player player;
  139. private B2WorldCreator creator;
  140.  
  141. public PlayScreen(FishAdventureMain main) {
  142. this.main = main;
  143. gameCam = new OrthographicCamera();
  144. gamePort = new StretchViewport(FishAdventureMain.V_WIDTH / FishAdventureMain.PPM, FishAdventureMain.V_HEIGHT / FishAdventureMain.PPM, gameCam);
  145. hud = new Hud(main.batch);
  146. bcont = new ButtonController(main.batch);
  147. background = new Texture("background.jpg");
  148. world = new World(new Vector2(0, 0), true);
  149. creator = new B2WorldCreator(world);
  150. renderer = new Box2DDebugRenderer();
  151. player = new Player(world);
  152. gameCam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2,0);
  153. world.setContactListener(new ContactListener() {
  154. @Override
  155. public void beginContact(Contact contact) {
  156. Fixture fixA = contact.getFixtureA();
  157. Fixture fixB = contact.getFixtureB();
  158.  
  159. if (fixA.getUserData() == "head" || fixB.getUserData() == "head") {
  160. Fixture head = fixA.getUserData() == "head" ? fixA : fixB;
  161. Fixture object = head == fixA ? fixB : fixA;
  162. if (object.getUserData() != null && AbstractGameObj.class.isAssignableFrom(object.getUserData().getClass())) {
  163. ((AbstractGameObj) object.getUserData()).onCollision();
  164. }
  165. }
  166. }
  167.  
  168. @Override
  169. public void endContact(Contact contact) {
  170.  
  171. }
  172.  
  173. @Override
  174. public void preSolve(Contact contact, Manifold oldManifold) {
  175.  
  176. }
  177.  
  178. @Override
  179. public void postSolve(Contact contact, ContactImpulse impulse) {
  180.  
  181. }
  182. });
  183. }
  184.  
  185. @Override
  186. public void show() {
  187. Gdx.gl.glClearColor(0, 0,0,1);
  188. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  189.  
  190. main.batch.setProjectionMatrix(gamePort.getCamera().combined);
  191. main.batch.begin();
  192. main.batch.draw(background, 0, 0, FishAdventureMain.V_WIDTH / FishAdventureMain.PPM, FishAdventureMain.V_HEIGHT / FishAdventureMain.PPM);
  193. player.draw(main.batch);
  194. creator.showCocoNuts(main.batch);
  195. main.batch.end();
  196. renderer.render(world, gameCam.combined);
  197.  
  198. main.batch.setProjectionMatrix(hud.stage.getCamera().combined);
  199. hud.stage.draw();
  200. bcont.stage.draw();
  201. }
  202.  
  203. @Override
  204. public void render(float delta) {
  205. update(delta);
  206. show();
  207. }
  208.  
  209. public void update(float dt) {
  210. handleInput(dt);
  211. world.step(1/60f, 6, 2);
  212. gameCam.update();
  213. player.update();
  214. creator.updateCocoNuts(dt);
  215. }
  216.  
  217. private void handleInput(float dt) {
  218. if (bcont.getUpPressed())
  219. player.b2body.setLinearVelocity(new Vector2(player.b2body.getLinearVelocity().x, Player.moveSpeed * dt));
  220. else if (bcont.getDownPressed())
  221. player.b2body.setLinearVelocity(new Vector2(player.b2body.getLinearVelocity().x, -Player.moveSpeed * dt));
  222. else
  223. player.b2body.setLinearVelocity(player.b2body.getLinearVelocity().x, 0);
  224. if(bcont.getLeftPressed())
  225. player.b2body.setLinearVelocity(new Vector2(-Player.moveSpeed * dt, player.b2body.getLinearVelocity().y));
  226. else if (bcont.getRightPressed())
  227. player.b2body.setLinearVelocity(new Vector2(Player.moveSpeed * dt, player.b2body.getLinearVelocity().y));
  228. else
  229. player.b2body.setLinearVelocity(new Vector2(0, player.b2body.getLinearVelocity().y));
  230. }
  231.  
  232. @Override
  233. public void resize(int width, int height) {
  234. gamePort.update(width, height);
  235. }
  236.  
  237. @Override
  238. public void pause() {
  239.  
  240. }
  241.  
  242. @Override
  243. public void resume() {
  244.  
  245. }
  246.  
  247. @Override
  248. public void hide() {
  249.  
  250. }
  251.  
  252. @Override
  253. public void dispose() {
  254. background.dispose();
  255. hud.dispose();
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement