123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package me.hammerle.supersnuvi.entity.components.ai;
- import me.hammerle.snuviengine.api.Shader;
- import me.hammerle.snuviengine.api.Texture;
- import me.hammerle.supersnuvi.Keys;
- import me.hammerle.supersnuvi.entity.Entity;
- import me.hammerle.supersnuvi.util.Face;
- import me.hammerle.supersnuvi.util.Utils;
- public class HumanController extends Controller
- {
- private final static int SIZE = 1024;
- private final static Texture HERO = new Texture("resources/hero.png");
-
- private class TexturePart
- {
- private boolean draw = false;
- private float ox = 0.0f;
- private float oy = 0.0f;
- private float w = 0.0f;
- private float h = 0.0f;
- private float tx = 0.0f;
- private float ty = 0.0f;
- }
-
- private TexturePart body = new TexturePart();
- private TexturePart arms = new TexturePart();
- private TexturePart head = new TexturePart();
- private TexturePart[] parts = new TexturePart[]
- {
- body, arms, head
- };
-
- private int walkFrame = 0;
- private int idleFrame = 0;
- public HumanController(Entity ent)
- {
- super(ent);
- }
-
- private void nextWalkFrame()
- {
- walkFrame = (walkFrame + 1) % 9;
- idleFrame = 0;
- }
-
- private void nextIdleFrame()
- {
- walkFrame = 0;
- idleFrame = (idleFrame + 1) % 14;
- }
-
- private void resetFrames()
- {
- walkFrame = 0;
- idleFrame = 0;
- }
-
- @Override
- public void tick()
- {
- for(TexturePart tp : parts)
- {
- tp.draw = false;
- }
-
- float speed = ent.getMovement().getVelocityX();
- if(Keys.RUN.isDown())
- {
- speed *= 1.5f;
- }
-
- if(Keys.LEFT.isDown())
- {
- ent.setMotionX(-speed);
- }
-
- if(Keys.RIGHT.isDown())
- {
- ent.setMotionX(speed);
- }
-
- if(Keys.JUMP.isDown())
- {
- ent.getMovement().jump();
- }
-
- body.draw = true;
- body.ox = 0.0f;
- body.oy = 0.0f;
- body.h = 64.0f;
- body.w = 32.0f;
-
- if(ent.isOnGround())
- {
- if(ent.getMotionX() == 0.0f)
- {
- nextIdleFrame();
- body.tx = (idleFrame * 32.0f) / SIZE;
- body.ty = 128.0f / SIZE;
- }
- else
- {
- nextWalkFrame();
- body.tx = (96.0f + walkFrame * 32.0f) / SIZE;
- body.ty = 0.0f;
- }
- }
- else
- {
- resetFrames();
- body.tx = 0.0f;
- body.ty = 0.0f;
- }
- }
- @Override
- public void renderTick(float lag)
- {
- HERO.bind();
- float x = Utils.interpolate(ent.getLastX(), ent.getX(), lag);
- float y = Utils.interpolate(ent.getLastY(), ent.getY(), lag);
- for(TexturePart tp : parts)
- {
- if(tp.draw)
- {
- float m1;
- float m2;
- if(ent.getFace() == Face.LEFT)
- {
- m1 = tp.w;
- m2 = 0.0f;
- }
- else
- {
- m1 = 0.0f;
- m2 = tp.w;
- }
- Shader.getTextureRenderer().drawRectangle(
- x + tp.ox + m1, y + tp.oy,
- x + tp.ox + m2, y + tp.oy + tp.h,
- tp.tx, tp.ty,
- tp.tx + (tp.w / SIZE), tp.ty + (tp.h / SIZE));
- }
- }
- }
- }
|