Guest User

Untitled

a guest
Jan 16th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input;
  5. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  6. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  7. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  8. import com.badlogic.gdx.math.Circle;
  9. import com.badlogic.gdx.math.Vector2;
  10.  
  11. public class BaseUnit {
  12. protected GameScreen gameScreen;
  13. protected Map map;
  14. protected TextureRegion[] regions;
  15. protected Vector2 position;
  16. protected Vector2 tempPosition;
  17. protected Vector2 velocity;
  18. protected float animationTime;
  19. protected boolean right;
  20. protected int maxHp;
  21. protected int hp;
  22. protected Circle hitArea;
  23. protected int width;
  24. protected int height;
  25. protected float firePressTimer;
  26. protected float timeBetweenFire;
  27. protected float speed;
  28. protected boolean active;
  29.  
  30. public Vector2 getPosition() {
  31. return position;
  32. }
  33.  
  34. public Vector2 getVelocity() {
  35. return velocity;
  36. }
  37.  
  38. public Circle getHitArea() {
  39. return hitArea;
  40. }
  41.  
  42. public BaseUnit(GameScreen gameScreen, Map map, TextureRegion original, int maxHp, float speed, float timeBetweenFire, float radius, float x, float y, int width, int height) {
  43. this.gameScreen = gameScreen;
  44. this.map = map;
  45. this.position = new Vector2(x, y);
  46. this.velocity = new Vector2(0, 0);
  47. this.tempPosition = new Vector2(0, 0);
  48. this.width = width;
  49. this.height = height;
  50. this.regions = new TextureRegion(original).split(width, height)[0];
  51. this.right = true;
  52. this.maxHp = maxHp;
  53. this.speed = speed;
  54. this.hp = this.maxHp;
  55. this.hitArea = new Circle(position, radius);
  56. this.timeBetweenFire = timeBetweenFire;
  57. }
  58.  
  59. public void update(float dt) {
  60. velocity.add(0, -600.0f * dt);
  61. tempPosition.set(position);
  62. tempPosition.add(width / 2, height / 2);
  63. velocity.x *= 0.6f;
  64. float len = velocity.len() * dt;
  65. float dx = velocity.x * dt / len;
  66. float dy = velocity.y * dt / len;
  67. for (int i = 0; i < len; i++) {
  68. tempPosition.y += dy;
  69. if (checkCollision(tempPosition)) {
  70. tempPosition.y -= dy;
  71. velocity.y = 0.0f;
  72. }
  73. tempPosition.x += dx;
  74. if (checkCollision(tempPosition)) {
  75. tempPosition.x -= dx;
  76. velocity.x = 0.0f;
  77. }
  78. }
  79. if (Math.abs(velocity.x) > 1.0f) {
  80. if (Math.abs(velocity.y) < 1.0f) {
  81. animationTime += (Math.abs(velocity.x) / 1800.0f);
  82. }
  83. } else {
  84. if (getCurrentFrame() > 0) {
  85. animationTime += dt * 50.0f;
  86. }
  87. }
  88. tempPosition.add(-width / 2, -height / 2);
  89. position.set(tempPosition);
  90. hitArea.setPosition(position.x + width / 2, position.y + height / 2);
  91. }
  92.  
  93. public void moveLeft() {
  94. velocity.x = -speed;
  95. right = false;
  96. }
  97.  
  98. public void moveRight() {
  99. velocity.x = speed;
  100. right = true;
  101. }
  102.  
  103. public void fire(float dt, boolean isPlayer) {
  104. firePressTimer += dt;
  105. if (firePressTimer > timeBetweenFire) {
  106. firePressTimer -= timeBetweenFire;
  107. float bulletVelX = 600.0f;
  108. if (!right) bulletVelX *= -1;
  109. gameScreen.getBulletEmitter().setup(isPlayer, position.x + width / 2, position.y + height / 2, bulletVelX, 0);
  110. }
  111. }
  112.  
  113. public void jump() {
  114. tempPosition.set(position).add(0, 1);
  115. if (Math.abs(velocity.y) < 0.1f && checkCollision(tempPosition)) {
  116. velocity.y = 400.0f;
  117. }
  118. }
  119.  
  120. public void takeDamage(int dmg) {
  121. hp -= dmg;
  122. }
  123.  
  124. public boolean checkCollision(Vector2 pos) {
  125. for (float i = 0; i < 6.28f; i += 0.1f) {
  126. if (!map.checkSpaceIsEmpty(pos.x + hitArea.radius * (float) Math.cos(i), pos.y + hitArea.radius * (float) Math.sin(i))) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132.  
  133. public void render(SpriteBatch batch) {
  134. int frameIndex = getCurrentFrame();
  135. if (!right && !regions[frameIndex].isFlipX()) {
  136. regions[frameIndex].flip(true, false);
  137. }
  138. if (right && regions[frameIndex].isFlipX()) {
  139. regions[frameIndex].flip(true, false);
  140. }
  141. batch.draw(regions[frameIndex], position.x, position.y, width / 2, height / 2, width, height, 1, 1, 0);
  142. }
  143.  
  144. public int getCurrentFrame() {
  145. return (int) animationTime % regions.length;
  146. }
  147.  
  148. public void addHP(int i) {
  149. hp += i;
  150. }
  151. }
Add Comment
Please, Sign In to add comment