/** * Handles the keyboard input. * * @param elapsed time elapsed since the last frame */ private void handleControllerInput(float elapsed) { if (controllerActive) { if (controller.justPressed(KeyEvent.VK_A)) { simpleAttack(); } if (state == DynamicObjectState.Attacking) { curAnim.update(elapsed); return; } else if (controller.justPressed(KeyEvent.VK_S)) spellAttack(0); else if (controller.justPressed(KeyEvent.VK_D)) spellAttack(1); else if (controller.justPressed(KeyEvent.VK_F)) spellAttack(2); else if (controller.justPressed(KeyEvent.VK_5)) spellManager.activateShield(ElementType.Fire); else if (controller.justPressed(KeyEvent.VK_6)) spellManager.activateShield(ElementType.Water); else if (controller.justPressed(KeyEvent.VK_7)) spellManager.activateShield(ElementType.Earth); else if (controller.isDownPressed()) move(Heading.Down); else if (controller.isUpPressed()) move(Heading.Up); else if (controller.isLeftPressed()) move(Heading.Left); else if (controller.isRightPressed()) move(Heading.Right); else if (controller.justReleased(KeyEvent.VK_LEFT) || controller.justReleased(KeyEvent.VK_RIGHT) || controller.justReleased(KeyEvent.VK_UP) || controller.justReleased(KeyEvent.VK_DOWN)) stopMovement(); else if (controller.justPressed(KeyEvent.VK_ENTER)) collision.checkOnKeyTriggers(this); } }
@Override protected BufferedImage getTexture() { return curAnim.getTexture(); }
@Override public void update(float elapsed) { if (!isInNetwork && health.isDead()) { terminate(); return; } regenerate(); spellManager.update(elapsed); ElementalShield curShield = spellManager.getCurrentShield(); if (curShield != null && curShield.getElementType() == ElementType.Fire) manager.handleAreaOfEffectSpell( this, curShield.getDamage(), ElementType.Fire, curShield.getAOErect()); if (state == DynamicObjectState.Attacking) { curAnim.update(elapsed); return; } if (state == DynamicObjectState.Hit) { curAnim.update(elapsed); curHitDuration -= 1; if (curHitDuration <= 0) { setState(DynamicObjectState.Idle); curHitDuration = maxHitDuration; health.setInvul(false); } return; } if (controllerActive) handleControllerInput(elapsed); // DEBUG PURPOSE if (controllerActive) { if (controller.isPressed(KeyEvent.VK_SHIFT)) supressEnemyCollision = true; else supressEnemyCollision = false; } // ------------- if (velocity.x > 0.01f || velocity.x < -0.01f || velocity.y > 0.01f || velocity.y < -0.01f) { collisionRect.x += velocity.x; collisionRect.y += velocity.y; boolean collidingStatic = collision.isCollidingStatic(this); boolean collidingDynamic = false; if (!supressEnemyCollision) collidingDynamic = collision.isCollidingDynamic(this); if (collidingStatic || collidingDynamic) { collisionRect.x -= velocity.x; collisionRect.y -= velocity.y; stopMovement(); } else { collision.checkTriggers(this); screenPosition.x += velocity.x; screenPosition.y += velocity.y; curAnim.update(elapsed); } } if (isInNetwork && controllerActive) { sendPositionMessage(); } }
/** Initializes all animations for the player. */ private void initAnimations() { // Setup Animations try { BufferedImage wl, wr, wu, wd; wl = ImageIO.read(new File("res/plWleft.png")); wr = ImageIO.read(new File("res/plWright.png")); wd = ImageIO.read(new File("res/plWdown.png")); wu = ImageIO.read(new File("res/plWup.png")); int frameDuration = 150; Animation aWalkLeft = Animation.createWalkingAnimation(wl, frameDuration); Animation aWalkRight = Animation.createWalkingAnimation(wr, frameDuration); Animation aWalkDown = Animation.createWalkingAnimation(wd, frameDuration); Animation aWalkUp = Animation.createWalkingAnimation(wu, frameDuration); walkAnim = new HashMap<Heading, Animation>(); walkAnim.put(Heading.Left, aWalkLeft); walkAnim.put(Heading.Right, aWalkRight); walkAnim.put(Heading.Down, aWalkDown); walkAnim.put(Heading.Up, aWalkUp); BufferedImage[] iLeft = {aWalkLeft.getTextureByFrame(1)}; BufferedImage[] iRight = {aWalkRight.getTextureByFrame(1)}; BufferedImage[] iDown = {aWalkDown.getTextureByFrame(1)}; BufferedImage[] iUp = {aWalkUp.getTextureByFrame(1)}; idleAnim = new HashMap<Heading, Animation>(); idleAnim.put(Heading.Left, new Animation(iLeft, 1)); idleAnim.put(Heading.Right, new Animation(iRight, 1)); idleAnim.put(Heading.Down, new Animation(iDown, 1)); idleAnim.put(Heading.Up, new Animation(iUp, 1)); // Only for debugging. simpleAttackAnim = new HashMap<Heading, Animation>(); BufferedImage[] iSALeft = new BufferedImage[1]; iSALeft[0] = new BufferedImage(32, 32, iLeft[0].getType()); Graphics2D g2 = (Graphics2D) iSALeft[0].getGraphics(); g2.setColor(new Color(0.0f, 0.0f, 1.0f)); g2.fillRect(0, 0, 32, 32); simpleAttackAnim.put(Heading.Left, new Animation(iSALeft, 1)); BufferedImage[] iSARight = new BufferedImage[1]; iSARight[0] = new BufferedImage(32, 32, iLeft[0].getType()); g2 = (Graphics2D) iSARight[0].getGraphics(); g2.setColor(new Color(1.0f, 0.0f, 0.0f)); g2.fillRect(0, 0, 32, 32); simpleAttackAnim.put(Heading.Right, new Animation(iSARight, 1)); BufferedImage[] iSAUp = new BufferedImage[1]; iSAUp[0] = new BufferedImage(32, 32, iLeft[0].getType()); g2 = (Graphics2D) iSAUp[0].getGraphics(); g2.setColor(new Color(0.0f, 1.0f, 0.0f)); g2.fillRect(0, 0, 32, 32); simpleAttackAnim.put(Heading.Up, new Animation(iSAUp, 1)); BufferedImage[] iSADown = new BufferedImage[1]; iSADown[0] = new BufferedImage(32, 32, iLeft[0].getType()); g2 = (Graphics2D) iSADown[0].getGraphics(); g2.setColor(new Color(1.0f, 0.0f, 1.0f)); g2.fillRect(0, 0, 32, 32); simpleAttackAnim.put(Heading.Down, new Animation(iSADown, 1)); HashMap<Integer, Rectangle> atRects = new HashMap<Integer, Rectangle>(); atRects.put(0, new Rectangle(-16, 0, 16, 32)); int frameDur = 44; simpleAttack = new Attack(15, simpleAttackAnim, atRects, frameDur, this); curAnim = idleAnim.get(heading); } catch (IOException e) { System.err.print("Couldn't load Players texture!"); System.exit(1); } }