Hello there, I am new to the forum and in game development in general!
I am currently building a simulator for some game, and I am having a few problems already, and I am using the libgdx framework.
I have an isometric map drawn, where each tile is a spot that only one entity can be at.
An entity can either move left, right or just forward in a straight line.
Now this is what it looks at the moment:
As you can see, my ship entity is located at 1,1.
I want to start with adding a function that moves the ship in a straight line, so I need to move this ship to 1,2.
So I have a Vector2 that represents the ship's position, I made a dummy one in the local class where it paints entities, and the map and it's set to isometric coordinates of 1,1:
r = new TextureRegion(texture, location.getX(), location.getY(), location.getWidth(), location.getHeight());
local = new Vector2(getIsometricX(1,1, r), getIsometricY(1,1, r));
"r" is my texture region, which is my sprite image off the spritesheet.
Now I have set a target position to 1,2:
target = new Vector2(getIsometricX(1,2, r), getIsometricY(1,2, r));
So now my question is, how can I make that ship move to the target position? If i add 1 to x and y every tick, it will just move too much to the right.
This is how I paint everything:
@Override
public void render() {
batch.setProjectionMatrix(camera.combined);
batch.begin();
// Render the map
renderSeaBattle();
// Render ships
renderEntities();
batch.end();
}
And the map painting:
private void renderSeaBattle() {
// The map tiles
GameTile[][] tiles = map.getTiles();
for (int i = 0; i < tiles.length; i++) {
for(int j = 0; j < tiles[i].length; j++) {
GameTile tile = tiles[i][j];
Texture texture = tile.getTexture();
int x = (i * GameTile.TILE_WIDTH / 2) - (j * GameTile.TILE_WIDTH / 2) -texture.getWidth() / 2;
int y = (i * GameTile.TILE_HEIGHT / 2) + (j * GameTile.TILE_HEIGHT / 2) -texture.getHeight() / 2;
batch.draw(texture, x, y);
}
}
}
And the entities:
private void renderEntities() {
batch.draw(r, local.x + location.getOffsetx(), local.y + location.getOffsety());
}
location is an instance that contains the offset position for that specific sprite, because not all sprites are the same size, so to center it on the tile, each one has set offsetX and Y.
And this is my coordinate conversion methods:
public int getIsometricX(int x, int y, TextureRegion region) {
return (x * GameTile.TILE_WIDTH / 2) - (y * GameTile.TILE_WIDTH / 2) - (region.getRegionWidth() / 2);
}
public int getIsometricY(int x, int y, TextureRegion region) {
return (x * GameTile.TILE_HEIGHT / 2) + (y * GameTile.TILE_HEIGHT / 2) - (region.getRegionHeight() / 2);
}
After I do the straight line, how can I create left/right movements in curves?
Thanks!
↧