Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. package com.cancer.entities;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input.Keys;
  5. import com.badlogic.gdx.graphics.Color;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.Animation;
  8. import com.badlogic.gdx.graphics.g2d.Sprite;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  11. import com.cancer.map.GameMap;
  12.  
  13. public class Player extends Entity {
  14.  
  15. private int SPEED = 4;
  16. private final int CLIVES = 3;
  17. private int userSpeed = 1 * SPEED;
  18. private int speedx = 0, speedy = 0;
  19. private Texture skin;
  20. private Sprite sprite;
  21. private TextureRegion[] animationFrames = new TextureRegion[3];
  22. private TextureRegion[] energizedFrames = new TextureRegion[3];
  23. private TextureRegion[] deathFrames = new TextureRegion[6];
  24. private TextureRegion frameActual;
  25. private Animation<TextureRegion> animation;
  26. private int score = 0;
  27. private float atime = 0f;
  28. private int trydir = -1;
  29. private float timer = 0;
  30. private int mode = -1;
  31. private int id;
  32. private float mytimer = 0;
  33.  
  34. public Player(int x, int y, GameMap map, int id) {
  35. super(x * map.TILESIZE, y * map.TILESIZE, EntityType.PLAYER, map);
  36. skin = new Texture(Gdx.files.internal("entities/pacman.png"));
  37. animationFrames[0] = new TextureRegion(new Texture(Gdx.files.internal("entities/pacman-sheet2.png")), 0, 0, 32,
  38. 32);
  39. animationFrames[1] = new TextureRegion(new Texture(Gdx.files.internal("entities/pacman-sheet2.png")), 32, 0, 32,
  40. 32);
  41. animationFrames[2] = new TextureRegion(new Texture(Gdx.files.internal("entities/pacman-sheet2.png")), 64, 0, 32,
  42. 32);
  43. energizedFrames[0] = new TextureRegion(new Texture(Gdx.files.internal("entities/energized-sheet.png")), 0, 0, 32,
  44. 32);
  45. energizedFrames[1] = new TextureRegion(new Texture(Gdx.files.internal("entities/energized-sheet.png")), 32, 0, 32,
  46. 32);
  47. energizedFrames[2] = new TextureRegion(new Texture(Gdx.files.internal("entities/energized-sheet.png")), 64, 0, 32,
  48. 32);
  49. deathFrames[0] = new TextureRegion(new Texture(Gdx.files.internal("entities/death-sheet.png")), 0, 0, 32,
  50. 32);
  51. deathFrames[1] = new TextureRegion(new Texture(Gdx.files.internal("entities/death-sheet.png")), 32, 0, 32,
  52. 32);
  53. deathFrames[2] = new TextureRegion(new Texture(Gdx.files.internal("entities/death-sheet.png")), 64, 0, 32,
  54. 32);
  55. deathFrames[3] = new TextureRegion(new Texture(Gdx.files.internal("entities/death-sheet.png")), 96, 0, 32,
  56. 32);
  57. deathFrames[4] = new TextureRegion(new Texture(Gdx.files.internal("entities/death-sheet.png")), 128, 0, 32,
  58. 32);
  59. deathFrames[5] = new TextureRegion(new Texture(Gdx.files.internal("entities/death-sheet.png")), 160, 0, 32,
  60. 32);
  61. animation = new Animation<TextureRegion>(0.05f, animationFrames);
  62. this.id = id;
  63. sprite = new Sprite(animationFrames[0]);
  64. sprite.setPosition(this.x, this.y);
  65. }
  66.  
  67. @Override
  68. public void update(float delta) {
  69. if(map.ingame) {
  70. mode = 1;
  71. }
  72. if(mode != -1) {
  73. if(map.getMode() == 1) {
  74. if(this.mode == 0) {
  75. this.mode = 1;
  76. }
  77. } else {
  78. if(this.mode == 1) {
  79. this.mode = 0;
  80. }
  81. }
  82. if(map.playerlives == 0) {
  83. map.setGameover(false);
  84. }
  85. if(map.checkCollisionWith(this.x, this.y, getWidth(), getHeight(), id)) {
  86. if(mode == 0 || !map.eatable) {
  87. map.restart();
  88. mode = 2;
  89. atime = 0;
  90. map.mode = 2;
  91. if(map.playerlives > 0) {
  92. map.playerlives -= 1;
  93. }
  94. System.out.println("Vidas restantes: " + map.playerlives);
  95. } else if(mode == 1){
  96. mode = 3;
  97. map.mode = 3;
  98. hide();
  99. }
  100. }
  101.  
  102.  
  103. atime += Gdx.graphics.getDeltaTime();
  104. if(mode == 2) {
  105. frameActual = animation.getKeyFrame(atime, false);
  106. } else if(mode != 3){
  107. frameActual = animation.getKeyFrame(atime, true);
  108. }
  109.  
  110. if(mode == 3) {
  111. mytimer += Gdx.graphics.getDeltaTime();
  112. System.out.println("timer: " + mytimer);
  113. if(mytimer >= 1) {
  114. show();
  115. if(map.getEnergytimer() != 0) {
  116. map.mode = 1;
  117. }
  118. map.ingame = true;
  119. mytimer = 0;
  120. }
  121. }
  122.  
  123. sprite.setRegion(frameActual);
  124. if(mode == 0) {
  125. animation = new Animation<TextureRegion>(0.05f, animationFrames);
  126. if(this.x % map.TILESIZE == 0 && this.y % map.TILESIZE == 0) {
  127. SPEED = 2;
  128. }
  129. } else if (mode == 1){
  130. animation = new Animation<TextureRegion>(0.05f, energizedFrames);
  131. if(this.x % map.TILESIZE == 0 && this.y % map.TILESIZE == 0) {
  132. SPEED = 4;
  133. }
  134. } else if (mode == 2) {
  135. animation = new Animation<TextureRegion>(0.2f, deathFrames);
  136. if(atime >= 1.5f) {
  137. if(map.playerlives == 0) {
  138. map.setGameover(true);
  139. map.ingame = false;
  140. } else {
  141. map.killPlayer();
  142. }
  143. }
  144.  
  145. }
  146. // Resetear la direccion buffereada
  147. if(trydir != -1) {
  148. timer += delta;
  149. if(timer >= 0.5) {
  150. trydir = -1;
  151. timer = 0;
  152. }
  153. } else {
  154. timer = 0;
  155. }
  156. sprite.setPosition(this.x, this.y);
  157.  
  158. if(this.mode < 2) {
  159. map.checkTile(this.x, this.y, this.getWidth(), this.getHeight(), id);
  160. this.checkdir();
  161. this.updateSkin();
  162. this.moveX(speedx);
  163. this.moveY(speedy);
  164. this.handleInput();
  165. }
  166. }
  167. }
  168.  
  169.  
  170. public void hide() {
  171. sprite.setSize(0, 0);
  172. }
  173.  
  174. public void show() {
  175. sprite.setSize(EntityType.PLAYER.getWidth(), EntityType.PLAYER.getHeight());
  176. }
  177.  
  178. private void handleInput() {
  179. if (Gdx.input.isKeyJustPressed(Keys.A)) {
  180. trydir = 0;
  181. }
  182. if (Gdx.input.isKeyJustPressed(Keys.D)) {
  183. trydir = 1;
  184. }
  185. if (Gdx.input.isKeyJustPressed(Keys.W)) {
  186. trydir = 2;
  187. }
  188. if (Gdx.input.isKeyJustPressed(Keys.S)) {
  189. trydir = 3;
  190. }
  191.  
  192. if (Gdx.input.isKeyJustPressed(Keys.TAB)) {
  193. score = map.getScore(0);
  194. if (!map.isDebugger()) {
  195. map.setDebugger(true);
  196. }
  197. }
  198. }
  199.  
  200. protected void moveX(float f) {
  201. float newX = this.x + f;
  202. if (!map.checkcolision(newX, this.y, getWidth(), getHeight(), direction)) {
  203. this.x = newX;
  204. }
  205. }
  206.  
  207. protected void moveY(float f) {
  208. float newY = this.y + f;
  209. if (!map.checkcolision(this.x, newY, getWidth(), getHeight(), direction)) {
  210. this.y = newY;
  211. }
  212. }
  213.  
  214. private void updateSkin() {
  215. switch (direction) {
  216. case -1:
  217. sprite.setRotation(0);
  218. sprite.setFlip(false, false);
  219. speedx = 0;
  220. speedy = 0;
  221. break;
  222. case 0:
  223. sprite.setRotation(0);
  224. sprite.setFlip(true, false);
  225. speedx = -SPEED;
  226. speedy = 0;
  227. break;
  228. case 1:
  229. sprite.setRotation(0);
  230. sprite.setFlip(false, false);
  231. speedx = SPEED;
  232. speedy = 0;
  233. break;
  234. case 2:
  235. sprite.setFlip(false, false);
  236. sprite.setRotation(90);
  237. speedx = 0;
  238. speedy = SPEED;
  239. break;
  240. case 3:
  241. sprite.setFlip(false, false);
  242. sprite.setRotation(270);
  243. speedx = 0;
  244. speedy = -SPEED;
  245. break;
  246. }
  247. }
  248.  
  249. private void checkdir() {
  250. switch (trydir) {
  251. case 0:
  252. if (this.y % map.TILESIZE == 0) {
  253. if (!map.checkcolision(this.x - getWidth(), this.y, getWidth(), getHeight(), trydir)) {
  254. direction = trydir;
  255. trydir = -1;
  256. }
  257. }
  258. break;
  259. case 1:
  260. if (this.y % map.TILESIZE == 0) {
  261. if (!map.checkcolision(this.x + getWidth(), this.y, getWidth(), getHeight(), trydir)) {
  262. direction = trydir;
  263. trydir = -1;
  264. }
  265. }
  266. break;
  267. case 2:
  268. if (this.x % map.TILESIZE == 0) {
  269. if (!map.checkcolision(this.x, this.y + getHeight(), getWidth(), getHeight(), trydir)) {
  270. direction = trydir;
  271. trydir = -1;
  272. }
  273. }
  274. break;
  275. case 3:
  276. if (this.x % map.TILESIZE == 0) {
  277. if (!map.checkcolision(this.x, this.y - getHeight(), getWidth(), getHeight(), trydir)) {
  278. direction = trydir;
  279. trydir = -1;
  280. }
  281. }
  282. break;
  283. }
  284. }
  285.  
  286. @Override
  287. public void render(SpriteBatch batch) {
  288. sprite.draw(batch);
  289.  
  290. }
  291.  
  292. public int getScore() {
  293. return score;
  294. }
  295.  
  296. public int getInt() {
  297. return getScore();
  298. }
  299.  
  300. public int getPlayerId() {
  301. return this.id;
  302. }
  303.  
  304. @Override
  305. public float getX() {
  306. return this.x;
  307. }
  308.  
  309. @Override
  310. public float getY() {
  311. return this.y;
  312. }
  313.  
  314.  
  315.  
  316.  
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement