Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.circlewars.game;
- import org.newdawn.slick.*;
- import org.newdawn.slick.tiled.TiledMap;
- public class BasicEngine extends BasicGame implements Runnable
- {
- private TiledMap grassMap;
- private Animation sprite, up, down, left, right;
- private float x = 32f, y = 32f;
- /** The collision map indicating which tiles block movement - generated based on tile properties */
- private boolean[][] blocked;
- private static final int SIZE = 32;
- public BasicEngine()
- {
- super("Test game");
- }
- @Override
- public void run()
- {
- try
- {
- AppGameContainer app = new AppGameContainer(new BasicEngine());
- app.setDisplayMode(1280, 720, false);
- app.start();
- }
- catch (SlickException e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public void init(GameContainer container) throws SlickException
- {
- Image [] movementUp = {new Image("com/circlewars/game/sprites/wmg1_bk1.png"), new Image("com/circlewars/game/sprites/wmg1_bk2.png")};
- Image [] movementDown = {new Image("com/circlewars/game/sprites/wmg1_fr1.png"), new Image("com/circlewars/game/sprites/wmg1_fr2.png")};
- Image [] movementLeft = {new Image("com/circlewars/game/sprites/wmg1_lf1.png"), new Image("com/circlewars/game/sprites/wmg1_lf2.png")};
- Image [] movementRight = {new Image("com/circlewars/game/sprites/wmg1_rt1.png"), new Image("com/circlewars/game/sprites/wmg1_rt2.png")};
- int [] duration = {300, 300};
- grassMap = new TiledMap("com/circlewars/game/grassmap.tmx");
- /*
- * false variable means do not auto update the animation.
- * By setting it to false animation will update only when
- * the user presses a key.
- */
- up = new Animation(movementUp, duration, false);
- down = new Animation(movementDown, duration, false);
- left = new Animation(movementLeft, duration, false);
- right = new Animation(movementRight, duration, false);
- // Original orientation of the sprite. It will look right.
- sprite = right;
- blocked = new boolean[grassMap.getWidth()][grassMap.getHeight()];
- for (int xAxis=0;xAxis<grassMap.getWidth(); xAxis++)
- {
- for (int yAxis=0;yAxis<grassMap.getHeight(); yAxis++)
- {
- int tileID = grassMap.getTileId(xAxis, yAxis, 0);
- String value = grassMap.getLayerProperty(tileID, "collision", "false");
- if ("true".equals(value))
- {
- blocked[xAxis][yAxis] = true;
- }
- }
- }
- }
- @Override
- public void update(GameContainer container, int delta) throws SlickException
- {
- Input input = container.getInput();
- float fdelta = delta*0.1f;
- if (input.isKeyDown(Input.KEY_UP))
- {
- sprite = up;
- if (!(isBlocked(x, y - fdelta) || isBlocked(x+SIZE-1, y - fdelta)))
- {
- sprite.update(delta);
- // The lower the delta the slowest the sprite will animate.
- y -= fdelta;
- }
- }
- else if (input.isKeyDown(Input.KEY_DOWN))
- {
- sprite = down;
- if (!(isBlocked(x, y + SIZE + fdelta) || isBlocked(x+SIZE-1, y + SIZE + fdelta)))
- {
- sprite.update(delta);
- y += fdelta;
- }
- }
- else if (input.isKeyDown(Input.KEY_LEFT))
- {
- sprite = left;
- if (!(isBlocked(x - fdelta, y) || isBlocked(x - fdelta, y+SIZE-1)))
- {
- sprite.update(delta);
- x -= fdelta;
- }
- }
- else if (input.isKeyDown(Input.KEY_RIGHT))
- {
- sprite = right;
- if (!(isBlocked(x + SIZE + fdelta, y) || isBlocked(x + SIZE + fdelta, y+SIZE-1)))
- {
- sprite.update(delta);
- x += fdelta;
- }
- }
- }
- @Override
- public void render(GameContainer container, Graphics g) throws SlickException {
- grassMap.render(0, 0);
- sprite.draw((int)x, (int)y);
- }
- private boolean isBlocked(float x, float y)
- {
- int xBlock = (int)x / SIZE;
- int yBlock = (int)y / SIZE;
- return blocked[xBlock][yBlock];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement