Java tutorial
/**************************************************************************************** * Copyright (C) 2013 UnluckyNinja * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see here:http://www.gnu.org/licenses/gpl-2.0.txt * ****************************************************************************************/ package com.github.unluckyninja.defenseofhuman.view.renderer; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.*; import com.badlogic.gdx.math.Vector2; import static com.github.unluckyninja.defenseofhuman.DefenseOfHuman.PIXELS_PERMETER; import com.badlogic.gdx.utils.ArrayMap; import com.github.unluckyninja.defenseofhuman.model.entity.Player; import com.github.unluckyninja.defenseofhuman.model.entity.Player.PlayerState; /** * @author UnluckyNinja */ public class PlayerRenderer extends Renderer { private TextureAtlas atlas; private Animation standing; private Animation stand_run; private Animation running; private Animation crouching; private Animation jumping; private Animation falling; private ArrayMap<Player, PlayerRenderAttributes> attributes = new ArrayMap<Player, PlayerRenderAttributes>( false, 64); public PlayerRenderer(TextureAtlas atlas) { if (atlas == null) { atlas = new TextureAtlas(Gdx.files.internal("textures/characters/test.pack")); } this.atlas = atlas; loadAtlas(atlas); } public void render(SpriteBatch batch, Player player, float delta) { PlayerRenderAttributes attributes = updateAttributes(player, delta); TextureRegion region; // ---? // --? // -?- // // float statetime // ? DONE // ?(>0 jumping, <0 falling) TODO // ???INAIR!INAIR?(<0.5s) TODO // ? TODO // TODO if (player.getState() == Player.PlayerState.STANDING) { if (player.getStatetime() >= 0.5) { region = standing.getKeyFrame(player.getStatetime()); } else { region = crouching.getKeyFrame(player.getStatetime()); } float width = region.getRegionWidth() / PIXELS_PERMETER; float height = region.getRegionHeight() / PIXELS_PERMETER; // X???? region.flip(attributes.isFaceright, false); batch.draw(region, player.getX() - width / 2, player.getY(), width, height); // region.flip(attributes.isFaceright, false); } else if (player.getState() == PlayerState.MOVING) { if (player.isRunning()) { if (attributes.isRetarding) { stand_run.setPlayMode(Animation.REVERSED); region = stand_run.getKeyFrame(player.getStatetime()); } else if (attributes.isAccelerating) { region = stand_run.getKeyFrame(player.getStatetime()); } else { region = running.getKeyFrame(player.getStatetime()); } } else { region = standing.getKeyFrame(player.getStatetime()); } float width = region.getRegionWidth() / PIXELS_PERMETER; float height = region.getRegionHeight() / PIXELS_PERMETER; // X???? region.flip(attributes.isFaceright, false); batch.draw(region, player.getX() - width / 2, player.getY(), width, height); // region.flip(attributes.isFaceright, false); if (attributes.isRetarding) { stand_run.setPlayMode(Animation.NORMAL); } } else if (player.getState() == PlayerState.INAIR) { if (attributes.isFalling) { region = falling.getKeyFrame(player.getStatetime()); } else { region = jumping.getKeyFrame(player.getStatetime()); } float width = region.getRegionWidth() / PIXELS_PERMETER; float height = region.getRegionHeight() / PIXELS_PERMETER; // X???? region.flip(attributes.isFaceright, false); batch.draw(region, player.getX() - width / 2, player.getY(), width, height); // region.flip(attributes.isFaceright, false); } } private PlayerRenderAttributes updateAttributes(Player player, float delta) { PlayerRenderAttributes attributes; if (this.attributes.containsKey(player)) { attributes = this.attributes.get(player); } else { attributes = new PlayerRenderAttributes(player); this.attributes.put(player, attributes); } // if (player.getBody().getLinearVelocity().x != 0) { attributes.isFaceright = player.getBody().getLinearVelocity().x >= 0; } attributes.isRetarding = false; attributes.isAccelerating = false; attributes.isFalling = false; if (player.getBody().getLinearVelocity().x > 0) { attributes.isRetarding = player.getBody().getLinearVelocity().x < attributes.oldVel.x; attributes.isAccelerating = player.getBody().getLinearVelocity().x > attributes.oldVel.x; } else if (player.getBody().getLinearVelocity().x < 0) { attributes.isRetarding = player.getBody().getLinearVelocity().x > attributes.oldVel.x; attributes.isAccelerating = player.getBody().getLinearVelocity().x < attributes.oldVel.x; } if (player.getState() == PlayerState.INAIR) { if (player.getBody().getLinearVelocity().y < 0) { attributes.isFalling = true; } } attributes.oldVel.set(player.getBody().getLinearVelocity()); return attributes; } public TextureAtlas getAtlas() { return atlas; } public void setAtlas(TextureAtlas atlas) { this.atlas = atlas; loadAtlas(atlas); } private void loadAtlas(TextureAtlas atlas) { int type = Animation.LOOP; standing = new Animation(0.15f, atlas.findRegions("standing"), type); stand_run = new Animation(0.15f, atlas.findRegions("stand_run"), Animation.NORMAL); running = new Animation(0.15f, atlas.findRegions("running"), type); crouching = new Animation(0.15f, atlas.findRegions("crouching"), type); jumping = new Animation(0.15f, atlas.findRegions("jumping"), type); falling = new Animation(0.15f, atlas.findRegions("falling"), type); } protected static class PlayerRenderAttributes { public boolean isFaceright; public boolean isRetarding; public boolean isAccelerating; public boolean isFalling; public Vector2 oldVel = new Vector2(); private Player player; public PlayerRenderAttributes(Player player) { this.player = player; } public Player getPlayer() { return player; } } }