Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javagame;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.Image;
- import org.newdawn.slick.Renderable;
- import java.util.*;
- import org.newdawn.slick.*;
- import org.newdawn.slick.geom.Polygon;
- import org.newdawn.slick.state.*;
- import org.newdawn.slick.tiled.*;
- import org.w3c.dom.Element;
- public class Play extends BasicGameState{
- int direction = 0;
- Image worldMap;
- Image creatures;
- SpriteSheet sheet;
- boolean quit = false;
- int[] duration = {200,200};
- float buckyPositionX = 0;
- float buckyPositionY = 0;
- float shiftX = buckyPositionX+320;
- float shiftY = buckyPositionY+320;
- int buckyPosX=10-(int)buckyPositionX/32;
- int buckyPosY=5-(int)buckyPositionY/32;
- TiledMap map;
- Image tileb;
- List<Polygon> entities;
- ArrayList<Float> entLocX;
- ArrayList<Float> entLocY;
- float[][] ent;
- Polygon playerPoly;
- int x =0;
- int y =0;
- Polygon[] polygo;
- int all=0;
- public Play(int state){
- }
- public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
- entities = new ArrayList<Polygon>();
- entLocX = new ArrayList<Float>();
- entLocY = new ArrayList<Float>();
- tileb = new Image("res/tile_b.png");
- map = new TiledMap("res/map3.tmx");
- creatures = new Image("res/lofi_char.png", false, Image.FILTER_NEAREST);
- sheet = new SpriteSheet(creatures.getScaledCopy(4), 32 , 32);
- worldMap= new Image("res/world.png");
- playerPoly = new Polygon(new float[]{ //makes polygon around player
- buckyPositionX,buckyPositionY,
- buckyPositionX+32,buckyPositionY,
- buckyPositionX+32,buckyPositionY+32,
- buckyPositionX,buckyPositionY+32
- });
- }
- public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
- map.render((int)buckyPositionX, (int)buckyPositionY); //renders map
- g.drawImage(sheet.getSubImage(direction,29), shiftX, shiftY); //renders player
- g.drawString("X"+ buckyPosX +"\nY"+ buckyPosY, 400, 20); //used for debug
- g.drawString("X"+buckyPositionX+"\nY"+buckyPositionY, 400, 60); //used for debug
- g.drawString("Col "+isColliding()+"\nY"+entLocY.get(0), 400, 100); //used for debug
- g.drawString("ID "+map.getTileId(buckyPosX, buckyPosY, map.getLayerIndex("collision")), 400, 140); //used for debug
- g.drawString("AofC: "+entities.size(), 400, 160); //used for debug
- g.draw(playerPoly); //draws polygon around player,debug
- for(int i=0;i<entities.size();i++){ //draws polygons around colliding objects ,debug
- g.draw(entities.get(i));
- }
- }
- public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
- for(int i=0;i<entities.size();i++){ //moves polygons as player moves
- entities.get(i).setX(entLocX.get(i)+buckyPositionX);
- entities.get(i).setY(entLocY.get(i)+buckyPositionY);
- }
- playerPoly.setX(shiftX); //sets player polygon to middle of screen
- playerPoly.setY(shiftY); //sets player polygon to middle of screen
- buckyPosX=10-(int)buckyPositionX/32; //makes tile cordinates
- buckyPosY=5-(int)buckyPositionY/32; //makes tile cordinates
- Input input = gc.getInput();
- if(input.isKeyDown(Input.KEY_UP)){ //move up
- direction = 3;
- buckyPositionY += delta* .1f;
- if(isColliding()){
- buckyPositionY -= delta* .1f;
- }
- }
- if(input.isKeyDown(Input.KEY_DOWN)){ //move down
- direction = 1;
- buckyPositionY -= delta* .1f;
- if(isColliding()){
- buckyPositionY += delta* .1f;
- }
- }
- if(input.isKeyDown(Input.KEY_LEFT)){ //move left
- direction = 2;
- buckyPositionX += delta* .1f;
- if(isColliding()){
- buckyPositionX -= delta* .1f;
- }
- }
- if(input.isKeyDown(Input.KEY_RIGHT)){ //move right
- direction = 0;
- buckyPositionX -= delta* .1f;
- if(isColliding()){
- buckyPositionX += delta* .1f;
- }
- }
- makeCollidePoly();
- if(all!=entities.size()){
- for(int en=0;en<entities.size();en++){
- entLocX.add(entities.get(en).getX());
- entLocY.add(entities.get(en).getY());
- }
- all++;
- }
- }
- public int getID(){
- return 1;
- }
- private boolean isColliding(){ //returns true if player's polygon collides with object's poly
- boolean b = false;
- for(int i=0;i<entities.size();i++){
- if(playerPoly.intersects(entities.get(i))){b=true;}
- }
- return false;//false cause player spawns next to tree and gets stuck,should return boolean b
- }
- private void makeCollidePoly(){ //makes polygons around every tile in "collision" layer
- if(entities.size()<=600){
- for (int x = 0; x < map.getWidth(); x++) {
- for (int y = 0; y < map.getHeight(); y++) {
- int tileID = map.getTileId(x, y, map.getLayerIndex("collision"));
- if (tileID != 0) {
- Polygon poly= new Polygon(new float[]{
- x*32,y*32,
- x*32+32,y*32,
- x*32+32,y*32+32,
- x*32,y*32+32
- });
- entities.add(poly);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement