Skip to main content
added 2963 characters in body
Source Link

I no longer have the portal class implemented, so can't show code from it. However hereHere is the code im using to render it as a tile:

EDIT: After attempting to reintroduce the portal class in the way that @Reg suggested, i had the following error:

Portal Class:

package com.ritcat14.GotYourSix.entity;

import com.ritcat14.GotYourSix.Game;
import com.ritcat14.GotYourSix.graphics.Screen;
import com.ritcat14.GotYourSix.graphics.Sprite;
import com.ritcat14.GotYourSix.level.Level;

public class Portal extends Entity {

    private int x = 0, y = 0;
    private Level currLevel = null, locationLevel = null;

    public static Portal Level1 = new Portal(1,1,Level.spawn, Level.level1);
    public static Portal Level2 = new Portal(2,1,Level.spawn, Level.level2);
    public static Portal Level3 = new Portal(3,1,Level.spawn, Level.level3);

    public Portal(int x, int y, Level currLevel, Level locationLevel){
        this.x = x;
        this.y = y;
        this.currLevel = currLevel;
        this.locationLevel = locationLevel;
        sprite = Sprite.portal;
    }

    public Portal(int x, int y, Level currLevel){
        this.x = x;
        this.y = y;
        this.locationLevel = Level.spawn;
        this.currLevel = currLevel;
        sprite = Sprite.portal;
    }

    public void update(){
        System.out.println(this);
        // --> null?? System.out.println(currLevel);
        System.out.println(currLevel.getClientPlayer());
        System.out.println(currLevel.getClientPlayer().getBounds());
        if     (this.getBounds().intersects(currLevel.getClientPlayer().getBounds())) {
          Game.getGame().changeLevel(locationLevel);
          remove();
        }
    }

    public void render(Screen screen){
        screen.renderSprite(x, y, sprite, true);
    } 
}

The change level method works fine, the issue seems to be that the current level doesn't actually exist. This is how im adding the portals to the level in the Level.spawn level:

    public class SpawnLevel extends Level {

    public SpawnLevel(String path) {
        super(path, false);
    }

    protected void loadLevel(String path){
        try{
            BufferedImage image = ImageIO.read(SpawnLevel.class.getResource(path));
            int w = width = image.getWidth();
            int h = height = image.getHeight();
            tiles = new int[w * h];
            image.getRGB(0, 0, w, h, tiles, 0, w);
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("Failed to load level file.");
        }
     generateLevel();
    }

     public void setPlayerLocation(){
      for (int i = 0; i < getPlayers().size(); i ++){
          getPlayers().get(i).setLocation(new Vector2i(5 * 16, 70 * 16));
      }
   }

  protected void generateLevel(){
    add(Portal.Level1);
    add(Portal.Level2);
    add(Portal.Level3);
    //no enemies!
  }
}

Any ideas? The currLevel is apparently null

I no longer have the portal class implemented, so can't show code from it. However here is the code im using to render it as a tile:

Here is the code im using to render it as a tile:

EDIT: After attempting to reintroduce the portal class in the way that @Reg suggested, i had the following error:

Portal Class:

package com.ritcat14.GotYourSix.entity;

import com.ritcat14.GotYourSix.Game;
import com.ritcat14.GotYourSix.graphics.Screen;
import com.ritcat14.GotYourSix.graphics.Sprite;
import com.ritcat14.GotYourSix.level.Level;

public class Portal extends Entity {

    private int x = 0, y = 0;
    private Level currLevel = null, locationLevel = null;

    public static Portal Level1 = new Portal(1,1,Level.spawn, Level.level1);
    public static Portal Level2 = new Portal(2,1,Level.spawn, Level.level2);
    public static Portal Level3 = new Portal(3,1,Level.spawn, Level.level3);

    public Portal(int x, int y, Level currLevel, Level locationLevel){
        this.x = x;
        this.y = y;
        this.currLevel = currLevel;
        this.locationLevel = locationLevel;
        sprite = Sprite.portal;
    }

    public Portal(int x, int y, Level currLevel){
        this.x = x;
        this.y = y;
        this.locationLevel = Level.spawn;
        this.currLevel = currLevel;
        sprite = Sprite.portal;
    }

    public void update(){
        System.out.println(this);
        // --> null?? System.out.println(currLevel);
        System.out.println(currLevel.getClientPlayer());
        System.out.println(currLevel.getClientPlayer().getBounds());
        if     (this.getBounds().intersects(currLevel.getClientPlayer().getBounds())) {
          Game.getGame().changeLevel(locationLevel);
          remove();
        }
    }

    public void render(Screen screen){
        screen.renderSprite(x, y, sprite, true);
    } 
}

The change level method works fine, the issue seems to be that the current level doesn't actually exist. This is how im adding the portals to the level in the Level.spawn level:

    public class SpawnLevel extends Level {

    public SpawnLevel(String path) {
        super(path, false);
    }

    protected void loadLevel(String path){
        try{
            BufferedImage image = ImageIO.read(SpawnLevel.class.getResource(path));
            int w = width = image.getWidth();
            int h = height = image.getHeight();
            tiles = new int[w * h];
            image.getRGB(0, 0, w, h, tiles, 0, w);
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("Failed to load level file.");
        }
     generateLevel();
    }

     public void setPlayerLocation(){
      for (int i = 0; i < getPlayers().size(); i ++){
          getPlayers().get(i).setLocation(new Vector2i(5 * 16, 70 * 16));
      }
   }

  protected void generateLevel(){
    add(Portal.Level1);
    add(Portal.Level2);
    add(Portal.Level3);
    //no enemies!
  }
}

Any ideas? The currLevel is apparently null

added 786 characters in body
Source Link

I don't mean to ask a broad question, so let me explain. I'm creating a 2D top-down design game and I'd like to implement "portals" to be able to switch between worlds. Each world is a level class, and at the moment portals are rendered as tiles, and changing world is handled in the player collision code. However, I have no way of determining which portal is dedicated to which level. I would perhaps also like to have the ability to lock portals until a player becomes a certain rank. I've tried to set up a portal class, and implemented it as entities that were spawned during level creation at the location of the portal tiles, and then all collision handling was passed to that class. It took in params of currLevel, targetLevel, x and y. When the player collided, it would change to the set targetLevel. However, when I ran the code and collided with the portal, the game just froze. There was no error output, ups was at 60 as standard and the fps was at about 3000 as standard. The terminal was also outputting as normal, but the game was doing nothing.

To conclude, I'd like to know if there is a better way to go about completing this? I may have just missed something.

Thanks

I no longer have the portal class implemented, so can't show code from it. However here is the code im using to render it as a tile:

(Level class):

if (tiles[x + y * width] == Tile.col_portal)
        return Tile.portal;

(in Mob class, for player collision):

        if (this instanceof Player) {
            if (level.getTile(ix, iy) instanceof SpawnWaterTile) {
                Player.swimming = true;
                Player.canShoot = false;
            } else if (level.getTile(ix,iy) instanceof SpawnPortalTile) {
                Game.getGame().changeLevel(Level.level1);
            } else {
                Player.swimming = false;
                Player.canShoot = true;
            }
        }

I don't mean to ask a broad question, so let me explain. I'm creating a 2D top-down design game and I'd like to implement "portals" to be able to switch between worlds. Each world is a level class, and at the moment portals are rendered as tiles, and changing world is handled in the player collision code. However, I have no way of determining which portal is dedicated to which level. I would perhaps also like to have the ability to lock portals until a player becomes a certain rank. I've tried to set up a portal class, and implemented it as entities that were spawned during level creation at the location of the portal tiles, and then all collision handling was passed to that class. It took in params of currLevel, targetLevel, x and y. When the player collided, it would change to the set targetLevel. However, when I ran the code and collided with the portal, the game just froze. There was no error output, ups was at 60 as standard and the fps was at about 3000 as standard. The terminal was also outputting as normal, but the game was doing nothing.

To conclude, I'd like to know if there is a better way to go about completing this? I may have just missed something.

Thanks

I don't mean to ask a broad question, so let me explain. I'm creating a 2D top-down design game and I'd like to implement "portals" to be able to switch between worlds. Each world is a level class, and at the moment portals are rendered as tiles, and changing world is handled in the player collision code. However, I have no way of determining which portal is dedicated to which level. I would perhaps also like to have the ability to lock portals until a player becomes a certain rank. I've tried to set up a portal class, and implemented it as entities that were spawned during level creation at the location of the portal tiles, and then all collision handling was passed to that class. It took in params of currLevel, targetLevel, x and y. When the player collided, it would change to the set targetLevel. However, when I ran the code and collided with the portal, the game just froze. There was no error output, ups was at 60 as standard and the fps was at about 3000 as standard. The terminal was also outputting as normal, but the game was doing nothing.

To conclude, I'd like to know if there is a better way to go about completing this? I may have just missed something.

Thanks

I no longer have the portal class implemented, so can't show code from it. However here is the code im using to render it as a tile:

(Level class):

if (tiles[x + y * width] == Tile.col_portal)
        return Tile.portal;

(in Mob class, for player collision):

        if (this instanceof Player) {
            if (level.getTile(ix, iy) instanceof SpawnWaterTile) {
                Player.swimming = true;
                Player.canShoot = false;
            } else if (level.getTile(ix,iy) instanceof SpawnPortalTile) {
                Game.getGame().changeLevel(Level.level1);
            } else {
                Player.swimming = false;
                Player.canShoot = true;
            }
        }
Source Link

How would i go about implementing portals into my game?

I don't mean to ask a broad question, so let me explain. I'm creating a 2D top-down design game and I'd like to implement "portals" to be able to switch between worlds. Each world is a level class, and at the moment portals are rendered as tiles, and changing world is handled in the player collision code. However, I have no way of determining which portal is dedicated to which level. I would perhaps also like to have the ability to lock portals until a player becomes a certain rank. I've tried to set up a portal class, and implemented it as entities that were spawned during level creation at the location of the portal tiles, and then all collision handling was passed to that class. It took in params of currLevel, targetLevel, x and y. When the player collided, it would change to the set targetLevel. However, when I ran the code and collided with the portal, the game just froze. There was no error output, ups was at 60 as standard and the fps was at about 3000 as standard. The terminal was also outputting as normal, but the game was doing nothing.

To conclude, I'd like to know if there is a better way to go about completing this? I may have just missed something.

Thanks