Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.teamnine.game.Model;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.assets.AssetManager;
- import com.badlogic.gdx.audio.Sound;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Animation;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- import com.badlogic.gdx.utils.Array;
- import com.teamnine.game.Entity.Attacks.Swing;
- import com.teamnine.game.Entity.Attacks.Weapon;
- import com.teamnine.game.Entity.Item;
- import com.teamnine.game.Entity.Note;
- import com.teamnine.game.Map.Location;
- import com.teamnine.game.Model.AbstractPlayer;
- import com.teamnine.game.Model.NPC.Npc;
- import com.teamnine.game.Player.PlayerManager;
- import com.teamnine.game.Screen.Play;
- import com.teamnine.game.Searching;
- /**
- * Models the kenny actor
- */
- public class Kenny extends AbstractPlayer implements Animatable{
- // Kenny stats variables
- private Item[] equippedItems;
- private int equippedItemsCount;
- private boolean walking;
- protected Stage stage;
- // Track kenny's state
- public enum State{IDLE, WALKING, RUNNING, JUMPING, DEAD, CROUCHING, SLIDING, ATTACK1, ATTACK2, FALLING, BOW1, BOW2, PUNCH1, PUNCH2, DIE, SWORDDRAW, SWORDSHEATH};
- private State currentState, previousState;
- private int jumpCount; // Max is double-jump; prevent extra jumps
- // Check which world screen kenny is in (i.e. menu or play)
- //private boolean playscreen;
- // Animations
- private Animation<TextureRegion> animIdle1, animIdle2; // Idle 1: without sword, Idle 2: with sword
- private Animation<TextureRegion> animRun1, animRun2, animWalk; // Run 1: without sword, Run 2: with sword
- private Animation<TextureRegion> animJump, animCrouch, animDie, animCrouchWalk;
- private Animation<TextureRegion> animPunch1, animPunch2;
- private Animation<TextureRegion> animSword1, animSword2;
- private Animation<TextureRegion> animSwordDraw, animSwordSheath;
- private Animation<TextureRegion> animBow1, animBow2;
- private boolean runAnimSwordDraw, runAnimSwordSheath;
- private boolean runanimPunch1, runanimPunch2, runanimSword1, canSoundanimSword2, runanimSword2, runanimDie, canSoundanimSword1, runanimBow1, canSoundanimBow1, runanimBow2;
- private Animation<TextureRegion> kennySomersault;
- private Animation<TextureRegion> kennySlide;
- private boolean runKennySlide;
- private Animation<TextureRegion> kennyFall;
- // Music related
- private AssetManager soundManager;
- private Sound sound;
- private Array<Sound> animRunSounds;
- // Records time in a specific state
- private float stateTimer;
- public Kenny(PlayerManager playerManager, Play screen, Location location, Vector2 fixtureSize, int inventorySize) {
- super(playerManager, screen, location, fixtureSize, inventorySize);
- soundManager = screen.getGame().getSoundAssetManager();
- generateAnimations();
- initStats(); // initialise kenny's stats
- stateTimer = 0;
- facingRight = true;
- currentState = previousState = State.IDLE;
- walking = true;
- equippedItems = new Item[10];
- equippedItemsCount = 0;
- // Display kenny
- texture = new TextureRegion(((Play)screen).getActorAtlas().findRegion("adventurer-idle-00"), 0,0,50,37);
- texture.getTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
- // Defining actor
- fixtureSize = new Vector2(6,14);
- defineActor(fixtureSize.x, fixtureSize.y);
- setBounds(0,0,50 / Searching.PPM, 37 / Searching.PPM);
- setRegion(texture);
- }
- @Override
- public void generateAnimations() {
- // Animation
- Array<TextureRegion> frames = new Array<TextureRegion>();
- for (int i = 0; i < 4; i++) // Idle 1 - without sword
- frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_idle"), i*50,0, 50, 37));
- animIdle1 = new Animation<TextureRegion>(0.3f, frames);
- frames.clear();
- for (int i = 0; i < 4; i++) // Idle 2 - with sword
- frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_idle2"), i*50, 0, 50, 37));
- animIdle2 = new Animation<TextureRegion>(0.3f, frames);
- frames.clear();
- for (int i = 0; i < 6; i++) // Run 1 - without sword
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_run"),0 , i*37, 50, 37));
- animRun1 = new Animation(0.1f, frames);
- frames.clear();
- for (int i = 0; i < 6; i++)
- frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_run2"), i*50, 0, 50, 37));
- animRun2 = new Animation<TextureRegion>(0.1f, frames);
- frames.clear();
- for (int i = 0; i < 4; i++) // Jump
- frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_jump"), i*50, 0, 50, 37));
- animJump = new Animation(0.02f, frames);
- frames.clear(); // Crouch
- for (int i = 0; i < 4; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_crouch"), i*50, 0, 50, 37));
- animCrouch = new Animation(0.25f, frames);
- frames.clear(); // Sword slash 1 - light
- for (int i = 0; i < 6; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_attack2"), 0, i*37, 50, 37));
- animSword1 = new Animation<TextureRegion>(0.1f, frames);
- runanimSword1 = false; canSoundanimSword1 = true;
- frames.clear(); // Sword slash 2 - heavy
- for (int i = 0; i < 5; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_attack1"), i*50, 0, 50, 37));
- animSword2 = new Animation<TextureRegion>(0.1f, frames);
- runanimSword2 = false; canSoundanimSword2 = true;
- frames.clear(); // Bow on-ground
- for (int i = 0; i < 9; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_bow"), i*50, 0, 50, 37));
- animBow1 = new Animation<TextureRegion>(0.1f, frames);
- runanimBow1 = false; canSoundanimBow1 = true;
- frames.clear(); // Bow mid-air
- for (int i = 0; i < 6; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_bow_jump"), 0, i*37, 50, 37));
- animBow2 = new Animation<TextureRegion>(0.1f, frames);
- runanimBow2 = false;
- frames.clear(); // Somersault
- for (int i = 0; i < 4; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_smrslt"), i*50, 0, 50, 37));
- kennySomersault = new Animation<TextureRegion>(0.1f, frames);
- frames.clear(); // Sliding
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer-slide-00"), 0,0,50,37));
- kennySlide = new Animation<TextureRegion>(0.1f, frames);
- runKennySlide = false;
- frames.clear(); // Falling
- for (int i = 0; i < 2; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_fall"), i*50, 0, 50, 37));
- kennyFall = new Animation<TextureRegion>(0.1f, frames);
- frames.clear(); // Dying
- for (int i = 0; i < 6; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_die"), i*50, 0, 50, 37));
- animDie = new Animation<TextureRegion>(0.1f, frames);
- frames.clear(); // Punch 1 - light
- for (int i = 0; i < 4; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_punch"), i*50, 0, 50, 37));
- animPunch1 = new Animation<TextureRegion>(0.1f, frames);
- runanimPunch1 = false;
- frames.clear(); // Punch 2 - heavy
- for (int i = 0; i < 7; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_run_punch"), i*50, 0, 50, 37));
- animPunch2 = new Animation<TextureRegion>(0.1f, frames);
- runanimPunch2 = false;
- frames.clear(); // Crouch walking
- for (int i = 0; i < 6; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_crouch_walk"), 0, i*37, 50, 37));
- animCrouchWalk = new Animation<TextureRegion>(0.1f, frames);
- frames.clear(); // Walking
- for (int i = 0; i < 6; i++)
- frames.add(new TextureRegion(((Play) screen).getActorAtlas().findRegion("adventurer_walk"), 0, i*37, 50, 37));
- animWalk = new Animation<TextureRegion>(0.1f, frames);
- frames.clear(); // Sword draw
- for (int i = 0; i < 4; i++)
- frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_draw"), i*50, 0, 50, 37));
- animSwordDraw = new Animation<TextureRegion>(0.1f, frames);
- frames.clear();
- for (int i = 0; i < 4; i++) // Sword sheath
- frames.add(new TextureRegion(screen.getActorAtlas().findRegion("adventurer_shte"), i*50, 0, 50, 37));
- animSwordSheath = new Animation<TextureRegion>(0.1f, frames);
- }
- /**
- * Initialises Kenny's stat variables
- */
- @Override
- protected void initStats(){
- // Set kenny stats and inventory
- jumpCount = 0;
- hp = basehp = 100;
- mana = baseMana = 100;
- attack = baseattack = 10;
- defence = basedefence = 10;
- hunger = 100;
- }
- public void defineActor(float hx, float hy) {
- BodyDef bodydef = new BodyDef();
- bodydef.position.set(location.getX(), location.getY());
- // Setting to DynamicBody allows to interact with environment/objects
- bodydef.type = BodyDef.BodyType.DynamicBody;
- // Create body in level
- body = world.createBody(bodydef);
- // Fixtures are physical attributes (check libgdx doc) - here we're defining the fixture
- //CircleShape shape = new CircleShape();
- //shape.setRadius(7);
- PolygonShape shape = new PolygonShape();
- shape.setAsBox(hx / Searching.PPM,hy / Searching.PPM); // Kenny size
- FixtureDef fixturedef = new FixtureDef();
- fixturedef.shape = shape;
- // Setting categorybit for collision
- fixturedef.filter.categoryBits = Searching.ACTOR_BIT;
- fixturedef.density *= 2;
- fixturedef.restitution = 0;
- body.createFixture(fixturedef).setUserData(this);
- }
- /** Game logic operations **/
- public void setDead() {
- setToDie = true;
- currentState = State.DIE;
- }
- public void increaseStateTimer(float deltatime){
- //System.out.println(stateTimer);
- switch (currentState){
- case BOW1:
- stateTimer = (animBow1.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
- break;
- case BOW2:
- stateTimer = (animBow2.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
- break;
- case ATTACK1:
- stateTimer = (animSword1.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
- break;
- case ATTACK2:
- stateTimer = (animSword2.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
- break;
- case PUNCH1:
- stateTimer = (animPunch1.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
- break;
- case PUNCH2:
- stateTimer = (animPunch2.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
- break;
- case DIE:
- stateTimer = (animDie.isAnimationFinished(stateTimer) ? 0 : stateTimer + deltatime);
- break;
- default:
- stateTimer = (currentState == previousState) ? stateTimer + deltatime : 0;
- break;
- }
- }
- /**
- * Update any changes made to kenny's behaviour
- * @param dt Delta time
- */
- public void update(float dt, String mapName){
- super.update(dt, mapName);
- if (active){
- if (currentState == State.DEAD){
- } else {
- handleInput();
- if (setToDestroy && !destroyed){
- // Destroy enemy
- stateTimer = 0;
- world.destroyBody(body);
- destroyed = true; // possibly remove this line
- setToDestroy = false;
- return;
- }
- if (this.body.getLinearVelocity().y == 0){
- jumpCount = 0;
- }
- location.set(body.getPosition().x, body.getPosition().y);
- setRegion(getFrame(dt));
- getInventoryCount(); // Update inventory count
- if (hand instanceof Weapon){
- for (Swing swing : ((Weapon) hand).getSwings())
- swing.update(dt);
- }
- }
- }
- adjustOffsetPosition();
- }
- @Override
- public void handleInput() {
- if (playerManager.getCurrentMode() == PlayerManager.Mode.PLAY){
- if (Gdx.input.isKeyJustPressed(Input.Keys.Q)) { // Check for Quest, else switch inventory[0] with hand
- if (actorCollidedWith != null){
- if (actorCollidedWith instanceof Npc){
- ((Npc) actorCollidedWith).startQuestConfirmationDialogue((screen).getDialogueController(), playerManager);
- }
- } else {
- this.switchHand();
- }
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.E)) { // Check for Npc interaction, else pickup item
- if (actorCollidedWith != null){
- if (actorCollidedWith instanceof Npc){
- if (((Npc) actorCollidedWith).hasDialogue())
- //npc.startDialogue(screen.getDialogueController(), kenny);
- ((Npc) actorCollidedWith).startDialogue((screen).getDialogueController(), playerManager);
- }
- } else if (itemCollidedWith != null) {
- pickup(); // Attempt to pick up an item that Kenny is currently in-contact with
- } else if (!(hand instanceof Weapon)){
- System.out.println("Using hand");
- useHand();
- }
- }
- handleInputAttacks();
- handleInputMovement();
- handleInputHotbar();
- }
- }
- @Override
- public void moveHorizontal(boolean facingRight, float speed) {
- float applySpeed = walking ? speed/2 : speed;
- if (facingRight) {
- body.applyLinearImpulse(new Vector2(applySpeed, 0), body.getWorldCenter(), true);
- }
- else { // Facing left
- body.applyLinearImpulse(new Vector2(-applySpeed, 0), body.getWorldCenter(), true);
- }
- }
- private void handleDash(){
- if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
- if (Gdx.input.isKeyPressed(Input.Keys.UP)){ // Dash right-up
- if (Gdx.input.isKeyJustPressed(Input.Keys.C))
- body.applyLinearImpulse(new Vector2(speed*200,speed*200), body.getWorldCenter(), true);
- } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)){ // Dash right-down
- if (Gdx.input.isKeyJustPressed(Input.Keys.C))
- body.applyLinearImpulse(new Vector2(speed*200,-speed*200), body.getWorldCenter(), true);
- } else { // Dash right
- if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
- body.applyLinearImpulse(new Vector2(speed*200, 0), body.getWorldCenter(), true);
- }
- }
- } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)){
- if (Gdx.input.isKeyPressed(Input.Keys.UP)){ // Dash left-up
- if (Gdx.input.isKeyJustPressed(Input.Keys.C))
- body.applyLinearImpulse(new Vector2(-speed*200,speed*200), body.getWorldCenter(), true);
- } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)){ // Dash left-down
- if (Gdx.input.isKeyJustPressed(Input.Keys.C))
- body.applyLinearImpulse(new Vector2(-speed*200,-speed*200), body.getWorldCenter(), true);
- } else { // Dash left
- if (Gdx.input.isKeyJustPressed(Input.Keys.C))
- body.applyLinearImpulse(new Vector2(-speed*200,0), body.getWorldCenter(), true);
- }
- } else if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyJustPressed(Input.Keys.C)){
- body.applyLinearImpulse(new Vector2(0,speed*200), body.getWorldCenter(), true);
- } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && Gdx.input.isKeyJustPressed(Input.Keys.C)){
- body.applyLinearImpulse(new Vector2(0,-speed*200), body.getWorldCenter(), true);
- } else {
- if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
- if (facingRight)
- body.applyLinearImpulse(new Vector2(speed * 200, 0), body.getWorldCenter(), true);
- else
- body.applyLinearImpulse(new Vector2(-speed * 200, 0), body.getWorldCenter(), true);
- }
- }
- }
- /** Inventory operations **/
- /**
- * Checks equippeditems array and unequips item if found
- * @param item The item to unequip from equippeditems
- */
- // FIX: Return item back to inventory or drop
- @Override
- public boolean unequipItem(Item item){
- if (isEquippedItemsFull())
- System.out.println("The equipped item list is empty");
- else {
- for (int i = 0; i < equippedItems.length; i++){
- if (equippedItems[i] == item){
- equippedItems[i] = null;
- equippedItemsCount--;
- System.out.println("Unequipped item");
- return true;
- }
- }
- System.out.println("That item does not exist in your equipped items to unequip");
- }
- return false;
- }
- @Override
- public boolean addInventoryItem(Item item) {
- return false;
- }
- @Override
- public boolean removeInventoryItem(Item item) {
- return false;
- }
- /** Graphics operations **/
- /**
- * Respositions player texture when facing in a particular direction
- */
- // Solve issue: Kenny's image flip being incorrectly positioned
- @Override
- public void adjustOffsetPosition(){
- int directionModifier = facingRight ? -1 : 1;
- if (currentState == State.IDLE || currentState == State.SWORDDRAW || currentState == State.CROUCHING || currentState == State.SLIDING || currentState == State.ATTACK1 || currentState == State.ATTACK2){
- if (facingRight)
- setPosition(body.getPosition().x - getWidth()/2, body.getPosition().y - getHeight()/2 + 3f/Searching.PPM);
- else // Facing left
- setPosition(body.getPosition().x - getWidth()/2, body.getPosition().y - getHeight()/2 + 3f/Searching.PPM);
- } else if (currentState == State.RUNNING || currentState == State.WALKING) {
- setPosition(body.getPosition().x - getWidth()/2 + 2*directionModifier, body.getPosition().y - getHeight()/2 + 4f/Searching.PPM);
- } else if (currentState == State.FALLING){
- setPosition(body.getPosition().x - getWidth()/2 + 1*directionModifier, body.getPosition().y - getHeight()/2 + .5f/Searching.PPM);
- } else { // Possibly running or jumping
- setPosition(body.getPosition().x - getWidth()/2, body.getPosition().y - getHeight()/2);
- }
- }
- /**
- * Returns next frame of Kenny and play sounds if beginning certain animations (e.g. bow attack)
- * @param deltatime
- * @return Next frame of Kenny
- */
- public TextureRegion getFrame(float deltatime){
- currentState = getState();
- TextureRegion region;
- PolygonShape shape;
- increaseStateTimer(deltatime);
- switch (currentState){
- case SWORDDRAW:
- region = animSwordDraw.getKeyFrame(stateTimer, false);
- if (animSwordDraw.isAnimationFinished(stateTimer))
- runAnimSwordDraw = false;
- break;
- case SLIDING:
- region = kennySlide.getKeyFrame(stateTimer, true);
- //setPosition(10,10);
- if (kennySlide.isAnimationFinished(stateTimer)){
- runKennySlide = false;
- }
- break;
- case DIE:
- region = animDie.getKeyFrame(stateTimer, false);
- if (animDie.isAnimationFinished(stateTimer))
- System.out.println("epic death");
- runanimDie = false;
- break;
- case PUNCH1:
- region = animPunch1.getKeyFrame(stateTimer, true);
- if (animPunch1.isAnimationFinished(stateTimer)){
- runanimPunch1 = false;
- }
- break;
- case PUNCH2:
- region = animPunch2.getKeyFrame(stateTimer, true);
- if (animPunch2.isAnimationFinished(stateTimer)){
- runanimPunch2 = false;
- }
- break;
- case ATTACK1:
- if (canSoundanimSword1){
- soundManager.get("audio/sound/swoosh_04.wav", Sound.class).play();
- canSoundanimSword1 = false;
- useHand();
- }
- region = animSword1.getKeyFrame(stateTimer, true);
- if (animSword1.isAnimationFinished(stateTimer)){
- canSoundanimSword1 = true;
- runanimSword1 = false;
- /*
- System.out.println(((Weapon) hand).getSwings().size);
- for (Swing swing : ((Weapon) hand).getSwings()){
- swing.destroy();
- }*/
- }
- break;
- case ATTACK2:
- if (canSoundanimSword2){
- soundManager.get("audio/sound/swoosh_03.wav", Sound.class).play();
- canSoundanimSword2 = false;
- useHand();
- }
- region = animSword2.getKeyFrame(stateTimer, true);
- if (animSword2.isAnimationFinished(stateTimer)){
- canSoundanimSword2 = true;
- runanimSword2 = false;
- /*
- System.out.println(((Weapon) hand).getSwings().size);
- for (Swing swing : ((Weapon) hand).getSwings()){
- swing.destroy();
- }*/
- }
- break;
- case BOW2:
- region = animBow2.getKeyFrame(stateTimer, true);
- System.out.println(stateTimer + " of " + animBow2.getAnimationDuration());
- if (animBow2.isAnimationFinished(stateTimer)) {
- System.out.println("IT'S... OVER!");
- canSoundanimBow1 = true;
- runanimBow2 = false;
- useHand();
- }
- break;
- case BOW1:
- if (canSoundanimBow1) {
- soundManager.get("audio/sound/Bow3.wav", Sound.class).play();
- canSoundanimBow1 = false;
- }
- body.setLinearVelocity(0,0);
- region = animBow1.getKeyFrame(stateTimer, true);
- System.out.println(stateTimer + " of " + animBow1.getAnimationDuration());
- if (animBow1.isAnimationFinished(stateTimer)) {
- System.out.println("IT'S... OVER!");
- canSoundanimBow1 = true;
- runanimBow1 = false;
- useHand();
- }
- break;
- case CROUCHING:
- region = animCrouch.getKeyFrame(stateTimer, true);
- shape = new PolygonShape();
- shape.setAsBox(14,5);
- break;
- case FALLING:
- region = kennyFall.getKeyFrame(stateTimer, true);
- break;
- case JUMPING:
- if (jumpCount == 2){
- region = kennySomersault.getKeyFrame(stateTimer, true);
- } else {
- region = animJump.getKeyFrame(stateTimer, false);
- }
- break;
- case RUNNING:
- if (hand instanceof Weapon) {
- if (((Weapon) hand).isMelee()) // If holding sword, run with sword
- region = animRun2.getKeyFrame(stateTimer, true);
- else // Otherwise run without sowrd
- region = animRun1.getKeyFrame(stateTimer, true);
- } else { // Otherwise run without sword
- region = animRun1.getKeyFrame(stateTimer, true);
- }
- break;
- case WALKING:
- region = animWalk.getKeyFrame(stateTimer, true);
- break;
- case IDLE:
- if (hand instanceof Weapon) {
- if (((Weapon) hand).isMelee())
- region = animIdle2.getKeyFrame(stateTimer, true);
- else
- region = animIdle1.getKeyFrame(stateTimer, true);
- } else {
- region = animIdle1.getKeyFrame(stateTimer, true);
- }
- break;
- default:
- region = texture;
- break;
- }
- // flip kennys sprite when his direction is changed
- if ((body.getLinearVelocity().x < 0 || !facingRight) && !region.isFlipX()){
- region.flip(true, false);
- facingRight = false;
- } else if ((body.getLinearVelocity().x > 0 || facingRight) && region.isFlipX()){
- region.flip(true, false);
- facingRight = true;
- }
- // If kenny was running left (prevState) and he is still running (currentState), continue to increase stateTimer
- //if (currentState == State.BOW1){
- // System.out.println("BOW MODE");
- // stateTimer = stateTimer + deltatime;
- //} else {
- //stateTimer = (currentState == previousState) ? stateTimer + deltatime : 0;
- //}
- previousState = currentState;
- return region;
- }
- /** Accessor and mutator operations **/
- /**
- * Accessor for Kenny's current state
- * @return State of Kenny
- */
- public State getState(){
- if (hp <= 0){
- return State.DIE;
- } else if (runAnimSwordDraw && body.getLinearVelocity().x == 0 && body.getLinearVelocity().y == 0){
- return State.SWORDDRAW;
- } else if (runKennySlide && body.getLinearVelocity().y == 0){
- return State.SLIDING;
- } else if (runanimPunch1){
- return State.PUNCH1;
- } else if (runanimPunch2){
- return State.PUNCH2;
- } else if (runanimSword1){
- return State.ATTACK1;
- } else if (runanimSword2){
- return State.ATTACK2;
- } else if (runanimBow1){
- return State.BOW1;
- } else if (runanimBow2){
- return State.BOW2;
- }
- else if ((Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) && body.getLinearVelocity().x < 10 && body.getLinearVelocity().x > -10){
- return State.CROUCHING;
- }
- else if (body.getLinearVelocity().y > 0|| (body.getLinearVelocity().y < 0 && currentState == State.JUMPING && stateTimer < 2)){
- return State.JUMPING;
- }
- else if (body.getLinearVelocity().y < 0){
- // Allow kenny to somersault in stateTimer condition
- return State.FALLING;
- }
- // FIX Falling
- else if (body.getLinearVelocity().x != 0){
- if (walking)
- return State.WALKING;
- else
- return State.RUNNING;
- }
- else {
- return State.IDLE;
- }
- }
- /**
- * Accessor for Kenny's direction
- * @return Kenny's direction; true if right
- */
- public boolean isFacingRight() {
- return facingRight;
- }
- /**
- * Determines which light attack animation to perform (i.e. swordslash or punch)
- * @param performAnim Boolean - If the animation should be performed
- */
- public void setLightAttackAnimation(boolean performAnim){
- if (hand == null){
- runanimPunch1 = performAnim;
- } else { // Else hand not null
- if (hand instanceof Weapon){
- if (((Weapon) hand).isMelee()){
- // Has a sword
- runanimSword1 = performAnim;
- } else {
- // Has a bow
- runanimPunch1 = performAnim;
- }
- } else { // Else hand has item that's not a weapon
- runanimPunch1 = performAnim;
- }
- }
- }
- /**
- * Determines which heavy attack animation to perform (i.e. swordslash or punch)
- * @param performAnim Boolean - If the animation should be performed
- */
- public void setHeavyAttackAnimation(boolean performAnim){
- if (hand == null){
- runanimPunch2 = performAnim;
- } else { // Else hand not null
- if (hand instanceof Weapon){
- if (((Weapon) hand).isMelee()){
- // Has a sword
- runanimSword2 = performAnim;
- } else {
- // Has a bow
- runanimPunch2 = performAnim;
- }
- } else { // Else hand has item that's not a weapon
- runanimPunch2 = performAnim;
- }
- }
- }
- /**
- * Determine which bow animation is to be performed (mid-air or ground bow attack)
- * @param performAnim Boolean - If the animation should be performed
- */
- public void setBowAnimation(boolean performAnim, boolean isJumping){
- boolean hasBow = false;
- // Determine if hand or inventory has a bow; if true, then kenny has a bow to fire
- if (hand instanceof Weapon) {
- if (!((Weapon) hand).isMelee())
- hasBow = true;
- }
- else {
- for (Item item : inventory){
- if (item instanceof Weapon)
- if (!((Weapon) item).isMelee())
- hasBow = true;
- }
- }
- // If has bow, carry out bow animation
- if (hasBow) {
- if (!isJumping)
- runanimBow1 = performAnim;
- else
- runanimBow2 = performAnim;
- }
- }
- public void setJumpCount(int value){
- jumpCount = value;
- }
- /**
- * Accessor for jumpcount; can only only double-jump and avoids 2+ jumps in one iteration
- * @return
- */
- public int getJumpCount(){
- return jumpCount;
- }
- /**
- * Accessor for item currently in hand of player
- * @return
- */
- public Item getHand() {
- return hand;
- }
- public void setKennySlide(boolean value) {
- runKennySlide = value;
- }
- /**
- * Determines if Kenny has no more space within equippeditems array
- * @return True if full, otherwise false if not full
- */
- public boolean isEquippedItemsFull(){
- if (equippedItemsCount == equippedItems.length)
- return true;
- return false;
- }
- public int getHunger(){
- return hunger;
- }
- public void setHunger(int value){
- hunger = value;
- }
- /** Input operations **/
- /** Input handler operations
- * - May only perform required behaviour if kenny is visible
- */
- private void handleInputAttacks() {
- if (Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT) || Gdx.input.isKeyPressed(Input.Keys.SHIFT_RIGHT)){
- if (Gdx.input.isKeyJustPressed(Input.Keys.J) && this.getBody().getLinearVelocity().x <= 150 && this.getBody().getLinearVelocity().y <= 5 && this.getBody().getLinearVelocity().y >= -5){
- this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
- this.setHeavyAttackAnimation(true);
- System.out.println("ATTACK2");
- }
- } else if ((Gdx.input.isKeyJustPressed(Input.Keys.J)) && this.getBody().getLinearVelocity().x <= 150 && this.getBody().getLinearVelocity().y <= 5 && this.getBody().getLinearVelocity().y >= -5) {
- this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
- this.setLightAttackAnimation(true);
- //this.setanimSword1(true);
- System.out.println("ATTACK1");
- } else if ((Gdx.input.isKeyJustPressed(Input.Keys.L)) && this.getBody().getLinearVelocity().x <= 150) {
- // Mid-air bow attack
- if (this.getBody().getLinearVelocity().y > -1 && this.getBody().getLinearVelocity().y <= 1) {
- this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
- this.setBowAnimation(true, false);
- //this.playthisSound(Kenny.State.BOW1);
- System.out.println("BOW1");
- } else { // Ground bow attack
- this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
- this.setBowAnimation(true, true);
- System.out.println("BOW2");
- }
- }
- }
- private void handleInputMovement() {
- handleDash();
- if (Gdx.input.isKeyJustPressed(Input.Keys.M))
- walking = !walking;
- if (Gdx.input.isKeyJustPressed(Input.Keys.UP) || Gdx.input.isKeyJustPressed(Input.Keys.W)) {
- jump();
- }
- //If down and right is pressed then Slide right
- if ((Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.isKeyPressed(Input.Keys.D)) && this.getBody().getLinearVelocity().x <= 150) {
- if ((Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) && (this.getBody().getLinearVelocity().x >= 0)) {
- this.setKennySlide(true);
- //System.out.println("SLIDE");
- } else {
- moveHorizontal(true, speed);
- //System.out.println("RIGHT");
- }
- }
- //If down and left is pressed then slide left
- if ((Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.isKeyPressed(Input.Keys.A)) && this.getBody().getLinearVelocity().x <= 150) {
- if ((Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) && this.getBody().getLinearVelocity().x <= -0) {
- this.setKennySlide(true);
- //System.out.println("SLIDE");
- } else {
- moveHorizontal(false, speed);
- //System.out.println("LEFT");
- }
- }
- //If only down is pressed the crouch
- if ((Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) && this.getBody().getLinearVelocity().x <= 150) {
- this.getBody().applyLinearImpulse(new Vector2(0, 0), this.getBody().getWorldCenter(), true);
- //System.out.println("CROUCH");
- }
- }
- private void jump(){
- if (jumpCount + 1 <= 2){ // Max Jump count: 2
- jumpCount++;
- this.body.applyLinearImpulse(new Vector2(0, 100), body.getWorldCenter(), true);
- sound = soundManager.get("audio/sound/swosh_01.wav", Sound.class);
- sound.play(0.5f);
- }
- }
- /**
- * Detect if inventory indexes 1-9 is selected and transfer to hand
- */
- public void handleInputHotbar(){
- if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_1)){
- inventoryToHand(0);
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)){
- inventoryToHand(1);
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_3)){
- inventoryToHand(2);
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_4)){
- inventoryToHand(3);
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_5)){
- inventoryToHand(4);
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_6)){
- inventoryToHand(5);
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_7)){
- inventoryToHand(6);
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_8)){
- inventoryToHand(7);
- } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_9)){
- inventoryToHand(8);
- }
- }
- /**
- * If the item added to the hand was a sword, perform sword draw
- * @param item The item to add to the hand/inventory
- */
- @Override
- protected void addToBody(Item item){
- if (item instanceof Note){
- item.playPickupSound(); // Play sound
- item.use(this);
- removeFromWorld(item, screen.getCreator());
- playerManager.getNoteManager().addNote((Note) item); // Add note to the player's collection
- playerManager.setCurrentMode(PlayerManager.Mode.NOTE);
- System.out.println("Item added to note inventory");
- } else if (item.isCureItem()){
- item.playPickupSound();
- removeFromWorld(item, screen.getCreator());
- playerManager.addCureItem(item); // Add the cure item to the cure-item collection
- System.out.println("Item added to cure inventory");
- } else { // regular item
- if (hand == null){
- hand = item;
- item.playPickupSound();
- removeFromWorld(item, screen.getCreator());
- System.out.println("Item added to hand");
- if (item instanceof Weapon)
- if (((Weapon) item).isMelee())
- runAnimSwordDraw = true;
- } else { // hand if full, add to inventory
- for (int i = 0; i < inventory.length; i++){
- if (inventory[i] == null){ // Add item to the first null element
- inventory[i] = item;
- item.playPickupSound();
- removeFromWorld(item, screen.getCreator());
- System.out.println("Item added to inventory");
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement