Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.83 KB | None | 0 0
  1. package not.zuul.world;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import org.newdawn.slick.Animation;
  6. import org.newdawn.slick.AppGameContainer;
  7. import org.newdawn.slick.BasicGame;
  8. import org.newdawn.slick.GameContainer;
  9. import org.newdawn.slick.Graphics;
  10. import org.newdawn.slick.Image;
  11. import org.newdawn.slick.Input;
  12. import org.newdawn.slick.SlickException;
  13. import org.newdawn.slick.tiled.TiledMap;
  14.  
  15. public class GameMain extends BasicGame{
  16.    
  17.     private TiledMap currentMap;
  18.     private Room currentRoom;
  19.     private Animation sprite, up, down, left, right;
  20.     private float x = 64f, y = 64f;
  21.     private boolean[][]blocked;
  22.     private boolean[][]doors;
  23.     private static final int SIZE = 32;
  24.     private HashMap<String, TiledMap> rooms;
  25.     private HashMap<String, Room> game_map;
  26.     private boolean isOnDoor = false;
  27.    
  28.     public GameMain()
  29.     {
  30.         super("Super Awesome Game V4.20 by Edgaras");
  31.     }
  32.    
  33.     public static void main(String[] arguments)
  34.     {
  35.         try
  36.         {
  37.             AppGameContainer app = new AppGameContainer(
  38.                     new GameMain());
  39.             app.setDisplayMode(320,320,false);
  40.             app.start();
  41.         }
  42.         catch(SlickException e)
  43.         {
  44.             e.printStackTrace();
  45.         }
  46.     }
  47.    
  48.     @Override
  49.     public void init(GameContainer container) throws SlickException
  50.     {
  51.        
  52.         initAllRooms();
  53.         initMap();
  54.        
  55.         currentMap = game_map.get("outside").getMap();
  56.         currentRoom = game_map.get("outside");
  57.         Image [] movementUp = {
  58.                 new Image("resources/player/player_up_1.png"),
  59.                 new Image("resources/player/player_up_2.png"),
  60.                 new Image("resources/player/player_up_3.png"),
  61.                 new Image("resources/player/player_up_4.png"),
  62.                 new Image("resources/player/player_up_5.png"),
  63.         };
  64.        
  65.         Image [] movementDown = {
  66.                 new Image("resources/player/player_down_1.png"),
  67.                 new Image("resources/player/player_down_2.png"),
  68.                 new Image("resources/player/player_down_3.png"),
  69.                 new Image("resources/player/player_down_4.png"),
  70.                 new Image("resources/player/player_down_5.png"),
  71.         };
  72.        
  73.         Image [] movementLeft = {
  74.                 new Image("resources/player/player_left_1.png"),
  75.                 new Image("resources/player/player_left_2.png"),
  76.                 new Image("resources/player/player_left_3.png"),
  77.                 new Image("resources/player/player_left_4.png"),
  78.                 new Image("resources/player/player_left_5.png"),
  79.         };
  80.        
  81.         Image [] movementRight = {
  82.                 new Image("resources/player/player_right_1.png"),
  83.                 new Image("resources/player/player_right_2.png"),
  84.                 new Image("resources/player/player_right_3.png"),
  85.                 new Image("resources/player/player_right_4.png"),
  86.                 new Image("resources/player/player_right_5.png"),
  87.         };
  88.        
  89.         int [] duration = {200, 200, 200, 200, 200};
  90.        
  91.         up = new Animation(movementUp, duration, false);
  92.         down = new Animation(movementDown, duration, false);
  93.         left = new Animation(movementLeft, duration, false);
  94.         right = new Animation(movementRight, duration, false);
  95.        
  96.         sprite = right;
  97.        
  98.         blocked = new boolean[currentMap.getWidth()][currentMap.getHeight()];
  99.         for(int xAxis = 0; xAxis<currentMap.getWidth(); xAxis++)
  100.         {
  101.            
  102.             for(int yAxis = 0; yAxis<currentMap.getHeight();yAxis++) {
  103.                
  104.                 int tileID = currentMap.getTileId(xAxis, yAxis, 0);
  105.                 String value = currentMap.getTileProperty(tileID, "blocked", "false");
  106.                 if("true".equals(value))
  107.                 {
  108.                     blocked[xAxis][yAxis] = true;
  109.             }
  110.         }
  111.     }
  112.        
  113.         doors = new boolean[currentMap.getWidth()][currentMap.getHeight()];
  114.        
  115.         for(int xAxis = 0; xAxis<currentMap.getWidth(); xAxis++)
  116.         {  
  117.             for(int yAxis = 0; yAxis<currentMap.getHeight();yAxis++) {
  118.                
  119.                 int tileID = currentMap.getTileId(xAxis, yAxis, 0);
  120.                 String value = currentMap.getTileProperty(tileID, "door", "false");
  121.                 if("true".equals(value))
  122.                 {
  123.                     doors[xAxis][yAxis] = true;
  124.             }
  125.         }
  126.     }
  127.        
  128.        
  129.     }
  130.    
  131.     @Override
  132.     public void update(GameContainer container, int delta) throws SlickException
  133.     {
  134.         Input input = container.getInput();
  135.         if(input.isKeyDown(Input.KEY_UP)) {
  136.             sprite = up;
  137.             if(!isBlocked(x,y - delta * 0.1f) && y > 0) {
  138.                 sprite.update(delta);                // lower delta = lower speed animation
  139.                 y -= delta * 0.1f;
  140.             }
  141.             if(isDoor(x,y - delta * 0.1f)) {
  142.                 isOnDoor = true;
  143.                 currentMap = currentRoom.getNorth().getMap();
  144.                 currentRoom = currentRoom.getNorth();
  145.                
  146.             }
  147.         }
  148.         else if(input.isKeyDown(Input.KEY_DOWN)) {
  149.             sprite = down;
  150.             if(!isBlocked(x,y + SIZE + delta * 0.1f) && y < (currentMap.getHeight() * SIZE) - (down.getHeight() + 5)) {
  151.                 sprite.update(delta);
  152.                 y+=delta*0.1f;
  153.             }
  154.             if(isDoor(x,y + SIZE + delta * 0.1f)) {
  155.                 isOnDoor = true;
  156.                 currentMap = currentRoom.getSouth().getMap();
  157.                 currentRoom = currentRoom.getSouth();
  158.             }
  159.         }
  160.         else if(input.isKeyDown(Input.KEY_LEFT) ) {
  161.             sprite = left;
  162.             if(!isBlocked(x - delta * 0.1f,y) && x > 0) {
  163.                 sprite.update(delta);
  164.                 x -= delta * 0.1f;
  165.             }
  166.             if(isDoor(x - delta * 0.1f,y)) {
  167.                 isOnDoor = true;
  168.                 currentMap = currentRoom.getWest().getMap();
  169.                 currentRoom = currentRoom.getWest();
  170.             }
  171.         }
  172.         else if(input.isKeyDown(Input.KEY_RIGHT) ) {
  173.             sprite = right;
  174.             System.out.println("" + currentMap.getWidth());
  175.             if(!isBlocked(x + SIZE + delta,y) && x < (currentMap.getWidth()*SIZE) - (right.getWidth() + 10)) {
  176.                 sprite.update(delta);
  177.                 x+= delta * 0.1f;
  178.             }
  179.             if(isDoor(x + SIZE + delta,y)) {
  180.                 isOnDoor = true;
  181.                 currentMap = currentRoom.getEast().getMap();
  182.                 currentRoom = currentRoom.getEast();
  183.             }
  184.         }
  185.     }
  186.    
  187.    
  188.     public void render(GameContainer container, Graphics g) throws SlickException
  189.     {
  190.         currentMap.render(0,0);
  191.         sprite.draw((int)x, (int)y);
  192.         if(isOnDoor) {
  193.             g.drawString("Door", 160, 160);
  194.             isOnDoor = false;
  195.         }
  196.     }
  197.    
  198.     private boolean isBlocked(float x, float y) {
  199.         int xBlock = (int)x / SIZE;
  200.         int yBlock = (int)y / SIZE;
  201.         return blocked[xBlock][yBlock];
  202.     }
  203.    
  204.     private boolean isDoor(float x, float y) {
  205.         int xBlock = (int)x / SIZE;
  206.         int yBlock = (int)y / SIZE;
  207.         return doors[xBlock][yBlock];
  208.     }
  209.    
  210.    
  211.     public void initAllRooms() throws SlickException {
  212.         rooms = new HashMap<String, TiledMap>();
  213.         rooms.put("N", new TiledMap("resources/map/Rooms/room_north.tmx"));
  214.         rooms.put("S", new TiledMap("resources/map/Rooms/room_south.tmx"));
  215.         rooms.put("E", new TiledMap("resources/map/Rooms/room_east.tmx"));
  216.         rooms.put("W", new TiledMap("resources/map/Rooms/room_west.tmx"));
  217.         rooms.put("NE", new TiledMap("resources/map/Rooms/room_north_east.tmx"));
  218.         rooms.put("NS", new TiledMap("resources/map/Rooms/room_north_south.tmx"));
  219.         rooms.put("NW", new TiledMap("resources/map/Rooms/room_north_west.tmx"));
  220.         rooms.put("SE", new TiledMap("resources/map/Rooms/room_south_east.tmx"));
  221.         rooms.put("SW", new TiledMap("resources/map/Rooms/room_south_west.tmx"));
  222.         rooms.put("EW", new TiledMap("resources/map/Rooms/room_east_west.tmx"));
  223.         rooms.put("NSE", new TiledMap("resources/map/Rooms/room_north_south_east.tmx"));
  224.         rooms.put("NSW", new TiledMap("resources/map/Rooms/room_north_south_west.tmx"));
  225.         rooms.put("NEW", new TiledMap("resources/map/Rooms/room_north_east_west.tmx"));
  226.         rooms.put("SEW", new TiledMap("resources/map/Rooms/room_south_east_west.tmx"));
  227.         rooms.put("NSEW", new TiledMap("resources/map/Rooms/room_north_south_east_west.tmx"));
  228.     }
  229.    
  230.     public void initMap() throws SlickException {
  231.         game_map = new HashMap<String, Room>();
  232.         game_map.put("outside", new Room(rooms.get("N"), "You're outside."));
  233.         game_map.put("mainHall", new Room(rooms.get("NSEW"), "You're in main hall."));
  234.         game_map.put("emptyRoom", new Room(rooms.get("NE"), "You're in empty room."));
  235.         game_map.put("bathRoom", new Room(rooms.get("S"), "You're in bathroom"));
  236.         game_map.put("kitchen", new Room(rooms.get("NW"), "You're in kitchen"));
  237.         game_map.put("diningRoom", new Room(rooms.get("NS"), "You;re in dining room"));
  238.         game_map.put("backYard", new Room(rooms.get("SE"), "You're in back yard"));
  239.         game_map.put("storageRoom", new Room(rooms.get("W"), "You're in storage room"));
  240.        
  241.         game_map.get("outside").setRooms(game_map.get("mainHall"), null, null, null);
  242.         game_map.get("mainHall").setRooms(game_map.get("diningRoom"), game_map.get("outside"),
  243.                 game_map.get("emptyRoom"), game_map.get("kitchen"));
  244.         game_map.get("emptyRoom").setRooms(game_map.get("bathRoom"), null, game_map.get("mainHall"), null );
  245.         game_map.get("bathRoom").setRooms(null, game_map.get("emptyRoom"), null, null);
  246.         game_map.get("kitchen").setRooms(null, null, null, game_map.get("mainHall"));
  247.         game_map.get("diningRoom").setRooms(game_map.get("backYard"), game_map.get("mainHall)"), null, null);
  248.         game_map.get("backYard").setRooms(null, game_map.get("diningRoom"), game_map.get("storageRoom"), null);
  249.         game_map.get("storageRoom").setRooms(null, null, null, game_map.get("backYard"));
  250.        
  251.     }
  252.    
  253.    
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement