I'm trying to make a roguelike in java to practice. This is my code to generate a floor (right now just a big room with wall tiles on the edge). I'm trying to set certain tiles in my tile array to either a wall tile or floor tile. Although when they leave the setTile method, they revert back to their value before entering the method. I'm going insane. Here's my code:
public Floor(int width, int height) {
this.tiles = new Tile[(width+1)*(height+1)];
this.width = width;
this.height = height;
generateTiles();
boolean test = false;
}
public Tile getTile(int x, int y)
{
return tiles[y * width + x];
}
public void setTile(int x, int y, Tile tile)
{
Tile tileToSet = getTile(x,y);
tileToSet = tile;
}
private void generateTiles() {
for (int i = 0; i < tiles.length; i++)
{
tiles[i] = new Tile();
}
//make the top wall
for (int i = 0; i<width;i++)
{
setTile(i,0,new WallTile());
}
}
}
Your setTile doesn't make sense. You're retrieving the tile which is currently at that position, storing it in a local variable tileToSet and then overwriting the value of that variable.
What you're trying to do is storing the given tile in the tiles array. Analogous to how getTile is implemented, you can do this with:
public void setTile(int x, int y, Tile tile)
{
tiles[y * width + x] = tile;
}
Note that this is not equivalent (but you seem to think it is) with:
public void setTile(int x, int y, Tile tile)
{
Tile tileToSet = tiles[y * width + x];
tileToSet = tile;
}
This code is setting the same variable twice and doing nothing with it.
public void setTile(int x, int y, Tile tile)
{
Tile tileToSet = getTile(x,y);
tileToSet = tile;
}
I think you want something like this:
public void setTile(int x, int y, Tile tile)
{
tiles[y * width + x] = tile;
}
That would change the value of what is stored in the tiles array to the supplied Tile object.
ya in Java when you pass an object into the function, the reference to that object is copied by value. What this means is you can't swap tile references.
What you'll need to do, is something like this:
public Floor(int width, int height) {
this.tiles = new Tile[(width+1)*(height+1)];
this.width = width;
this.height = height;
generateTiles();
boolean test = false;
}
public Tile getTile(int x, int y)
{
return tiles[y * width + x];
}
public void setTile(int x, int y, Tile tile)
{
tiles[y * width + x] = tile;//this works cuz it takes the ref from the array and assigns it the copy of the reference passed in
}
private void generateTiles() {
for (int i = 0; i < tiles.length; i++)
{
tiles[i] = new Tile();
}
//make the top wall
for (int i = 0; i<width;i++)
{
setTile(i,0,new WallTile());
}
}
}
Check out this article for explanation: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
Look at your setTile method:
public void setTile(int x, int y, Tile tile)
{
Tile tileToSet = getTile(x,y);
tileToSet = tile;
}
You are getting the tile value at x,y and set it to a local variable (tileToSet), then set the tile value to the variable tileToSet. Of course it doesn't change the tile at x,y. tileToSet is just a reference to the value, it is never a reference to the array element.
replace with this:
public void setTile(int x, int y, Tile tile)
{
tiles[y * width + x] = tile;
}
and if you want to have a method that returns tile index, like you said in your command, you can rewrite the get/set pair like this:
public void setTile(int x, int y, Tile tile)
{
tiles[getTileIndex(x, y)] = tile;
}
public Tile getTile(int x, int y)
{
tiles[getTileIndex(x, y)] = tile;
}
public int getTileIndex(int x, int y)
{
return y * width + x;
}
Related
I want to change the height of my texture to a random height (with a specified range), and I almost figured out how, but the problem I have is that the texture (tower) is now above the ground. I want to stretch the texture while it remains on the same position, but changes height length.
I have a Tower class and a Scrollable class. In the Tower class I generate a random height in the reset method, but the problem is that I don't know what exactly I have to add or write to put the texture on the correct position (so that it isn't above the ground).
Here is the Tower class:
public class Tower extends Scrollable {
private Random r;
// When Tower's constructor is invoked, invoke the super (Scrollable)
// constructor
public Tower(float x, float y, int width, int height, float scrollSpeed) {
super(x, y, width, height, scrollSpeed);
// Initialize a Random object for Random number generation
r = new Random();
}
#Override
public void reset(float newX) {
// Call the reset method in the superclass (Scrollable)
super.reset(newX); // newX
// Change the height to a random number
Random r = new Random();
int low = 0;
int high = 15;
int result = r.nextInt(high-low) + low;
height = result;
}
}
And here's the Scrollable class:
public class Scrollable {
protected Vector2 position;
protected Vector2 velocity;
protected int width;
protected int height;
protected boolean isScrolledLeft;
public Scrollable(float x, float y, int width, int height, float scrollSpeed) {
position = new Vector2(x, y);
velocity = new Vector2(scrollSpeed, 0);
this.width = width;
this.height = height;
isScrolledLeft = false;
}
public void update(float delta) {
position.add(velocity.cpy().scl(delta));
// If the Scrollable object is no longer visible:
if (position.x + width < 0) {
isScrolledLeft = true;
}
}
// Reset: Should Override in subclass for more specific behavior.
public void reset(float newX) {
position.x = newX;
isScrolledLeft = false;
}
public boolean isScrolledLeft() {
return isScrolledLeft;
}
public float getTailX() {
return position.x + width;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Maybe it's important to know that I have a GameRenderer class which has a drawTowers() method, which is then used in a render() method.
That's my drawTowers() method:
private void drawTowers() {
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight(),
tower1.getWidth(), midPointY - (tower1.getHeight()));
batcher.draw(AssetLoader.texture2, tower2.getX(), tower2.getY() + tower2.getHeight(),
tower2.getWidth(), midPointY - (tower2.getHeight()));
batcher.draw(AssetLoader.texture3, tower3.getX(), tower3.getY() + tower3.getHeight(),
tower3.getWidth(), midPointY - (tower3.getHeight()));
batcher.draw(AssetLoader.texture4, tower4.getX(), tower4.getY() + tower4.getHeight(),
tower4.getWidth(), midPointY - (tower4.getHeight()));
}
You are drawing the tower too high, you need to be adding half the height rather than the whole height.
Here in drawTowers():
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight() / 2, tower1.getWidth(), midPointY - (tower1.getHeight()));
Do the same for the other towers.
This may not be perfectly correct but it shouldn't be far off.
I have created an abstract shape class for 2d game but i am getting an error in both the shape classes. The error is something to do with super(). There maybe other errors as well. i have also shown where i get the error in the code. IS super() suitable to use.
Shape class
public abstract class Shape {
int Y;
int WIDTH;
int HEIGHT;
int DIAMETER;
public Shape(int Y, int WIDTH, int HEIGHT, int DIAMETER) {
this.Y = Y;
this.WIDTH = WIDTH;
this.HEIGHT = HEIGHT;
this.DIAMETER = DIAMETER;
}
public abstract void paint(Graphics g);
}
Racquet class
public class Racquet extends Shape {
int x = 0;
int xa = 0;
private Game game;
public Racquet(int Y, int WIDTH, int HEIGHT) {
super(Y, WIDTH, HEIGHT); // <- **Error Here**
}
public void move() {
if (x + xa > 0 && x + xa < game.getWidth() - this.WIDTH)
x = x + xa;
}
public void paint(Graphics r) {
r.setColor(new java.awt.Color(229, 144, 75));
r.fillRect(x, Y, this.WIDTH, this.HEIGHT);
}
public Rectangle getBounds() {
return new Rectangle(x, this.Y, this.WIDTH, this.HEIGHT);
}
public int getTopY() {
return this.Y - this.HEIGHT;
}
}
Ball Class
import java.awt.*;
public class Ball extends Shape {
int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
private Game game;
public Ball(Integer DIAMETER) {
super(DIAMETER); // <- **Error Here**
}
void move() {
if (x + xa < 0)
xa = game.speed;
if (x + xa > game.getWidth() - this.DIAMETER)
xa = -game.speed;
if (y + ya < 0)
ya = game.speed;
if (y + ya > game.getHeight() - this.DIAMETER)
game.CheckScore();
if (collision()) {
ya = -game.speed;
y = game.racquet.getTopY() - this.DIAMETER;
game.speed++;
}
x = x + xa;
y = y + ya;
}
private boolean collision() {
return game.racquet.getBounds().intersects(getBounds());
}
public void paint(Graphics b) {
b.setColor(new java.awt.Color(237, 238, 233));
b.fillOval(x, y, this.DIAMETER, this.DIAMETER);
}
public Rectangle getBounds() {
return new Rectangle(x, y, this.DIAMETER, this.DIAMETER);
}
}
Thanks a lot.
By calling super(...), you are actually calling constructor of super class. In the super class you have only one constructor which expects 4 parameters: Shape(int Y, int WIDTH, int HEIGHT, int DIAMETER), so you either have to provide 4 parameters when calling super(...), or provide needed constructors in the super class, with 3 parameters and with 1 parameter
Your Shape class doesn't have constructor with three parameters or one parameters.
You may want to use;
in recquet class
super(Y, WIDTH, HEIGHT, 0);
in Ball class
super(0, 0, 0, DIAMETER);
Shape doesn't have constructors that fit the parameters you are using in Racquet and Ball.
From a "best-practice" perspective, since Ball and Racquet should logically be constructed differently, it may be better to use composition, rather than inheritance.
I just want to create a simple game with 100 x 100 square, each square is 5 pixels.
I created a class:
public class Draw extends JComponent{
private List<Graphics2D> recList = new ArrayList<Graphics2D>();
public void paint(Graphics g) {
//THIS TO SET (0,0) PANEL START AT BOTTOM LEFT
Graphics2D g2 = (Graphics2D)g;
AffineTransform at = g2.getTransform();
at.translate(0, getHeight());
at.scale(1, -1);
g2.setTransform(at);
//THIS TO DRAW ALL THE SQUARES
for (int i = 0;i<100;i++){
for (int j=0;j<100;j++){
g2.setColor(Color.red);
g2.drawRect(5*i, 5*j, 5, 5);
recList.add(g2); //Store each square to the list to change the color
}
}
}
}
Then I just drag it to the design windows of netbeans and the squares are painted, looks good...
But it seems like I made a wrong move. At the first time I wanted to get a specific square from the list using their location, but the Graphic2d doesn't have any method to get the location (x and y) or to change the color.
I don't know if there is any other way I can make it come true?
PS: One more thing, can I set the location of each square to its center?
You could create your own Tile class, which stores info such as x, y, width, height, and color. Each Tile object could also be in charge of painting itself:
class Tile {
private int x, y, width, height;
private Color color;
public Tile(int x, int y, int width, int height, Color color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void paint(Graphics g) {
g.setColor(color);
g.fillRect(x, y, width, height);
}
}
Create the tiles before-hand:
List<Tile> tiles = ...;
void createTiles() {
for(int x = 0; x < 100; x++) {
for(int y = 0; y < 100; y++) {
Color color = ...; //choose color
int size = 5;
int tileX = x * size;
int tileY = y * size;
tiles.add(new Tile(tileX, tileY, size, size, color));
}
}
}
Then render by passing the graphics object to them in the paint method:
void paint(Graphics g) {
tiles.forEach(tile -> tile.paint(g));
}
I am making a 2d rpg game in java and I have run into a problem. I can make the player move around the stage and I have rocks, trees, walls, etc. on the stage as well. I don't know how to detect the collision and make it to where the player can't move through the object. The code that reads map file and draws image on the canvas is as follows:
public void loadLevel(BufferedImage levelImage){
tiles = new int[levelImage.getWidth()][levelImage.getHeight()];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
Color c = new Color(levelImage.getRGB(x, y));
String h = String.format("%02x%02x%02x", c.getRed(),c.getGreen(),c.getBlue());
switch(h){
case "00ff00"://GRASS Tile - 1
tiles[x][y] = 1;
break;
case "808080"://Stone -2
tiles[x][y] = 2;
break;
case "894627"://Dirt -3
tiles[x][y] = 3;
break;
case "404040"://Rock on Grass -4
tiles[x][y] = 4;
break;
case "00b700"://Tree -5
tiles[x][y] = 5;
break;
case"000000"://Wall -6
tiles[x][y] = 6;
break;
case "cccccc"://Rock on stone -7
tiles[x][y] = 7;
break;
default:
tiles[x][y] = 1;
System.out.println(h);
break;
}
}
}
}
And the player class is as follows:
public class Player {
private int x,y;
public int locx,locy;
private Rectangle playerR;
private ImageManager im;
public boolean up =false,dn = false,lt=false,rt=false,moving = false,canMove = true;
private final int SPEED =2;
public Player(int x, int y, ImageManager im){
this.x = x;
this.y = y;
this.im = im;
locx = x;
locy = y;
playerR = new Rectangle(x,y,16,16);
}
public void tick(){
if (up) {
if(canMove){
y -= SPEED;
locx = x;
locy = y;
playerR.setLocation(locx, locy);
moving = true;
}
else{
y += 1;
canMove=true;
}
}
if (dn) {
y +=SPEED;
locx = x;
locy = y;
moving = true;
}
}
if (lt) {
x -= SPEED;
locx = x;
locy = y;
moving = true;
}
if (rt) {
x+=SPEED;
locx = x;
locy = y;
moving = true;
}
}
if(moving){
System.out.println("PLAYER\tX:"+locx+" Y:"+locy);
moving = false;
}
}
public void render(Graphics g){
g.drawImage(im.player, x, y, Game.TILESIZE*Game.SCALE, Game.TILESIZE*Game.SCALE, null);
}
}
I don't really know how to do collision, but i googled it and people said to make a rectangle for the player and all the objects that the player should collide with, and every time the player moves, move the player's rectangle. Is this the right way to do this?
EDIT EDIT EDIT EDIT
code for when collision is true:
if (rt) {
x+=SPEED;
locx = x;
locy = y;
playerR.setLocation(locx, locy);
for(int i = 0;i<Level.collisions.size();i++){
if(intersects(playerR,Level.collisions.get(i))==true){
x-=SPEED;
locx = x;
playerR.setLocation(locx, locy);
}
}
moving = true;
}
And the intersects method is as follows:
private boolean intersects(Rectangle r1, Rectangle r2){
return r1.intersects(r2);
}
I'm going to focus on your tick method since that is where most of this logic is going. There are a couple changes here. Most notably, we only move the rectangle before checking for collisions. Then loop through all the collideable objects in your level. Once one is found, we reset our x and y and break out of the loop (no sense in looking at any of the other objects since we already found the one we collided with). Then we update our player position. By doing it this way, I centralized the code so it is not being repeated. If you ever see yourself repeating code, there is a pretty good chance that it can be pulled out to a common place, or to a method.
public void tick() {
if (up) {
y -= SPEED;
} else if (dn) {
y += SPEED;
} else if (lt) {
x -= SPEED;
} else if (rt) {
x += SPEED;
}
playerR.setLocation(x, y);
for (Rectangle collideable : Level.collisions) {
if (intersects(playerR, collideable)) {
x = locx;
y = locy;
playerR.setLocation(x, y);
break;
}
}
locx = x;
locy = y;
}
There are different ways to do that. As you talk about a "rpg" i think your view is Isometric (45° top down).
I would do the collision detection in pure 90° top down, as it is easier and, imho, more realistic.
We have 2 possibilities:
Move your Player to the next position. If there is a collision, reset his position.
Calculate the next position, if there would be a collision don't move.
If you want to have a "gliding" collision response, you have to check in which axis the collision will happen, and stop / reset movement for this axis only.
To have a more efficient collision detection only check near objects, which will possibly collide.
Do this by comparing a squared "dangerRadius" with the squared distance between your player and the object:
if ((player.x - object.x)² + (player.y - object.y)² <= dangerRadius²)
// Check for intersection
This will sort out most of the objects by using a simple calculation of:
2 subtractions
1 addition
3 multiplications (the ²)
1 compare (<=)
In your game you should sepparate the logic and the view. So basicly you don't detect, if the two images overlapp, but you check, if the objects in your logic overlap. Then you draw the images on the right position.
Hope this helps.
EDIT: Important: If you update your character, depending on the time between the last and this frame (1/FPS) you have to limit the max timestep. Why? Because if for some reason (maybe slow device?) the FPS are really low, it is possible, that the character moves verry far between 2 frames and for that he could go through an object in 1 frame.
Also if you simply reset the movement on collision or just don't move the distance between you and the object could be big for low FPS. For normal FPS and not to high movementspeed this won't happen/ be noticeable.
I personally am fairly new to Java, though I have worked with C# in the past. I am making a similar game, and for collision detection I just check the locations of the player and objects:
if (z.gettileX() == p.gettileX()){
if (z.gettileY() == p.gettileY()){
System.out.println("Collision!");
}
}
If the player (p) has equal X coordinates and Y coordinates to z(the bad guy), it will send this message and confirm that the two have, in fact, collided. If you can make it inherent in the actual class behind z to check if the coordinates a equal, you can create an unlimited number of in-game objects that detect collision and react in the same way, i.e. walls.
This is probably what your looking for. I've made this class spicificly for collision of multiple objects and for individual side collisions.
abstract class Entity {
private Line2D topLine;
private Line2D bottomLine;
private Line2D leftLine;
private Line2D rightLine;
private Rectangle rectangle;
private Entity entity;
protected boolean top;
protected boolean bottom;
protected boolean left;
protected boolean right;
protected int x;
protected int y;
protected int width;
protected int height;
public Entity(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
updateLinesAndRects();
}
public void updateLinesAndRects() {
topLine = new Line(x + 1, y, width - 2, 0);
bottomLine = new Line(x + 1, y + height, width - 2, height);
leftLine = new Line(x, y + 1, 0, height - 2);
rightLine = new Line(x + width, y + 1, 0, height - 2);
rectangle = new Rectangle(x, y, width, height)
}
public void setCollision(Entity entity) {
this.entity = entity;
top = isColliding(new Line2D[]{topLine, bottomLine, leftLine, rightLine});
bottom = isColliding(new Line2D[]{bottomLine, topLine, leftLine, rightLine});
left = isColliding(new Line2D[]{leftLine, topLine, bottomLine, rightLine});
right = isColliding(new Line2D[]{rightLine, topLine, bottomLine, leftLine});
}
public void updateBounds() {
if(top) y = entity.y + entity.height;
if(bottom) y = entity.y - height;
if(left) x = entity.x + entity.width;
if(right) x = entity.x - width;
}
public boolean isColliding() {
return rectangle.intersects(entity.rect);
}
private boolean isLinesColliding(Line2D[] lines) {
Rectangle rect = entity.getRectangle();
return lines[0].intersects(rect) && !lines[1].intersects(rect) && !lines[2].intersects(rect) && !lines[3].intersects(rect);
}
private Line2D line(float x, float y, float width, float height) {
return new Line2D(new Point2D.Float(x, y), new Point2D.Float(x + width, x + height));
}
public Rectangle getRectangle() {
return rectangle;
}
}
Example:
class Player extends Entity{
Entity[] entities;
public Player(int x, int y, int width, int height) {
super(x, y, width, height);
}
public void update() {
updateLinesAndRects();
for(Entity entity : entities) {
setCollision(entity);
if(top) system.out.println("player is colliding from the top!");
if(isColliding()) system.out.println("player is colliding!");
updateBounds(); // updates the collision bounds for the player from the entities when colliding.
}
}
public void setEntities(Entity[] entities) {
this.entities = entities;
}
}
I'm stuck on something that is usually really simple. I'm getting a NullPointerException when calling this simple class's constructor:
import java.awt.geom.*;
public class Brick extends ColorShape {
private int xPos = 0;
private int yPos = 0;
private int width = 0;
private int height = 0;
private Rectangle2D.Double shape;
// constructor
public Brick(int x, int y, int w, int h) {
super(new Rectangle2D.Double(x, y, w, h));
//set brick x, y, width, and height
xPos = x;
yPos = y;
width = w;
height = h;
// update shape
shape.setRect((double)xPos, (double)yPos, (double)width, (double)height);
}
public int getX() {
return xPos;
}
public Rectangle2D.Double getShape() {
return shape;
}
}
It gets called this way:
for (int i = 0; i < numCols; i++) {
for (int j = 0; j < numRows; j++) {
// initialize bricks[i][j]
bricks[i][j].setLocation((double)(i*brickWidth), (double)(j*brickHeight));
bricks[i][j].setSize((double)brickWidth, (double)brickHeight);
//bricks[i][j] = new Brick(i*brickWidth, j*brickHeight, brickWidth, brickHeight);
//bricks[i][j] = new Brick(0, 0, 0, 0);
}
}
No matter what I try, I always get a NullPointerException trying to initialize that class.
EDIT:
Tristan's suggestions along with changing the nested for loops to the code below fixed it
// create new bricks and store them in bricks array
for (int i = 0; i < numCols; i++) {
for (int j = 0; j < numRows; j++) {
// initialize bricks[i][j]
//bricks[i][j].setLocation((double)(i*brickWidth), (double)(j*brickHeight));
//bricks[i][j].setSize((double)brickWidth, (double)brickHeight);
bricks[i][j] = new Brick(i*brickWidth, j*brickHeight, brickWidth, brickHeight);
//bricks[i][j] = new Brick(0, 0, 0, 0);
}
}
I think you are accidentally redeclaring shape as an uninitialized field. The shape you are calling setRect on has not been initialized the way you think it has.
If you have a shape in the parent class that you are trying to access, simply set its modifier to protected and remove the private shape declaration in the class you posted.
/* REMOVE THIS */
private Rectangle2D.Double shape; // uninitialized!
// constructor
public Brick(int x, int y, int w, int h) {
super(new Rectangle2D.Double(x, y, w, h));
//set brick x, y, width, and height
xPos = x;
yPos = y;
width = w;
height = h;
// update shape
// This now references a protected instance variable inherited from the parent.
shape.setRect((double)xPos, (double)yPos, (double)width, (double)height);
}
However looking through this constructor, it seems rather off. If it is the case that the parent class has a shape itself, why do you need to set the rectangle any differently than the way you set it in the parent class?
For example, this code appears to be logically equivalent.
// Call the parents constructor to set the shape of this brick..
public Brick(int x, int y, int w, int h) {
super(new Rectangle2D.Double(x, y, w, h));
}
As mentioned by Tristan, the problem with the initial brick constructor is that shape has been declared but not instantiated.
That said, it is simple to instantiate shape:
public Brick(int x, int y, int w, int h) {
super(new Rectangle2D.Double(x, y, w, h));
//set brick x, y, width, and height
xPos = x;
yPos = y;
width = w;
height = h;
// update shape
// This now references a protected instance variable inherited from the parent.
shape = (Rectangle2D.Double)super.shape;
shape.setRect(xPos, yPos, width, height);
}