HarveyRoper

Player

Aug 17th, 2019
95
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Rectangle;
  6. import java.util.LinkedList;
  7.  
  8. public class Player extends GameObject{
  9.  
  10. /*
  11. *
  12. Powerup ideas:
  13.  
  14. > Build a temporary wall
  15. > Lock on aim;
  16. > Rapid fire
  17. > Burst Shot;
  18.  
  19. */
  20.  
  21.  
  22. private float _acc = 0.1f;
  23. private float _dcc = 0.2f;
  24. private KeyInput input;
  25.  
  26. private int width = 32;
  27. private int height = 32;
  28.  
  29. private float health = 32;
  30.  
  31. public int frequency = 50;
  32. public int iterator = 0;
  33.  
  34. private final float MAX_SPEED = 20;
  35.  
  36. private float gravity = 1f;
  37.  
  38. private Handler handler;
  39.  
  40. public Player(float x, float y, ID id, KeyInput input, Handler handler) {
  41. super(x,y,id);
  42. this.input = input;
  43. this.handler = handler;
  44. }
  45.  
  46. public void tick(LinkedList<GameObject> object) {
  47. iterator++; //For Regular shooting intervals. Yet to be added
  48.  
  49. x += velX;
  50. y += velY;
  51.  
  52. if(falling || jumping) {
  53. velY += gravity;
  54.  
  55. if(velY > MAX_SPEED) {
  56. velY = MAX_SPEED;
  57. }
  58. }
  59.  
  60. Collision(object);
  61. bulletCollision(object);
  62.  
  63. //Horizontal
  64. if(input.keys[0]) velX = 5;
  65. else if(input.keys[1]) velX = -5;
  66. else if(!input.keys[0] && !input.keys[1]) {
  67. if(velX > 0 ) velX = 0;
  68. else if(velX < 0) velX = 0;
  69. }
  70.  
  71. velX = clamp(velX, 8 , -8);
  72. velY = clamp(velY, MAX_SPEED, -MAX_SPEED);
  73.  
  74.  
  75. if(x >= Game.WIDTH - 38) x = Game.WIDTH - 38;
  76. if(x <= 0) x = 0;
  77. if(y >= Game.HEIGHT - 60) y = Game.HEIGHT - 60;
  78. if(y <= 0) {
  79. velY = 0;
  80. velX = 0;
  81. y = 1;
  82. }
  83.  
  84. if(health <= 0) {
  85. handler.setPlaying(false);
  86. }
  87.  
  88. }
  89.  
  90. private void bulletCollision(LinkedList<GameObject> object) {
  91. for(int i = 0; i < handler.object.size(); i++) {
  92. GameObject tempObject = handler.object.get(i);
  93.  
  94. if(tempObject.getId() == ID.EnemyBullet) {
  95.  
  96. if(tempObject.getBounds().intersects(getBoundsTop()) || tempObject.getBounds().intersects(getBoundsBottom()) || tempObject.getBounds().intersects(getBoundsLeft()) || tempObject.getBounds().intersects(getBoundsRight())) {
  97.  
  98. health -= 2;
  99.  
  100. }
  101. }
  102. }
  103. }
  104.  
  105. private void Collision(LinkedList<GameObject> object) {
  106.  
  107. for(int i = 0; i < handler.object.size(); i++) {
  108. GameObject tempObject = handler.object.get(i);
  109.  
  110.  
  111. if(tempObject.getId() == ID.Block) {
  112.  
  113. if(getBoundsTop().intersects(tempObject.getBounds())) {
  114. y = tempObject.getY() + height;
  115. velY = 0;
  116. }
  117.  
  118. if(getBoundsBottom().intersects(tempObject.getBounds())) {
  119. y = tempObject.getY() - height;
  120. velY = 0;
  121. falling = false;
  122. jumping = false;
  123. }else falling = true;
  124.  
  125. //Right
  126. if(getBoundsRight().intersects(tempObject.getBounds())) {
  127. x = tempObject.getX() - width;
  128.  
  129. }
  130.  
  131. //Left
  132. if(getBoundsLeft().intersects(tempObject.getBounds())) {
  133. x = tempObject.getX() + width;
  134.  
  135. }
  136. }
  137. }
  138.  
  139. }
  140.  
  141.  
  142.  
  143. private float clamp(float value, float max, float min) {
  144. if(value > max) value = max;
  145. else if( value <= min) value = min;
  146.  
  147. return value;
  148. }
  149.  
  150.  
  151. public void render(Graphics g) {
  152. Graphics2D g2d = (Graphics2D) g;
  153.  
  154. g.setColor(Color.blue);
  155. g.fillRect((int)x, (int)y, width, height+1);
  156. g.setFont(new Font("Arial", Font.PLAIN, 10));
  157. g.setColor(Color.RED);
  158. g.fillRect((int)x, (int)y - 10, (int)health, 5);
  159. g.setColor(Color.WHITE);
  160. g.drawRect((int)x, (int)y - 10, width, 5);
  161. //g.drawString("x: "+x + " y: "+y, (int)x, (int)y-5);
  162.  
  163. g2d.draw(getBoundsBottom());
  164. g2d.draw(getBoundsLeft());
  165. g2d.draw(getBoundsRight());
  166. g2d.draw(getBoundsTop());
  167.  
  168. }
  169.  
  170.  
  171. public int getWidth() {
  172. return width;
  173. }
  174.  
  175. public void setWidth(int width) {
  176. this.width = width;
  177. }
  178.  
  179. public int getHeight() {
  180. return height;
  181. }
  182.  
  183. public void setHeight(int height) {
  184. this.height = height;
  185. }
  186.  
  187. public float getHealth() {
  188. return health;
  189. }
  190.  
  191. public void setHealth(float health) {
  192. this.health = health;
  193. }
  194.  
  195. public Rectangle getBoundsTop() {
  196. return new Rectangle((int)x+width/2-((width/2)/2),(int)y, width/2, height/2);
  197. }
  198. public Rectangle getBoundsBottom() {
  199. return new Rectangle((int)x+width/2-((width/2)/2),(int)y+(height/2), width/2, height/2);
  200. }
  201. public Rectangle getBoundsLeft() {
  202. return new Rectangle((int)x,(int)y, 5, height-2);
  203. }
  204. public Rectangle getBoundsRight() {
  205. return new Rectangle((int)x+width-5,(int)y, 5, height-2);
  206. }
  207.  
  208. }
RAW Paste Data