Advertisement
Guest User

Kenny

a guest
Mar 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.08 KB | None | 0 0
  1. package com.teamnine.game.Model;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input;
  5. import com.badlogic.gdx.assets.AssetManager;
  6. import com.badlogic.gdx.audio.Sound;
  7. import com.badlogic.gdx.graphics.Texture;
  8. import com.badlogic.gdx.graphics.g2d.Animation;
  9. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  10. import com.badlogic.gdx.math.Vector2;
  11. import com.badlogic.gdx.physics.box2d.BodyDef;
  12. import com.badlogic.gdx.physics.box2d.FixtureDef;
  13. import com.badlogic.gdx.physics.box2d.PolygonShape;
  14. import com.badlogic.gdx.scenes.scene2d.Stage;
  15. import com.badlogic.gdx.utils.Array;
  16. import com.teamnine.game.Entity.Attacks.Swing;
  17. import com.teamnine.game.Entity.Attacks.Weapon;
  18. import com.teamnine.game.Entity.Item;
  19. import com.teamnine.game.Entity.Note;
  20. import com.teamnine.game.Map.Location;
  21. import com.teamnine.game.Model.AbstractPlayer;
  22. import com.teamnine.game.Model.NPC.Npc;
  23. import com.teamnine.game.Player.PlayerManager;
  24. import com.teamnine.game.Screen.Play;
  25. import com.teamnine.game.Searching;
  26.  
  27. /**
  28. * Models the kenny actor
  29. */
  30. public class Kenny extends AbstractPlayer implements Animatable{
  31.  
  32. // Kenny stats variables
  33. private Item[] equippedItems;
  34. private int equippedItemsCount;
  35. private boolean walking;
  36. protected Stage stage;
  37. // Track kenny's state
  38. public enum State{IDLE, WALKING, RUNNING, JUMPING, DEAD, CROUCHING, SLIDING, ATTACK1, ATTACK2, FALLING, BOW1, BOW2, PUNCH1, PUNCH2, DIE, SWORDDRAW, SWORDSHEATH};
  39. private State currentState, previousState;
  40. private int jumpCount; // Max is double-jump; prevent extra jumps
  41.  
  42. // Check which world screen kenny is in (i.e. menu or play)
  43. //private boolean playscreen;
  44.  
  45. // Animations
  46. private Animation<TextureRegion> animIdle1, animIdle2; // Idle 1: without sword, Idle 2: with sword
  47. private Animation<TextureRegion> animRun1, animRun2, animWalk; // Run 1: without sword, Run 2: with sword
  48. private Animation<TextureRegion> animJump, animCrouch, animDie, animCrouchWalk;
  49. private Animation<TextureRegion> animPunch1, animPunch2;
  50. private Animation<TextureRegion> animSword1, animSword2;
  51. private Animation<TextureRegion> animSwordDraw, animSwordSheath;
  52. private Animation<TextureRegion> animBow1, animBow2;
  53. private boolean runAnimSwordDraw, runAnimSwordSheath;
  54. private boolean runanimPunch1, runanimPunch2, runanimSword1, canSoundanimSword2, runanimSword2, runanimDie, canSoundanimSword1, runanimBow1, canSoundanimBow1, runanimBow2;
  55. private Animation<TextureRegion> kennySomersault;
  56. private Animation<TextureRegion> kennySlide;
  57. private boolean runKennySlide;
  58. private Animation<TextureRegion> kennyFall;
  59.  
  60. // Music related
  61. private AssetManager soundManager;
  62. private Sound sound;
  63. private Array<Sound> animRunSounds;
  64.  
  65. // Records time in a specific state
  66. private float stateTimer;
  67. public Kenny(PlayerManager playerManager, Play screen, Location location, Vector2 fixtureSize, int inventorySize) {
  68. super(playerManager, screen, location, fixtureSize, inventorySize);
  69. soundManager = screen.getGame().getSoundAssetManager();
  70. generateAnimations();
  71. initStats(); // initialise kenny's stats
  72.  
  73. stateTimer = 0;
  74. facingRight = true;
  75. currentState = previousState = State.IDLE;
  76. walking = true;
  77.  
  78. equippedItems = new Item[10];
  79. equippedItemsCount = 0;
  80.  
  81. // Display kenny
  82. texture = new TextureRegion(((Play)screen).getActorAtlas().findRegion("adventurer-idle-00"), 0,0,50,37);
  83. texture.getTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
  84.  
  85. // Defining actor
  86. fixtureSize = new Vector2(6,14);
  87. defineActor(fixtureSize.x, fixtureSize.y);
  88. setBounds(0,0,50 / Searching.PPM, 37 / Searching.PPM);
  89. setRegion(texture);
  90.  
  91. }
  92.  
  93. @Override
  94. public void generateAnimations() {
  95. // Animation
  96. Array<TextureRegion> frames = new Array<TextureRegion>();
  97.  
  98. for (int i = 0; i < 4; i++) // Idle 1 - without sword
  99. frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_idle"), i*50,0, 50, 37));
  100. animIdle1 = new Animation<TextureRegion>(0.3f, frames);
  101. frames.clear();
  102. for (int i = 0; i < 4; i++) // Idle 2 - with sword
  103. frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_idle2"), i*50, 0, 50, 37));
  104. animIdle2 = new Animation<TextureRegion>(0.3f, frames);
  105. frames.clear();
  106. for (int i = 0; i < 6; i++) // Run 1 - without sword
  107. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_run"),0 , i*37, 50, 37));
  108. animRun1 = new Animation(0.1f, frames);
  109. frames.clear();
  110. for (int i = 0; i < 6; i++)
  111. frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_run2"), i*50, 0, 50, 37));
  112. animRun2 = new Animation<TextureRegion>(0.1f, frames);
  113. frames.clear();
  114. for (int i = 0; i < 4; i++) // Jump
  115. frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_jump"), i*50, 0, 50, 37));
  116. animJump = new Animation(0.02f, frames);
  117. frames.clear(); // Crouch
  118. for (int i = 0; i < 4; i++)
  119. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_crouch"), i*50, 0, 50, 37));
  120. animCrouch = new Animation(0.25f, frames);
  121.  
  122. frames.clear(); // Sword slash 1 - light
  123. for (int i = 0; i < 6; i++)
  124. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_attack2"), 0, i*37, 50, 37));
  125. animSword1 = new Animation<TextureRegion>(0.1f, frames);
  126. runanimSword1 = false; canSoundanimSword1 = true;
  127.  
  128. frames.clear(); // Sword slash 2 - heavy
  129. for (int i = 0; i < 5; i++)
  130. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_attack1"), i*50, 0, 50, 37));
  131. animSword2 = new Animation<TextureRegion>(0.1f, frames);
  132. runanimSword2 = false; canSoundanimSword2 = true;
  133.  
  134. frames.clear(); // Bow on-ground
  135. for (int i = 0; i < 9; i++)
  136. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_bow"), i*50, 0, 50, 37));
  137. animBow1 = new Animation<TextureRegion>(0.1f, frames);
  138. runanimBow1 = false; canSoundanimBow1 = true;
  139.  
  140. frames.clear(); // Bow mid-air
  141. for (int i = 0; i < 6; i++)
  142. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_bow_jump"), 0, i*37, 50, 37));
  143. animBow2 = new Animation<TextureRegion>(0.1f, frames);
  144. runanimBow2 = false;
  145.  
  146. frames.clear(); // Somersault
  147. for (int i = 0; i < 4; i++)
  148. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_smrslt"), i*50, 0, 50, 37));
  149. kennySomersault = new Animation<TextureRegion>(0.1f, frames);
  150. frames.clear(); // Sliding
  151. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer-slide-00"), 0,0,50,37));
  152. kennySlide = new Animation<TextureRegion>(0.1f, frames);
  153. runKennySlide = false;
  154.  
  155. frames.clear(); // Falling
  156. for (int i = 0; i < 2; i++)
  157. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_fall"), i*50, 0, 50, 37));
  158. kennyFall = new Animation<TextureRegion>(0.1f, frames);
  159. frames.clear(); // Dying
  160. for (int i = 0; i < 6; i++)
  161. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_die"), i*50, 0, 50, 37));
  162. animDie = new Animation<TextureRegion>(0.1f, frames);
  163.  
  164. frames.clear(); // Punch 1 - light
  165. for (int i = 0; i < 4; i++)
  166. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_punch"), i*50, 0, 50, 37));
  167. animPunch1 = new Animation<TextureRegion>(0.1f, frames);
  168. runanimPunch1 = false;
  169.  
  170. frames.clear(); // Punch 2 - heavy
  171. for (int i = 0; i < 7; i++)
  172. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_run_punch"), i*50, 0, 50, 37));
  173. animPunch2 = new Animation<TextureRegion>(0.1f, frames);
  174. runanimPunch2 = false;
  175.  
  176. frames.clear(); // Crouch walking
  177. for (int i = 0; i < 6; i++)
  178. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_crouch_walk"), 0, i*37, 50, 37));
  179. animCrouchWalk = new Animation<TextureRegion>(0.1f, frames);
  180. frames.clear(); // Walking
  181. for (int i = 0; i < 6; i++)
  182. frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_walk"), 0, i*37, 50, 37));
  183. animWalk = new Animation<TextureRegion>(0.1f, frames);
  184. frames.clear(); // Sword draw
  185. for (int i = 0; i < 4; i++)
  186. frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_draw"), i*50, 0, 50, 37));
  187. animSwordDraw = new Animation<TextureRegion>(0.1f, frames);
  188. frames.clear();
  189. for (int i = 0; i < 4; i++) // Sword sheath
  190. frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_shte"), i*50, 0, 50, 37));
  191. animSwordSheath = new Animation<TextureRegion>(0.1f, frames);
  192.  
  193. }
  194.  
  195. /**
  196. * Initialises Kenny's stat variables
  197. */
  198. @Override
  199. protected void initStats(){
  200. // Set kenny stats and inventory
  201. jumpCount = 0;
  202. hp = basehp = 100;
  203. mana = baseMana = 100;
  204. attack = baseattack = 10;
  205. defence = basedefence = 10;
  206. hunger = 100;
  207. }
  208.  
  209. public void defineActor(float hx, float hy) {
  210. BodyDef bodydef = new BodyDef();
  211. bodydef.position.set(location.getX(), location.getY());
  212. // Setting to DynamicBody allows to interact with environment/objects
  213. bodydef.type = BodyDef.BodyType.DynamicBody;
  214.  
  215. // Create body in level
  216. body = world.createBody(bodydef);
  217. // Fixtures are physical attributes (check libgdx doc) - here we're defining the fixture
  218. //CircleShape shape = new CircleShape();
  219. //shape.setRadius(7);
  220. PolygonShape shape = new PolygonShape();
  221. shape.setAsBox(hx / Searching.PPM,hy / Searching.PPM); // Kenny size
  222. FixtureDef fixturedef = new FixtureDef();
  223. fixturedef.shape = shape;
  224. // Setting categorybit for collision
  225. fixturedef.filter.categoryBits = Searching.ACTOR_BIT;
  226. fixturedef.density *= 2;
  227. fixturedef.restitution = 0;
  228.  
  229. body.createFixture(fixturedef).setUserData(this);
  230. }
  231.  
  232. /** Game logic operations **/
  233.  
  234. public void setDead() {
  235. setToDie = true;
  236. currentState = State.DIE;
  237. }
  238. public void increaseStateTimer(float deltatime){
  239. //System.out.println(stateTimer);
  240. switch (currentState){
  241. case BOW1:
  242. stateTimer = (animBow1.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
  243. break;
  244. case BOW2:
  245. stateTimer = (animBow2.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
  246. break;
  247. case ATTACK1:
  248. stateTimer = (animSword1.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
  249. break;
  250. case ATTACK2:
  251. stateTimer = (animSword2.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
  252. break;
  253. case PUNCH1:
  254. stateTimer = (animPunch1.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
  255. break;
  256. case PUNCH2:
  257. stateTimer = (animPunch2.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
  258. break;
  259. case DIE:
  260. stateTimer = (animDie.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
  261. break;
  262. default:
  263. stateTimer = (currentState == previousState) ? stateTimer + deltatime : 0;
  264. break;
  265. }
  266. }
  267.  
  268. /**
  269. * Update any changes made to kenny's behaviour
  270. * @param dt Delta time
  271. */
  272. public void update(float dt, String mapName){
  273. super.update(dt, mapName);
  274. if (active){
  275.  
  276.  
  277. if (currentState == State.DEAD){
  278.  
  279. } else {
  280. handleInput();
  281.  
  282. if (setToDestroy && !destroyed){
  283. // Destroy enemy
  284. stateTimer = 0;
  285. world.destroyBody(body);
  286. destroyed = true; // possibly remove this line
  287. setToDestroy = false;
  288. return;
  289. }
  290.  
  291. if (this.body.getLinearVelocity().y == 0){
  292. jumpCount = 0;
  293. }
  294.  
  295. location.set(body.getPosition().x, body.getPosition().y);
  296. setRegion(getFrame(dt));
  297. getInventoryCount(); // Update inventory count
  298.  
  299. if (hand instanceof Weapon){
  300. for (Swing swing : ((Weapon) hand).getSwings())
  301. swing.update(dt);
  302. }
  303. }
  304. }
  305.  
  306. adjustOffsetPosition();
  307. }
  308.  
  309. @Override
  310. public void handleInput() {
  311. if (playerManager.getCurrentMode() == PlayerManager.Mode.PLAY){
  312. if (Gdx.input.isKeyJustPressed(Input.Keys.Q)) { // Check for Quest, else switch inventory[0] with hand
  313. if (actorCollidedWith != null){
  314. if (actorCollidedWith instanceof Npc){
  315. ((Npc) actorCollidedWith).startQuestConfirmationDialogue((screen).getDialogueController(), playerManager);
  316. }
  317. } else {
  318. this.switchHand();
  319. }
  320. } else if (Gdx.input.isKeyJustPressed(Input.Keys.E)) { // Check for Npc interaction, else pickup item
  321. if (actorCollidedWith != null){
  322. if (actorCollidedWith instanceof Npc){
  323. if (((Npc) actorCollidedWith).hasDialogue())
  324. //npc.startDialogue(screen.getDialogueController(), kenny);
  325. ((Npc) actorCollidedWith).startDialogue((screen).getDialogueController(), playerManager);
  326. }
  327. } else if (itemCollidedWith != null) {
  328. pickup(); // Attempt to pick up an item that Kenny is currently in-contact with
  329. } else if (!(hand instanceof Weapon)){
  330. System.out.println("Using hand");
  331. useHand();
  332. }
  333. }
  334.  
  335. handleInputAttacks();
  336. handleInputMovement();
  337. handleInputHotbar();
  338. }
  339. }
  340.  
  341. @Override
  342. public void moveHorizontal(boolean facingRight, float speed) {
  343. float applySpeed = walking ? speed/2 : speed;
  344. if (facingRight) {
  345. body.applyLinearImpulse(new Vector2(applySpeed, 0), body.getWorldCenter(), true);
  346. }
  347. else { // Facing left
  348. body.applyLinearImpulse(new Vector2(-applySpeed, 0), body.getWorldCenter(), true);
  349. }
  350. }
  351.  
  352. private void handleDash(){
  353. if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
  354. if (Gdx.input.isKeyPressed(Input.Keys.UP)){ // Dash right-up
  355. if (Gdx.input.isKeyJustPressed(Input.Keys.C))
  356. body.applyLinearImpulse(new Vector2(speed*200,speed*200), body.getWorldCenter(), true);
  357. } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)){ // Dash right-down
  358. if (Gdx.input.isKeyJustPressed(Input.Keys.C))
  359. body.applyLinearImpulse(new Vector2(speed*200,-speed*200), body.getWorldCenter(), true);
  360. } else { // Dash right
  361. if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
  362. body.applyLinearImpulse(new Vector2(speed*200, 0), body.getWorldCenter(), true);
  363. }
  364. }
  365. } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)){
  366. if (Gdx.input.isKeyPressed(Input.Keys.UP)){ // Dash left-up
  367. if (Gdx.input.isKeyJustPressed(Input.Keys.C))
  368. body.applyLinearImpulse(new Vector2(-speed*200,speed*200), body.getWorldCenter(), true);
  369. } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)){ // Dash left-down
  370. if (Gdx.input.isKeyJustPressed(Input.Keys.C))
  371. body.applyLinearImpulse(new Vector2(-speed*200,-speed*200), body.getWorldCenter(), true);
  372. } else { // Dash left
  373. if (Gdx.input.isKeyJustPressed(Input.Keys.C))
  374. body.applyLinearImpulse(new Vector2(-speed*200,0), body.getWorldCenter(), true);
  375. }
  376. } else if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyJustPressed(Input.Keys.C)){
  377. body.applyLinearImpulse(new Vector2(0,speed*200), body.getWorldCenter(), true);
  378. } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && Gdx.input.isKeyJustPressed(Input.Keys.C)){
  379. body.applyLinearImpulse(new Vector2(0,-speed*200), body.getWorldCenter(), true);
  380. } else {
  381. if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
  382. if (facingRight)
  383. body.applyLinearImpulse(new Vector2(speed * 200, 0), body.getWorldCenter(), true);
  384. else
  385. body.applyLinearImpulse(new Vector2(-speed * 200, 0), body.getWorldCenter(), true);
  386. }
  387. }
  388. }
  389.  
  390. /** Inventory operations **/
  391.  
  392. /**
  393. * Checks equippeditems array and unequips item if found
  394. * @param item The item to unequip from equippeditems
  395. */
  396. // FIX: Return item back to inventory or drop
  397. @Override
  398. public boolean unequipItem(Item item){
  399. if (isEquippedItemsFull())
  400. System.out.println("The equipped item list is empty");
  401. else {
  402. for (int i = 0; i < equippedItems.length; i++){
  403. if (equippedItems[i] == item){
  404. equippedItems[i] = null;
  405. equippedItemsCount--;
  406. System.out.println("Unequipped item");
  407. return true;
  408. }
  409. }
  410. System.out.println("That item does not exist in your equipped items to unequip");
  411. }
  412. return false;
  413.  
  414. }
  415.  
  416. @Override
  417. public boolean addInventoryItem(Item item) {
  418. return false;
  419. }
  420.  
  421. @Override
  422. public boolean removeInventoryItem(Item item) {
  423. return false;
  424. }
  425.  
  426. /** Graphics operations **/
  427.  
  428. /**
  429. * Respositions player texture when facing in a particular direction
  430. */
  431. // Solve issue: Kenny's image flip being incorrectly positioned
  432. @Override
  433. public void adjustOffsetPosition(){
  434. int directionModifier = facingRight ? -1 : 1;
  435.  
  436. if (currentState == State.IDLE || currentState == State.SWORDDRAW || currentState == State.CROUCHING || currentState == State.SLIDING || currentState == State.ATTACK1 || currentState == State.ATTACK2){
  437. if (facingRight)
  438. setPosition(body.getPosition().x - getWidth()/2, body.getPosition().y - getHeight()/2 + 3f/Searching.PPM);
  439. else // Facing left
  440. setPosition(body.getPosition().x - getWidth()/2, body.getPosition().y - getHeight()/2 + 3f/Searching.PPM);
  441. } else if (currentState == State.RUNNING || currentState == State.WALKING) {
  442. setPosition(body.getPosition().x - getWidth()/2 + 2*directionModifier, body.getPosition().y - getHeight()/2 + 4f/Searching.PPM);
  443. } else if (currentState == State.FALLING){
  444. setPosition(body.getPosition().x - getWidth()/2 + 1*directionModifier, body.getPosition().y - getHeight()/2 + .5f/Searching.PPM);
  445. } else { // Possibly running or jumping
  446. setPosition(body.getPosition().x - getWidth()/2, body.getPosition().y - getHeight()/2);
  447. }
  448. }
  449.  
  450. /**
  451. * Returns next frame of Kenny and play sounds if beginning certain animations (e.g. bow attack)
  452. * @param deltatime
  453. * @return Next frame of Kenny
  454. */
  455. public TextureRegion getFrame(float deltatime){
  456. currentState = getState();
  457. TextureRegion region;
  458. PolygonShape shape;
  459. increaseStateTimer(deltatime);
  460.  
  461. switch (currentState){
  462. case SWORDDRAW:
  463. region = animSwordDraw.getKeyFrame(stateTimer, false);
  464. if (animSwordDraw.isAnimationFinished(stateTimer))
  465. runAnimSwordDraw = false;
  466. break;
  467. case SLIDING:
  468. region = kennySlide.getKeyFrame(stateTimer, true);
  469. //setPosition(10,10);
  470. if (kennySlide.isAnimationFinished(stateTimer)){
  471. runKennySlide = false;
  472. }
  473. break;
  474. case DIE:
  475. region = animDie.getKeyFrame(stateTimer, false);
  476. if (animDie.isAnimationFinished(stateTimer))
  477. System.out.println("epic death");
  478. runanimDie = false;
  479. break;
  480. case PUNCH1:
  481. region = animPunch1.getKeyFrame(stateTimer, true);
  482. if (animPunch1.isAnimationFinished(stateTimer)){
  483. runanimPunch1 = false;
  484. }
  485. break;
  486. case PUNCH2:
  487. region = animPunch2.getKeyFrame(stateTimer, true);
  488. if (animPunch2.isAnimationFinished(stateTimer)){
  489. runanimPunch2 = false;
  490. }
  491. break;
  492. case ATTACK1:
  493. if (canSoundanimSword1){
  494. soundManager.get("audio/sound/swoosh_04.wav", Sound.class).play();
  495. canSoundanimSword1 = false;
  496. useHand();
  497. }
  498. region = animSword1.getKeyFrame(stateTimer, true);
  499. if (animSword1.isAnimationFinished(stateTimer)){
  500. canSoundanimSword1 = true;
  501. runanimSword1 = false;
  502. /*
  503. System.out.println(((Weapon) hand).getSwings().size);
  504. for (Swing swing : ((Weapon) hand).getSwings()){
  505. swing.destroy();
  506. }*/
  507.  
  508. }
  509. break;
  510. case ATTACK2:
  511. if (canSoundanimSword2){
  512. soundManager.get("audio/sound/swoosh_03.wav", Sound.class).play();
  513. canSoundanimSword2 = false;
  514. useHand();
  515. }
  516. region = animSword2.getKeyFrame(stateTimer, true);
  517. if (animSword2.isAnimationFinished(stateTimer)){
  518. canSoundanimSword2 = true;
  519. runanimSword2 = false;
  520. /*
  521. System.out.println(((Weapon) hand).getSwings().size);
  522. for (Swing swing : ((Weapon) hand).getSwings()){
  523. swing.destroy();
  524. }*/
  525. }
  526. break;
  527. case BOW2:
  528. region = animBow2.getKeyFrame(stateTimer, true);
  529. System.out.println(stateTimer + " of " + animBow2.getAnimationDuration());
  530. if (animBow2.isAnimationFinished(stateTimer)) {
  531. System.out.println("IT'S... OVER!");
  532. canSoundanimBow1 = true;
  533. runanimBow2 = false;
  534. useHand();
  535. }
  536. break;
  537. case BOW1:
  538. if (canSoundanimBow1) {
  539. soundManager.get("audio/sound/Bow3.wav", Sound.class).play();
  540. canSoundanimBow1 = false;
  541. }
  542. body.setLinearVelocity(0,0);
  543. region = animBow1.getKeyFrame(stateTimer, true);
  544. System.out.println(stateTimer + " of " + animBow1.getAnimationDuration());
  545. if (animBow1.isAnimationFinished(stateTimer)) {
  546. System.out.println("IT'S... OVER!");
  547. canSoundanimBow1 = true;
  548. runanimBow1 = false;
  549. useHand();
  550. }
  551. break;
  552. case CROUCHING:
  553. region = animCrouch.getKeyFrame(stateTimer, true);
  554.  
  555. shape = new PolygonShape();
  556. shape.setAsBox(14,5);
  557. break;
  558. case FALLING:
  559. region = kennyFall.getKeyFrame(stateTimer, true);
  560. break;
  561. case JUMPING:
  562. if (jumpCount == 2){
  563. region = kennySomersault.getKeyFrame(stateTimer, true);
  564. } else {
  565. region = animJump.getKeyFrame(stateTimer, false);
  566. }
  567. break;
  568. case RUNNING:
  569. if (hand instanceof Weapon) {
  570. if (((Weapon) hand).isMelee()) // If holding sword, run with sword
  571. region = animRun2.getKeyFrame(stateTimer, true);
  572. else // Otherwise run without sowrd
  573. region = animRun1.getKeyFrame(stateTimer, true);
  574. } else { // Otherwise run without sword
  575. region = animRun1.getKeyFrame(stateTimer, true);
  576. }
  577. break;
  578. case WALKING:
  579. region = animWalk.getKeyFrame(stateTimer, true);
  580. break;
  581. case IDLE:
  582. if (hand instanceof Weapon) {
  583. if (((Weapon) hand).isMelee())
  584. region = animIdle2.getKeyFrame(stateTimer, true);
  585. else
  586. region = animIdle1.getKeyFrame(stateTimer, true);
  587. } else {
  588. region = animIdle1.getKeyFrame(stateTimer, true);
  589. }
  590. break;
  591. default:
  592. region = texture;
  593. break;
  594. }
  595.  
  596. // flip kennys sprite when his direction is changed
  597. if ((body.getLinearVelocity().x < 0 || !facingRight) && !region.isFlipX()){
  598. region.flip(true, false);
  599. facingRight = false;
  600. } else if ((body.getLinearVelocity().x > 0 || facingRight) && region.isFlipX()){
  601. region.flip(true, false);
  602. facingRight = true;
  603. }
  604. // If kenny was running left (prevState) and he is still running (currentState), continue to increase stateTimer
  605. //if (currentState == State.BOW1){
  606. // System.out.println("BOW MODE");
  607. // stateTimer = stateTimer + deltatime;
  608. //} else {
  609. //stateTimer = (currentState == previousState) ? stateTimer + deltatime : 0;
  610. //}
  611. previousState = currentState;
  612. return region;
  613. }
  614.  
  615. /** Accessor and mutator operations **/
  616.  
  617. /**
  618. * Accessor for Kenny's current state
  619. * @return State of Kenny
  620. */
  621. public State getState(){
  622. if (hp <= 0){
  623. return State.DIE;
  624. } else if (runAnimSwordDraw && body.getLinearVelocity().x == 0 && body.getLinearVelocity().y == 0){
  625. return State.SWORDDRAW;
  626. } else if (runKennySlide && body.getLinearVelocity().y == 0){
  627. return State.SLIDING;
  628. } else if (runanimPunch1){
  629. return State.PUNCH1;
  630. } else if (runanimPunch2){
  631. return State.PUNCH2;
  632. } else if (runanimSword1){
  633. return State.ATTACK1;
  634. } else if (runanimSword2){
  635. return State.ATTACK2;
  636. } else if (runanimBow1){
  637. return State.BOW1;
  638. } else if (runanimBow2){
  639. return State.BOW2;
  640. }
  641. else if ((Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) && body.getLinearVelocity().x < 10 && body.getLinearVelocity().x > -10){
  642. return State.CROUCHING;
  643. }
  644. else if (body.getLinearVelocity().y > 0|| (body.getLinearVelocity().y < 0 && currentState == State.JUMPING && stateTimer < 2)){
  645. return State.JUMPING;
  646. }
  647. else if (body.getLinearVelocity().y < 0){
  648. // Allow kenny to somersault in stateTimer condition
  649. return State.FALLING;
  650. }
  651. // FIX Falling
  652. else if (body.getLinearVelocity().x != 0){
  653. if (walking)
  654. return State.WALKING;
  655. else
  656. return State.RUNNING;
  657. }
  658. else {
  659. return State.IDLE;
  660. }
  661. }
  662.  
  663. /**
  664. * Accessor for Kenny's direction
  665. * @return Kenny's direction; true if right
  666. */
  667. public boolean isFacingRight() {
  668. return facingRight;
  669. }
  670.  
  671. /**
  672. * Determines which light attack animation to perform (i.e. swordslash or punch)
  673. * @param performAnim Boolean - If the animation should be performed
  674. */
  675. public void setLightAttackAnimation(boolean performAnim){
  676. if (hand == null){
  677. runanimPunch1 = performAnim;
  678. } else { // Else hand not null
  679. if (hand instanceof Weapon){
  680. if (((Weapon) hand).isMelee()){
  681. // Has a sword
  682. runanimSword1 = performAnim;
  683. } else {
  684. // Has a bow
  685. runanimPunch1 = performAnim;
  686. }
  687. } else { // Else hand has item that's not a weapon
  688. runanimPunch1 = performAnim;
  689. }
  690. }
  691. }
  692.  
  693. /**
  694. * Determines which heavy attack animation to perform (i.e. swordslash or punch)
  695. * @param performAnim Boolean - If the animation should be performed
  696. */
  697. public void setHeavyAttackAnimation(boolean performAnim){
  698. if (hand == null){
  699. runanimPunch2 = performAnim;
  700. } else { // Else hand not null
  701. if (hand instanceof Weapon){
  702. if (((Weapon) hand).isMelee()){
  703. // Has a sword
  704. runanimSword2 = performAnim;
  705. } else {
  706. // Has a bow
  707. runanimPunch2 = performAnim;
  708. }
  709. } else { // Else hand has item that's not a weapon
  710. runanimPunch2 = performAnim;
  711. }
  712. }
  713. }
  714.  
  715. /**
  716. * Determine which bow animation is to be performed (mid-air or ground bow attack)
  717. * @param performAnim Boolean - If the animation should be performed
  718. */
  719. public void setBowAnimation(boolean performAnim, boolean isJumping){
  720. boolean hasBow = false;
  721.  
  722. // Determine if hand or inventory has a bow; if true, then kenny has a bow to fire
  723. if (hand instanceof Weapon) {
  724. if (!((Weapon) hand).isMelee())
  725. hasBow = true;
  726. }
  727. else {
  728. for (Item item : inventory){
  729. if (item instanceof Weapon)
  730. if (!((Weapon) item).isMelee())
  731. hasBow = true;
  732. }
  733.  
  734. }
  735.  
  736. // If has bow, carry out bow animation
  737. if (hasBow) {
  738. if (!isJumping)
  739. runanimBow1 = performAnim;
  740. else
  741. runanimBow2 = performAnim;
  742. }
  743. }
  744.  
  745. public void setJumpCount(int value){
  746. jumpCount = value;
  747. }
  748.  
  749. /**
  750. * Accessor for jumpcount; can only only double-jump and avoids 2+ jumps in one iteration
  751. * @return
  752. */
  753. public int getJumpCount(){
  754. return jumpCount;
  755. }
  756.  
  757. /**
  758. * Accessor for item currently in hand of player
  759. * @return
  760. */
  761. public Item getHand() {
  762. return hand;
  763. }
  764.  
  765. public void setKennySlide(boolean value) {
  766. runKennySlide = value;
  767. }
  768.  
  769. /**
  770. * Determines if Kenny has no more space within equippeditems array
  771. * @return True if full, otherwise false if not full
  772. */
  773. public boolean isEquippedItemsFull(){
  774. if (equippedItemsCount == equippedItems.length)
  775. return true;
  776. return false;
  777. }
  778.  
  779. public int getHunger(){
  780. return hunger;
  781. }
  782.  
  783. public void setHunger(int value){
  784. hunger = value;
  785. }
  786.  
  787. /** Input operations **/
  788.  
  789. /** Input handler operations
  790. * - May only perform required behaviour if kenny is visible
  791. */
  792.  
  793. private void handleInputAttacks() {
  794. if (Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT) || Gdx.input.isKeyPressed(Input.Keys.SHIFT_RIGHT)){
  795. if (Gdx.input.isKeyJustPressed(Input.Keys.J) && this.getBody().getLinearVelocity().x <= 150 && this.getBody().getLinearVelocity().y <= 5 && this.getBody().getLinearVelocity().y >= -5){
  796. this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
  797. this.setHeavyAttackAnimation(true);
  798. System.out.println("ATTACK2");
  799. }
  800. } else if ((Gdx.input.isKeyJustPressed(Input.Keys.J)) && this.getBody().getLinearVelocity().x <= 150 && this.getBody().getLinearVelocity().y <= 5 && this.getBody().getLinearVelocity().y >= -5) {
  801. this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
  802. this.setLightAttackAnimation(true);
  803. //this.setanimSword1(true);
  804. System.out.println("ATTACK1");
  805. } else if ((Gdx.input.isKeyJustPressed(Input.Keys.L)) && this.getBody().getLinearVelocity().x <= 150) {
  806. // Mid-air bow attack
  807. if (this.getBody().getLinearVelocity().y > -1 && this.getBody().getLinearVelocity().y <= 1) {
  808. this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
  809. this.setBowAnimation(true, false);
  810. //this.playthisSound(Kenny.State.BOW1);
  811. System.out.println("BOW1");
  812. } else { // Ground bow attack
  813. this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
  814. this.setBowAnimation(true, true);
  815. System.out.println("BOW2");
  816. }
  817. }
  818. }
  819.  
  820. private void handleInputMovement() {
  821. handleDash();
  822. if (Gdx.input.isKeyJustPressed(Input.Keys.M))
  823. walking = !walking;
  824.  
  825. if (Gdx.input.isKeyJustPressed(Input.Keys.UP) || Gdx.input.isKeyJustPressed(Input.Keys.W)) {
  826. jump();
  827. }
  828.  
  829. //If down and right is pressed then Slide right
  830. if ((Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.isKeyPressed(Input.Keys.D)) && this.getBody().getLinearVelocity().x <= 150) {
  831. if ((Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) && (this.getBody().getLinearVelocity().x >= 0)) {
  832. this.setKennySlide(true);
  833. //System.out.println("SLIDE");
  834. } else {
  835. moveHorizontal(true, speed);
  836. //System.out.println("RIGHT");
  837. }
  838. }
  839. //If down and left is pressed then slide left
  840. if ((Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.isKeyPressed(Input.Keys.A)) && this.getBody().getLinearVelocity().x <= 150) {
  841. if ((Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) && this.getBody().getLinearVelocity().x <= -0) {
  842. this.setKennySlide(true);
  843. //System.out.println("SLIDE");
  844. } else {
  845. moveHorizontal(false, speed);
  846. //System.out.println("LEFT");
  847. }
  848.  
  849. }
  850. //If only down is pressed the crouch
  851. if ((Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) && this.getBody().getLinearVelocity().x <= 150) {
  852. this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
  853. //System.out.println("CROUCH");
  854. }
  855. }
  856.  
  857. private void jump(){
  858. if (jumpCount + 1 <= 2){ // Max Jump count: 2
  859. jumpCount++;
  860. this.body.applyLinearImpulse(new Vector2(0, 100), body.getWorldCenter(), true);
  861. sound = soundManager.get("audio/sound/swosh_01.wav", Sound.class);
  862. sound.play(0.5f);
  863. }
  864. }
  865.  
  866. /**
  867. * Detect if inventory indexes 1-9 is selected and transfer to hand
  868. */
  869. public void handleInputHotbar(){
  870. if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_1)){
  871. inventoryToHand(0);
  872. } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)){
  873. inventoryToHand(1);
  874. } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_3)){
  875. inventoryToHand(2);
  876. } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_4)){
  877. inventoryToHand(3);
  878. } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_5)){
  879. inventoryToHand(4);
  880. } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_6)){
  881. inventoryToHand(5);
  882. } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_7)){
  883. inventoryToHand(6);
  884. } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_8)){
  885. inventoryToHand(7);
  886. } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_9)){
  887. inventoryToHand(8);
  888. }
  889. }
  890.  
  891. /**
  892. * If the item added to the hand was a sword, perform sword draw
  893. * @param item The item to add to the hand/inventory
  894. */
  895. @Override
  896. protected void addToBody(Item item){
  897. if (item instanceof Note){
  898. item.playPickupSound(); // Play sound
  899. item.use(this);
  900. removeFromWorld(item, screen.getCreator());
  901. playerManager.getNoteManager().addNote((Note) item); // Add note to the player's collection
  902. playerManager.setCurrentMode(PlayerManager.Mode.NOTE);
  903. System.out.println("Item added to note inventory");
  904. } else if (item.isCureItem()){
  905. item.playPickupSound();
  906. removeFromWorld(item, screen.getCreator());
  907. playerManager.addCureItem(item); // Add the cure item to the cure-item collection
  908. System.out.println("Item added to cure inventory");
  909. } else { // regular item
  910. if (hand == null){
  911. hand = item;
  912. item.playPickupSound();
  913. removeFromWorld(item, screen.getCreator());
  914. System.out.println("Item added to hand");
  915. if (item instanceof Weapon)
  916. if (((Weapon) item).isMelee())
  917. runAnimSwordDraw = true;
  918. } else { // hand if full, add to inventory
  919. for (int i = 0; i < inventory.length; i++){
  920. if (inventory[i] == null){ // Add item to the first null element
  921. inventory[i] = item;
  922. item.playPickupSound();
  923. removeFromWorld(item, screen.getCreator());
  924. System.out.println("Item added to inventory");
  925. break;
  926. }
  927. }
  928. }
  929. }
  930. }
  931.  
  932. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement