Advertisement
progrmor

Untitled

May 12th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import com.badlogic.gdx.ApplicationAdapter;
  2. import com.badlogic.gdx.Gdx;
  3. import com.badlogic.gdx.InputAdapter;
  4. import com.badlogic.gdx.graphics.GL20;
  5. import com.badlogic.gdx.graphics.OrthographicCamera;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.Sprite;
  8. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  9. import com.badlogic.gdx.math.Vector2;
  10. import com.badlogic.gdx.math.Vector3;
  11.  
  12.  
  13. public class SBGame extends ApplicationAdapter {
  14.  
  15. Vector3 temp = new Vector3();
  16.  
  17. OrthographicCamera camera;
  18.  
  19. SpriteBatch batch;
  20. Texture texture, textureTwo;
  21. Sprite sprite, spriteTwo, spriteThree;
  22.  
  23. Ball ball = new Ball(100, 100);
  24. Enemy enemy = new Enemy(150, 150);
  25.  
  26.  
  27.  
  28.  
  29. @Override
  30. public void create () {
  31. camera = new OrthographicCamera();
  32. camera.setToOrtho(false);
  33. batch = new SpriteBatch();
  34.  
  35.  
  36. texture = new Texture(Gdx.files.internal("ball.png"));
  37. textureTwo = new Texture(Gdx.files.internal("enemy.png"));
  38.  
  39. sprite = new Sprite(texture);
  40. spriteTwo = new Sprite(textureTwo);
  41.  
  42. Gdx.input.setInputProcessor(new InputAdapter() {
  43. @Override
  44. public boolean touchDown (int screenX, int screenY, int pointer, int button) {
  45. camera.unproject(temp.set(screenX, screenY, 0));
  46. ball.moveTo(temp.x, temp.y);
  47. return true;
  48. }
  49. });
  50. }
  51.  
  52. @Override
  53. public void render () {
  54. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  55. ball.update(Gdx.graphics.getDeltaTime());
  56. enemy.update(Gdx.graphics.getDeltaTime());
  57. sprite.setPosition(ball.position.x, ball.position.y);
  58. sprite.setSize(ball.getHeight(), ball.getWidth());
  59. spriteTwo.setPosition(enemy.positionTwo.x, enemy.positionTwo.y);
  60. spriteTwo.setSize(enemy.getHeight(), enemy.getWidth());
  61. batch.begin();
  62. sprite.draw(batch);
  63. spriteTwo.draw(batch);
  64. batch.end();
  65. }
  66.  
  67. @Override
  68. public void dispose () {
  69. texture.dispose();
  70. textureTwo.dispose();
  71. batch.dispose();
  72. }
  73.  
  74.  
  75. }
  76.  
  77. class Ball {
  78.  
  79. Vector2 position = new Vector2();
  80. Vector2 velocity = new Vector2();
  81. Vector2 movement = new Vector2();
  82. Vector2 target = new Vector2();
  83. Vector2 dir = new Vector2();
  84. private float width;
  85. private float height;
  86. float speed = 100;
  87.  
  88. public Ball (float x, float y) {
  89. position.set(x, y);
  90. target.set(x,y);
  91. this.width = 10;
  92. this.height = 10;
  93. }
  94.  
  95.  
  96.  
  97. public float getWidth() {
  98. return width;
  99. }
  100.  
  101. public float getHeight() {
  102. return height;
  103. }
  104.  
  105. public void update (float deltaTime) {
  106. movement.set(velocity).scl(deltaTime);
  107. if (position.dst2(target) > movement.len2()) {
  108. position.add(movement);
  109. } else {
  110. position.set(target);
  111. }
  112.  
  113.  
  114. }
  115.  
  116. public void moveTo (float x, float y) {
  117. dir.set(target.set(x,y)).sub(position).nor();
  118. velocity.set(dir).scl(speed);
  119. }
  120.  
  121. }
  122.  
  123.  
  124. class Enemy {
  125.  
  126. private Ball ball;
  127.  
  128. Vector2 positionTwo = new Vector2();
  129. Vector2 velocityTwo = new Vector2();
  130. Vector2 movementTwo = new Vector2();
  131. Vector2 targetTwo = new Vector2();
  132. Vector2 dirTwo = new Vector2();
  133.  
  134. private float width;
  135. private float height;
  136. float speed = 50;
  137.  
  138. public Enemy (float x, float y)
  139. {
  140. positionTwo.set(x, y);
  141. targetTwo.set(x,y);
  142. this.width = 5;
  143. this.height = 5;
  144.  
  145. }
  146.  
  147. public float getWidth() {
  148. return width;
  149. }
  150.  
  151. public float getHeight() {
  152. return height;
  153. }
  154.  
  155. public void update (float deltaTime) {
  156.  
  157. movementTwo.set(velocityTwo).scl(deltaTime);
  158. if (positionTwo.dst2(targetTwo) > movementTwo.len2()) {
  159. positionTwo.add(movementTwo);
  160. } else {
  161. positionTwo.set(targetTwo);
  162. }
  163.  
  164.  
  165. }
  166.  
  167. public void moveTo (float x, float y) {
  168. dirTwo.set(targetTwo.set(x,y)).sub(positionTwo).nor();
  169. velocityTwo.set(dirTwo).scl(speed);
  170.  
  171.  
  172.  
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement