Show Posts
|
|
Pages: [1] 2
|
|
2
|
Game Development / Newbie & Debugging Questions / Re: Tile based movement (Bomberman style)
|
on: 2011-12-07 02:44:34
|
yes I did that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public void headTowardsDestination(){ if(!this.destinationReached){ int currentX = this.currentPosition.getX(); int currentY = this.currentPosition.getY(); int destinationX = this.destinationPosition.getX(); int destinationY = this.destinationPosition.getY(); if(currentX < destinationX){ this.currentPixelX++; }else if(currentX > destinationX){ this.currentPixelX--; }else if(currentY < destinationY){ this.currentPixelY++; }else if(currentY > destinationY){ this.currentPixelY--; } } } |
But where should should I assign destinationReached = false; ?
|
|
|
|
|
4
|
Game Development / Newbie & Debugging Questions / Re: Tile based movement (Bomberman style)
|
on: 2011-12-07 00:01:58
|
This is my unit class, and the getters and setters for each field: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public abstract class Unit implements WorldObject{ private Position currentPosition; private Position destinationPosition; private String name; private String type; private int strength; private int agility; private int vitality; private int xp; private int level; private int chips; private double health; private double attackDamage; private double dodgeRating; private int luck; private int attackRange; private double maxHealth; private String unitClass; private Vector direction; private int energy; private BufferedImage image; private int currentPixelX; private int currentPixelY; private boolean visible; private boolean isMoving;
public Unit(Position p, String name, String type) { this.currentPosition = p; this.setCurrentPixelX(0); this.setCurrentPixelY(0); this.name = name; this.type = type; this.direction = new Vector(1,0); loadImage(); }
private void loadImage() { this.image = null; try{ this.image = ImageIO.read(new File("bin/image/unit/" + this.type + ".png")); }catch (Exception e) { System.out.println("Couldn't find " + this.type + "'s image file"); } } } |
|
|
|
|
|
5
|
Game Development / Newbie & Debugging Questions / Re: Tile based movement (Bomberman style)
|
on: 2011-12-06 05:52:11
|
So I'm back to square one, tried to draw units in a separate loop, but couldn't get it to work. Is there any other approach, or would you get me started at drawing them the correct way? This is my "old" render method: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public void render() { Graphics2D bbg = (Graphics2D) this.backBuffer.getGraphics(); BufferedImage image = null;
this.player = this.game.getTileMap().getPlayer(); this.inventory = player.getInventory(); nullTile = new Tile(position, "WATER"); this.unitHeightOffset = 0; for(int y = 0; y < 24; y++){ for(int x = 0; x < 42; x++){ int unitHeightOffset = 0; position.setX(x + player.getPosition().getX() - this.xOffset); position.setY(y + player.getPosition().getY() - this.yOffset); Tile tile = this.game.getTileMap().getTileAt(position); if(tile != null){ image = tile.getImage(); }else{ image = nullTile.getImage(); }
Item item = this.game.getTileMap().getItemAt(position); if(item != null){ image = item.getImage(); }
Building building = this.game.getTileMap().getBuildingAt(position); if(building != null){ image = building.getImage(); }
Unit unit = this.game.getTileMap().getUnitAt(position); if(unit != null){ image = unit.getImage(); unitHeightOffset = -40; unit.setVisible(true); }
bbg.drawImage(image, x*tileDimension, y*tileDimension+unitHeightOffset, this);
} }
bbg.drawImage(inventory.getImage(), 400, 480, this);
bbg.setColor(Color.WHITE); bbg.fillRect(0, 580, 40, 20); bbg.setColor(Color.BLACK); bbg.drawString(String.valueOf(gui.getFps()), 10, 595);
bbg.dispose(); } |
|
|
|
|
|
8
|
Game Development / Newbie & Debugging Questions / Re: Tile based movement (Bomberman style)
|
on: 2011-12-05 09:55:37
|
This is now my render method: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| public void render() { Graphics2D bbg = (Graphics2D) this.backBuffer.getGraphics(); BufferedImage image = null;
this.player = this.game.getTileMap().getPlayer(); this.units = this.game.getTileMap().getUnits(); this.inventory = player.getInventory(); nullTile = new Tile(position, "WATER"); for(int y = 0; y < 24; y++){ for(int x = 0; x < 42; x++){ position.setX(x + player.getPosition().getX()-xOffset); position.setY(y + player.getPosition().getY()-yOffset); Tile tile = this.game.getTileMap().getTileAt(position); if(tile != null){ image = tile.getImage(); }else{ image = nullTile.getImage(); }
Item item = this.game.getTileMap().getItemAt(position); if(item != null){ image = item.getImage(); }
Building building = this.game.getTileMap().getBuildingAt(position); if(building != null){ image = building.getImage(); }
bbg.drawImage(image, x*tileDimension, y*tileDimension, this);
} } for(Unit unit : units){ image = unit.getImage(); bbg.drawImage(image, unit.getPosition().getX() + unit.getCurrentPixelX() + (xOffset * tileDimension), unit.getPosition().getY() + unit.getCurrentPixelY() + (yOffset * tileDimension) - unitHeightOffset, this); }
bbg.drawImage(inventory.getImage(), 400, 480, this);
bbg.setColor(Color.WHITE); bbg.fillRect(0, 580, 40, 20); bbg.setColor(Color.BLACK); bbg.drawString(String.valueOf(gui.getFps()), 10, 595);
bbg.dispose(); } |
My move method: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public boolean moveUnitToAnimated(Position from, Position to){ Tween tween = new Tween(); if(isAllowedUnitPosition(to)){ Unit unit = this.tileMap.getUnitAt(from); this.tileMap.removeUnit(from); unit.setDirection(new Vector(to.getX() + from.getX(), to.getY() + from.getY())); if(to.getX() < from.getX() && to.getY() == from.getY()){ tween.tweenUnitLeft(unit); } else if(to.getX() > from.getX() && to.getY() == from.getY()){ tween.tweenUnitLRight(unit); } else if(to.getX() == from.getX() && to.getY() < from.getY()){ tween.tweenUnitUp(unit); } else if(to.getX() == from.getX() && to.getY() > from.getY()){ tween.tweenUnitDown(unit); } this.tileMap.addUnitAt(to, unit); this.tileMap.getTileAt(from).setOccupied(false); this.tileMap.getTileAt(to).setOccupied(true); return true; } return false; } |
And my tween class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class Tween {
public void tweenUnitLeft(Unit unit){ for(int i = 20; i > 0; i--){ unit.setCurrentPixelX(i); } unit.setPosition(new Position(unit.getPosition().getX() - 1, unit.getPosition().getY())); unit.setCurrentPixelX(0); } public void tweenUnitLRight(Unit unit){ for(int i = 0; i < 0; i++){ unit.setCurrentPixelX(i); } unit.setPosition(new Position(unit.getPosition().getX() + 1, unit.getPosition().getY())); unit.setCurrentPixelX(0); } public void tweenUnitUp(Unit unit){ for(int i = 20; i > 0; i--){ unit.setCurrentPixelY(i); } unit.setPosition(new Position(unit.getPosition().getX(), unit.getPosition().getY() - 1)); unit.setCurrentPixelY(0); } public void tweenUnitDown(Unit unit){ for(int i = 0; i < 20; i++){ unit.setCurrentPixelX(i); } unit.setPosition(new Position(unit.getPosition().getX(), unit.getPosition().getY() + 1)); unit.setCurrentPixelY(0); } } |
Is that anything what you meant? 
|
|
|
|
|
9
|
Game Development / Newbie & Debugging Questions / Re: Tile based movement
|
on: 2011-12-05 00:39:26
|
unit.setPosition(to) sets the unit on the next tile, no? You need to just set the unit's position (say) 1 pixel towards the destination.
Yes it does, it is that which gives the teleporting effect, but I think my problem is, that I draw not in pixels, but tiles: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| public void render() { Graphics2D bbg = (Graphics2D) this.backBuffer.getGraphics(); BufferedImage image = null;
this.player = this.game.getTileMap().getPlayer(); this.inventory = player.getInventory(); nullTile = new Tile(position, "WATER"); for(int y = 0; y < 24; y++){ for(int x = 0; x < 42; x++){ int yOffset = 0; position.setX(x + player.getPosition().getX()-21); position.setY(y + player.getPosition().getY()-12); Tile tile = this.game.getTileMap().getTileAt(position); if(tile != null){ image = tile.getImage(); }else{ image = nullTile.getImage(); }
Item item = this.game.getTileMap().getItemAt(position); if(item != null){ image = item.getImage(); }
Building building = this.game.getTileMap().getBuildingAt(position); if(building != null){ image = building.getImage(); }
Unit unit = this.game.getTileMap().getUnitAt(position); if(unit != null){ image = unit.getImage(); yOffset = -40; }
bbg.drawImage(image, x*20, y*20+yOffset, this);
} }
bbg.drawImage(inventory.getImage(), 400, 480, this);
bbg.setColor(Color.WHITE); bbg.fillRect(0, 580, 40, 20); bbg.setColor(Color.BLACK); bbg.drawString(String.valueOf(gui.getFps()), 10, 595);
bbg.dispose(); } |
Or am I totally wrong?  I really want a bomberman like movement. If you press a movement key, then it animates a movement between tiles...
|
|
|
|
|
10
|
Game Development / Newbie & Debugging Questions / Re: Tile based movement
|
on: 2011-12-05 00:17:06
|
This is my move method so far, it's kind of teleporting from one tile to another... I can't figure out to do those tweens, as my draw method draws in tiles, and not pixels. How do I get started?  1 2 3 4 5 6 7 8 9 10 11 12 13
| public boolean moveUnitTo(Position from, Position to) { if(isAllowedUnitPosition(to)){ Unit unit = this.tileMap.getUnitAt(from); this.tileMap.removeUnit(from); unit.setDirection(new Vector(to.getX() + from.getX(), to.getY() + from.getY())); unit.setPosition(to); this.tileMap.addUnitAt(to, unit); this.tileMap.getTileAt(from).setOccupied(false); this.tileMap.getTileAt(to).setOccupied(true); return true; } return false; } |
|
|
|
|
|
14
|
Game Development / Newbie & Debugging Questions / [SOLVED minor bugs] Tile based movement (Bomberman style)
|
on: 2011-12-01 03:11:11
|
So I have this tile based game, with each tile being 20x20 pixels, and I draw them on a canvas in a loop like this: 1 2 3 4 5 6 7
| for(int y = 0; y < 24; y++){ for(int x = 0; x < 42; x++){ int yOffset = 0; position.setX(x + player.getPosition().getX()-21); position.setY(y + player.getPosition().getY()-12); } |
I now wonder how to make my player move smoothly from tile to tile when I press a button on my keyboard Any ideas how to start? To get me going? 
|
|
|
|
|
23
|
Game Development / Newbie & Debugging Questions / Re: Having trouble with drawing the right way (Top-Down Tile Game)
|
on: 2011-11-30 15:51:18
|
I already use ImageIO, but it's in the tile class, and I use .png This is the code I use in Tile.class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public BufferedImage getImage() { BufferedImage image = null; try{ image = ImageIO.read(new File("bin/image/tile/" + this.type + ".png")); }catch (Exception e) { System.out.println("Couldn't find " + this.type + "'s image file!"); } if(this.isTargeted){ Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.red); g2d.fill(new Ellipse2D.Float(0, 0, 20, 20)); g2d.dispose(); } return image; } |
|
|
|
|
|
25
|
Game Development / Newbie & Debugging Questions / [Solved]Having trouble with drawing the right way (Top-Down Tile Game)
|
on: 2011-11-30 14:17:53
|
I'm having trouble drawing the right way it seems, it takes about 500-800 milliseconds to complete a single loop in my render method. Any suggestions or links that might explain what to do. Can't find anything on the forum, please help!  This is my render code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| public class GamePanel extends Canvas{
private BufferedImage backBuffer; private int windowHeight; private int windowWidth; private ZompocalypseGame game; private GUI gui; private Position position;
public GamePanel(int windowWidth, int windowHeight, ZompocalypseGame sGame, GUI gui){ setIgnoreRepaint(true); this.windowWidth = windowWidth; this.windowHeight = windowHeight; setVisible(true); this.game = sGame; this.gui = gui; this.position = new Position(0,0); this.backBuffer = new BufferedImage(this.windowWidth, this.windowHeight, BufferedImage.TYPE_INT_RGB); }
public void draw(){ Graphics2D g = (Graphics2D) getGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, windowWidth, windowHeight); g.drawImage(this.backBuffer, 0, 0, this); g.dispose(); }
public void render() { Graphics2D bbg = (Graphics2D) this.backBuffer.getGraphics(); BufferedImage image = null; Player player = this.game.getTileMap().getPlayer();
long foo = System.currentTimeMillis(); for(int y = 0; y < 24; y++){ for(int x = 0; x < 42; x++){ position.setX(x + player.getPosition().getX()-21); position.setY(y + player.getPosition().getY()-12); int yOffset = 0; Tile tile = this.game.getTileMap().getTileAt(position); if(tile != null){ image = tile.getImage(); }else{ tile = new Tile(position, "WATER"); image = tile.getImage(); }
Item item = this.game.getTileMap().getItemAt(position); if(item != null){ image = item.getImage(); }
Building building = this.game.getTileMap().getBuildingAt(position); if(building != null){ image = building.getImage(); }
Unit unit = this.game.getTileMap().getUnitAt(position); if(unit != null){ image = unit.getImage(); yOffset = -40; } bbg.drawImage(image, x*20, y*20+yOffset, this); bbg.setColor(Color.WHITE); bbg.fillRect(0, 580, 800, 20); bbg.setColor(Color.BLACK); bbg.drawString(String.valueOf(gui.getFps()), 10, 595); } } long bar = System.currentTimeMillis(); System.out.println(bar - foo); } } |
|
|
|
|
|
27
|
Game Development / Newbie & Debugging Questions / Re: So I think I have a problem with my fps
|
on: 2011-11-30 13:43:08
|
So I tried your loop, hopefully I understood it right. It looks like this now: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public void run() { double startTime, endTime, deltaTime, time; double timeCounter = 0; int framesDrawn = 0;
while (running) { startTime = System.currentTimeMillis();
update(); render(); draw();
endTime = System.currentTimeMillis();
deltaTime = endTime - startTime; time = fps - deltaTime;
timeCounter += deltaTime; if(timeCounter > 1000){ currentFps = framesDrawn / 1000; timeCounter = 0; framesDrawn = 0; }else{ framesDrawn++; }
long sleepTime = (long) (1000 / 60);
try { Thread.sleep(sleepTime); } catch (Exception e) { System.out.println(e); }
} setVisible(false); } |
but it says I have 1.7E-6 fps o.O
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|