123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package pathgame.rendering;
- import me.hammerle.snuviengine.api.Renderer;
- import me.hammerle.snuviengine.api.Texture;
- import pathgame.gameplay.Facing;
- import pathgame.gameplay.Player;
- import pathgame.gameplay.PlayerAbilities;
- import pathgame.tilemap.TileMap;
- import pathgame.tilemap.TileRenderType;
- /**
- * A container for holding everything about the renderer for the player
- *
- * @author julia
- */
- public class PlayerRenderer
- {
- private static final Texture CHARACTER = new Texture("resources/character.png");
- private static final Texture CHARACTER_HIKER = new Texture("resources/hiker.png");
- private static final Texture CHARACTER_HUNTER = new Texture("resources/hunter.png");
- private static final Texture CHARACTER_SAILOR = new Texture("resources/sailor.png");
- private static final Texture CHARACTER_SWIMMER = new Texture("resources/swimmer.png");
- /**
- * Recalculates the rendering positions and settings of the menu-elements on
- * the screen every rendertick based on the gamelogic in the gametick
- *
- * @param map the current map
- * @param mapR the map renderer
- * @param r the renderer
- * @param p the current player
- * @param lag the current lag
- * @param offX the current x-offset of the map
- * @param offY the current y-offset of the map
- */
- public void renderTick(TileMap map, TileMapRenderer mapR, Renderer r, Player p, float lag, float offX, float offY)
- {
- boolean inWater = p.getCurrTile().getRenderType() == TileRenderType.WATER;
- float playerSize = mapR.getScale() * TileRenderer.TILE_SIZE;
- r.setMixColorEnabled(false);
- r.setColorEnabled(false);
- r.setTextureEnabled(true);
- r.translateTo(0.0f, 0.0f);
- r.updateMatrix();
- float yIndex;
- float baseX = (p.getLastX() + (p.getX() - p.getLastX()) * lag);
- float ix = baseX * mapR.getScale() * TileRenderer.TILE_SIZE + offX;
- float baseY = (p.getLastY() + (p.getY() - p.getLastY()) * lag);
- float iy = baseY * mapR.getScale() * TileRenderer.TILE_SIZE + offY;
- baseX = baseX - (int) baseX;
- baseY = baseY - (int) baseY;
- int tIndex = 0;
- float xTexOff = 0;
- float yTexOff = 0;
- bindTexture(p, inWater);
-
- if(p.getVelX() > 0)
- {
- //go right
- yIndex = 2;
- tIndex = checkForAnimationIndex(baseX);
- }
- else if(p.getVelX() < 0)
- {
- //go left
- yIndex = 1;
- tIndex = checkForAnimationIndex(baseX);
- }
- else if(p.getVelY() > 0)
- {
- //go down
- yIndex = 0;
- tIndex = checkForAnimationIndex(baseY);
- }
- else if(p.getVelY() < 0)
- {
- //go up
- yIndex = 3;
- tIndex = checkForAnimationIndex(baseY);
- }
- else
- {
- //stand still
- yIndex = p.getFacing().getIndex();
- // idle animation on water
- if(inWater && (p.getTicksLived() % 20) < 10)
- {
- tIndex = 1;
- }
- }
- if(p.isSailing())
- {
- yTexOff = 0.5f;
- }
- else if(inWater)
- {
- xTexOff = 0.5f;
- }
- float viewScale = r.getViewScale();
- ix = (int) (ix * viewScale) / viewScale;
- iy = (int) (iy * viewScale) / viewScale;
- r.getTextureRenderer().drawRectangle(ix, iy, ix + playerSize, iy + playerSize,
- tIndex * 0.125f + xTexOff, yIndex * 0.125f + yTexOff,
- (tIndex + 1) * 0.125f + xTexOff, yIndex * 0.125f + 0.125f + yTexOff);
- }
- private int checkForAnimationIndex(float base)
- {
- return ((int) (base * 4 * 2)) % 4;
- }
-
- private void bindTexture(Player p, boolean inWater)
- {
- if(p.isSailing() || inWater || p.getAbilities() == PlayerAbilities.NORMAL)
- {
- CHARACTER.bind();
- }
- else if(p.getAbilities() == PlayerAbilities.HIKER)
- {
- CHARACTER_HIKER.bind();
- }
- else if(p.getAbilities() == PlayerAbilities.HUNTER)
- {
- CHARACTER_HUNTER.bind();
- }
- else if(p.getAbilities() == PlayerAbilities.SAILOR)
- {
- CHARACTER_SAILOR.bind();
- }
- else if(p.getAbilities() == PlayerAbilities.SWIMMER)
- {
- CHARACTER_SWIMMER.bind();
- }
- else
- {
- CHARACTER.bind();
- }
- }
- }
|