Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package not.zuul.world;
- import java.util.HashMap;
- import org.newdawn.slick.Animation;
- import org.newdawn.slick.AppGameContainer;
- import org.newdawn.slick.BasicGame;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.Image;
- import org.newdawn.slick.Input;
- import org.newdawn.slick.SlickException;
- import org.newdawn.slick.tiled.TiledMap;
- public class GameMain extends BasicGame{
- private TiledMap currentMap;
- private Room currentRoom;
- private Animation sprite, up, down, left, right;
- private float x = 64f, y = 64f;
- private boolean[][]blocked;
- private boolean[][]doors;
- private static final int SIZE = 32;
- private HashMap<String, TiledMap> rooms;
- private HashMap<String, Room> game_map;
- private boolean isOnDoor = false;
- public GameMain()
- {
- super("Super Awesome Game V4.20 by Edgaras");
- }
- public static void main(String[] arguments)
- {
- try
- {
- AppGameContainer app = new AppGameContainer(
- new GameMain());
- app.setDisplayMode(320,320,false);
- app.start();
- }
- catch(SlickException e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public void init(GameContainer container) throws SlickException
- {
- initAllRooms();
- initMap();
- currentMap = game_map.get("outside").getMap();
- currentRoom = game_map.get("outside");
- Image [] movementUp = {
- new Image("resources/player/player_up_1.png"),
- new Image("resources/player/player_up_2.png"),
- new Image("resources/player/player_up_3.png"),
- new Image("resources/player/player_up_4.png"),
- new Image("resources/player/player_up_5.png"),
- };
- Image [] movementDown = {
- new Image("resources/player/player_down_1.png"),
- new Image("resources/player/player_down_2.png"),
- new Image("resources/player/player_down_3.png"),
- new Image("resources/player/player_down_4.png"),
- new Image("resources/player/player_down_5.png"),
- };
- Image [] movementLeft = {
- new Image("resources/player/player_left_1.png"),
- new Image("resources/player/player_left_2.png"),
- new Image("resources/player/player_left_3.png"),
- new Image("resources/player/player_left_4.png"),
- new Image("resources/player/player_left_5.png"),
- };
- Image [] movementRight = {
- new Image("resources/player/player_right_1.png"),
- new Image("resources/player/player_right_2.png"),
- new Image("resources/player/player_right_3.png"),
- new Image("resources/player/player_right_4.png"),
- new Image("resources/player/player_right_5.png"),
- };
- int [] duration = {200, 200, 200, 200, 200};
- up = new Animation(movementUp, duration, false);
- down = new Animation(movementDown, duration, false);
- left = new Animation(movementLeft, duration, false);
- right = new Animation(movementRight, duration, false);
- sprite = right;
- blocked = new boolean[currentMap.getWidth()][currentMap.getHeight()];
- for(int xAxis = 0; xAxis<currentMap.getWidth(); xAxis++)
- {
- for(int yAxis = 0; yAxis<currentMap.getHeight();yAxis++) {
- int tileID = currentMap.getTileId(xAxis, yAxis, 0);
- String value = currentMap.getTileProperty(tileID, "blocked", "false");
- if("true".equals(value))
- {
- blocked[xAxis][yAxis] = true;
- }
- }
- }
- doors = new boolean[currentMap.getWidth()][currentMap.getHeight()];
- for(int xAxis = 0; xAxis<currentMap.getWidth(); xAxis++)
- {
- for(int yAxis = 0; yAxis<currentMap.getHeight();yAxis++) {
- int tileID = currentMap.getTileId(xAxis, yAxis, 0);
- String value = currentMap.getTileProperty(tileID, "door", "false");
- if("true".equals(value))
- {
- doors[xAxis][yAxis] = true;
- }
- }
- }
- }
- @Override
- public void update(GameContainer container, int delta) throws SlickException
- {
- Input input = container.getInput();
- if(input.isKeyDown(Input.KEY_UP)) {
- sprite = up;
- if(!isBlocked(x,y - delta * 0.1f) && y > 0) {
- sprite.update(delta); // lower delta = lower speed animation
- y -= delta * 0.1f;
- }
- if(isDoor(x,y - delta * 0.1f)) {
- isOnDoor = true;
- currentMap = currentRoom.getNorth().getMap();
- currentRoom = currentRoom.getNorth();
- }
- }
- else if(input.isKeyDown(Input.KEY_DOWN)) {
- sprite = down;
- if(!isBlocked(x,y + SIZE + delta * 0.1f) && y < (currentMap.getHeight() * SIZE) - (down.getHeight() + 5)) {
- sprite.update(delta);
- y+=delta*0.1f;
- }
- if(isDoor(x,y + SIZE + delta * 0.1f)) {
- isOnDoor = true;
- currentMap = currentRoom.getSouth().getMap();
- currentRoom = currentRoom.getSouth();
- }
- }
- else if(input.isKeyDown(Input.KEY_LEFT) ) {
- sprite = left;
- if(!isBlocked(x - delta * 0.1f,y) && x > 0) {
- sprite.update(delta);
- x -= delta * 0.1f;
- }
- if(isDoor(x - delta * 0.1f,y)) {
- isOnDoor = true;
- currentMap = currentRoom.getWest().getMap();
- currentRoom = currentRoom.getWest();
- }
- }
- else if(input.isKeyDown(Input.KEY_RIGHT) ) {
- sprite = right;
- System.out.println("" + currentMap.getWidth());
- if(!isBlocked(x + SIZE + delta,y) && x < (currentMap.getWidth()*SIZE) - (right.getWidth() + 10)) {
- sprite.update(delta);
- x+= delta * 0.1f;
- }
- if(isDoor(x + SIZE + delta,y)) {
- isOnDoor = true;
- currentMap = currentRoom.getEast().getMap();
- currentRoom = currentRoom.getEast();
- }
- }
- }
- public void render(GameContainer container, Graphics g) throws SlickException
- {
- currentMap.render(0,0);
- sprite.draw((int)x, (int)y);
- if(isOnDoor) {
- g.drawString("Door", 160, 160);
- isOnDoor = false;
- }
- }
- private boolean isBlocked(float x, float y) {
- int xBlock = (int)x / SIZE;
- int yBlock = (int)y / SIZE;
- return blocked[xBlock][yBlock];
- }
- private boolean isDoor(float x, float y) {
- int xBlock = (int)x / SIZE;
- int yBlock = (int)y / SIZE;
- return doors[xBlock][yBlock];
- }
- public void initAllRooms() throws SlickException {
- rooms = new HashMap<String, TiledMap>();
- rooms.put("N", new TiledMap("resources/map/Rooms/room_north.tmx"));
- rooms.put("S", new TiledMap("resources/map/Rooms/room_south.tmx"));
- rooms.put("E", new TiledMap("resources/map/Rooms/room_east.tmx"));
- rooms.put("W", new TiledMap("resources/map/Rooms/room_west.tmx"));
- rooms.put("NE", new TiledMap("resources/map/Rooms/room_north_east.tmx"));
- rooms.put("NS", new TiledMap("resources/map/Rooms/room_north_south.tmx"));
- rooms.put("NW", new TiledMap("resources/map/Rooms/room_north_west.tmx"));
- rooms.put("SE", new TiledMap("resources/map/Rooms/room_south_east.tmx"));
- rooms.put("SW", new TiledMap("resources/map/Rooms/room_south_west.tmx"));
- rooms.put("EW", new TiledMap("resources/map/Rooms/room_east_west.tmx"));
- rooms.put("NSE", new TiledMap("resources/map/Rooms/room_north_south_east.tmx"));
- rooms.put("NSW", new TiledMap("resources/map/Rooms/room_north_south_west.tmx"));
- rooms.put("NEW", new TiledMap("resources/map/Rooms/room_north_east_west.tmx"));
- rooms.put("SEW", new TiledMap("resources/map/Rooms/room_south_east_west.tmx"));
- rooms.put("NSEW", new TiledMap("resources/map/Rooms/room_north_south_east_west.tmx"));
- }
- public void initMap() throws SlickException {
- game_map = new HashMap<String, Room>();
- game_map.put("outside", new Room(rooms.get("N"), "You're outside."));
- game_map.put("mainHall", new Room(rooms.get("NSEW"), "You're in main hall."));
- game_map.put("emptyRoom", new Room(rooms.get("NE"), "You're in empty room."));
- game_map.put("bathRoom", new Room(rooms.get("S"), "You're in bathroom"));
- game_map.put("kitchen", new Room(rooms.get("NW"), "You're in kitchen"));
- game_map.put("diningRoom", new Room(rooms.get("NS"), "You;re in dining room"));
- game_map.put("backYard", new Room(rooms.get("SE"), "You're in back yard"));
- game_map.put("storageRoom", new Room(rooms.get("W"), "You're in storage room"));
- game_map.get("outside").setRooms(game_map.get("mainHall"), null, null, null);
- game_map.get("mainHall").setRooms(game_map.get("diningRoom"), game_map.get("outside"),
- game_map.get("emptyRoom"), game_map.get("kitchen"));
- game_map.get("emptyRoom").setRooms(game_map.get("bathRoom"), null, game_map.get("mainHall"), null );
- game_map.get("bathRoom").setRooms(null, game_map.get("emptyRoom"), null, null);
- game_map.get("kitchen").setRooms(null, null, null, game_map.get("mainHall"));
- game_map.get("diningRoom").setRooms(game_map.get("backYard"), game_map.get("mainHall)"), null, null);
- game_map.get("backYard").setRooms(null, game_map.get("diningRoom"), game_map.get("storageRoom"), null);
- game_map.get("storageRoom").setRooms(null, null, null, game_map.get("backYard"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement