mirror of
https://github.com/Kapendev/parin.git
synced 2025-04-27 05:29:53 +03:00
Fixed tile map and added extra function.
This commit is contained in:
parent
ffb694fb1e
commit
a24a8efe1f
3 changed files with 50 additions and 51 deletions
|
@ -4,19 +4,12 @@ import popka;
|
||||||
// The game variables.
|
// The game variables.
|
||||||
auto atlas = Texture();
|
auto atlas = Texture();
|
||||||
auto map = TileMap();
|
auto map = TileMap();
|
||||||
auto camera = Camera();
|
|
||||||
auto cameraSpeed = Vec2(120);
|
|
||||||
|
|
||||||
bool gameLoop() {
|
bool gameLoop() {
|
||||||
// Move the camera and set up the drawing options of the game.
|
|
||||||
camera.position += wasd * cameraSpeed * Vec2(deltaTime);
|
|
||||||
auto options = DrawOptions();
|
|
||||||
options.scale = Vec2(2);
|
|
||||||
|
|
||||||
// Passing a camera to the tile map drawing function allows for efficient rendering by only drawing the tiles that are currently in view.
|
// Passing a camera to the tile map drawing function allows for efficient rendering by only drawing the tiles that are currently in view.
|
||||||
attachCamera(camera);
|
auto options = DrawOptions();
|
||||||
drawTileMap(atlas, Vec2(), map, camera, options);
|
options.scale = Vec2(2.0f);
|
||||||
detachCamera(camera);
|
drawTileMap(atlas, Vec2(), map, Camera(), options);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,21 +17,13 @@ void gameStart() {
|
||||||
lockResolution(320, 180);
|
lockResolution(320, 180);
|
||||||
setBackgroundColor(toRgb(0x0b0b0b));
|
setBackgroundColor(toRgb(0x0b0b0b));
|
||||||
|
|
||||||
// Loads the `atlas.png` texture from the assets folder.
|
// Loads the `atlas.png` file from the assets folder and parses the tile map data.
|
||||||
auto result = loadTexture("atlas.png");
|
atlas = loadTexture("atlas.png").unwrap();
|
||||||
if (result.isSome) {
|
map.parse("145,0,65\n21,22,23\n37,38,39\n53,54,55", 16, 16);
|
||||||
atlas = result.unwrap();
|
|
||||||
} else {
|
|
||||||
printfln("Can not load texture. Fault: `{}`", result.fault);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse the tile map data and set the tile size.
|
|
||||||
map.parse("145,0,65\n21,22,23\n37,38,39\n53,54,55");
|
|
||||||
map.tileWidth = 16;
|
|
||||||
map.tileHeight = 16;
|
|
||||||
|
|
||||||
updateWindow!gameLoop();
|
updateWindow!gameLoop();
|
||||||
atlas.free();
|
atlas.free();
|
||||||
|
map.free();
|
||||||
}
|
}
|
||||||
|
|
||||||
mixin callGameStart!(gameStart, 640, 360);
|
mixin callGameStart!(gameStart, 640, 360);
|
||||||
|
|
|
@ -1018,6 +1018,11 @@ int fps() {
|
||||||
return ray.GetFPS();
|
return ray.GetFPS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@trusted
|
||||||
|
double elapsedTime() {
|
||||||
|
return ray.GetTime();
|
||||||
|
}
|
||||||
|
|
||||||
@trusted
|
@trusted
|
||||||
float deltaTime() {
|
float deltaTime() {
|
||||||
return ray.GetFrameTime();
|
return ray.GetFrameTime();
|
||||||
|
|
|
@ -38,8 +38,11 @@ struct TileMap {
|
||||||
return tileSize * Vec2(colCount, rowCount);
|
return tileSize * Vec2(colCount, rowCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
Fault parse(IStr csv) {
|
Fault parse(IStr csv, int tileWidth, int tileHeight) {
|
||||||
data.clear();
|
data.clear();
|
||||||
|
this.tileWidth = 0;
|
||||||
|
this.tileHeight = 0;
|
||||||
|
|
||||||
if (csv.length == 0) {
|
if (csv.length == 0) {
|
||||||
return Fault.invalid;
|
return Fault.invalid;
|
||||||
}
|
}
|
||||||
|
@ -65,23 +68,25 @@ struct TileMap {
|
||||||
auto value = line.skipValue(',').toSigned();
|
auto value = line.skipValue(',').toSigned();
|
||||||
if (value.isNone) {
|
if (value.isNone) {
|
||||||
data.clear();
|
data.clear();
|
||||||
|
this.tileWidth = 0;
|
||||||
|
this.tileHeight = 0;
|
||||||
return Fault.invalid;
|
return Fault.invalid;
|
||||||
}
|
}
|
||||||
data[row, col] = cast(short) value.unwrap();
|
data[row, col] = cast(short) value.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.tileWidth = tileWidth;
|
||||||
|
this.tileHeight = tileHeight;
|
||||||
return Fault.none;
|
return Fault.none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Result!TileMap toTileMap(IStr csv, int tileWidth, int tileHeight) {
|
Result!TileMap toTileMap(IStr csv, int tileWidth, int tileHeight) {
|
||||||
auto value = TileMap();
|
auto value = TileMap();
|
||||||
auto fault = value.parse(csv);
|
auto fault = value.parse(csv, tileWidth, tileHeight);
|
||||||
if (fault) {
|
if (fault) {
|
||||||
value.free();
|
value.free();
|
||||||
}
|
}
|
||||||
value.tileWidth = tileWidth;
|
|
||||||
value.tileHeight = tileHeight;
|
|
||||||
return Result!TileMap(value, fault);
|
return Result!TileMap(value, fault);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,46 +98,50 @@ Result!TileMap loadTileMap(IStr path, int tileWidth, int tileHeight) {
|
||||||
return toTileMap(temp.unwrap(), tileWidth, tileHeight);
|
return toTileMap(temp.unwrap(), tileWidth, tileHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTile(Texture texture, Vec2 position, int tileID, Vec2 tileSize, DrawOptions options = DrawOptions()) {
|
void drawTile(Texture texture, Vec2 position, int tileID, int tileWidth, int tileHeight, DrawOptions options = DrawOptions()) {
|
||||||
auto gridWidth = cast(int) (texture.size.x / tileSize.x);
|
auto gridWidth = cast(int) (texture.size.x / tileWidth);
|
||||||
auto gridHeight = cast(int) (texture.size.y / tileSize.y);
|
auto gridHeight = cast(int) (texture.size.y / tileHeight);
|
||||||
if (gridWidth == 0 || gridHeight == 0) {
|
if (gridWidth == 0 || gridHeight == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto row = tileID / gridWidth;
|
auto row = tileID / gridWidth;
|
||||||
auto col = tileID % gridWidth;
|
auto col = tileID % gridWidth;
|
||||||
auto area = Rect(col * tileSize.x, row * tileSize.y, tileSize.x, tileSize.y);
|
auto area = Rect(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
|
||||||
drawTexture(texture, position, area, options);
|
drawTexture(texture, position, area, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTileMap(Texture texture, Vec2 position, TileMap tileMap, Camera camera, DrawOptions options = DrawOptions()) {
|
void drawTileMap(Texture texture, Vec2 position, TileMap map, Camera camera, DrawOptions options = DrawOptions()) {
|
||||||
enum extraTileCount = 1;
|
|
||||||
|
|
||||||
auto cameraArea = Rect(camera.position, resolution).area(camera.hook);
|
auto cameraArea = Rect(camera.position, resolution).area(camera.hook);
|
||||||
auto topLeft = cameraArea.point(Hook.topLeft);
|
auto topLeft = cameraArea.topLeftPoint;
|
||||||
auto bottomRight = cameraArea.point(Hook.bottomRight);
|
auto bottomRight = cameraArea.bottomRightPoint;
|
||||||
auto col1 = 0;
|
auto targetTileWidth = cast(int) (map.tileWidth * options.scale.x);
|
||||||
auto col2 = 0;
|
auto targetTileHeight = cast(int) (map.tileHeight * options.scale.y);
|
||||||
auto row1 = 0;
|
auto targetTileSize = Vec2(targetTileWidth, targetTileHeight);
|
||||||
auto row2 = 0;
|
|
||||||
|
|
||||||
|
auto row1 = 0;
|
||||||
|
auto col1 = 0;
|
||||||
|
auto row2 = 0;
|
||||||
|
auto col2 = 0;
|
||||||
if (camera.isAttached) {
|
if (camera.isAttached) {
|
||||||
col1 = cast(int) floor(clamp((topLeft.x - position.x) / tileMap.tileSize.x - extraTileCount, 0, tileMap.colCount));
|
row1 = cast(int) floor(clamp((topLeft.y - position.y) / targetTileHeight, 0, map.rowCount));
|
||||||
col2 = cast(int) floor(clamp((bottomRight.x - position.x) / tileMap.tileSize.x + extraTileCount, 0, tileMap.colCount));
|
col1 = cast(int) floor(clamp((topLeft.x - position.x) / targetTileWidth, 0, map.colCount));
|
||||||
row1 = cast(int) floor(clamp((topLeft.y - position.y) / tileMap.tileSize.y - extraTileCount, 0, tileMap.rowCount));
|
row2 = cast(int) floor(clamp((bottomRight.y - position.y) / targetTileHeight + 1, 0, map.rowCount));
|
||||||
row2 = cast(int) floor(clamp((bottomRight.y - position.y) / tileMap.tileSize.y + extraTileCount, 0, tileMap.rowCount));
|
col2 = cast(int) floor(clamp((bottomRight.x - position.x) / targetTileWidth + 1, 0, map.colCount));
|
||||||
} else {
|
} else {
|
||||||
col1 = 0;
|
row1 = cast(int) 0;
|
||||||
col2 = cast(int) tileMap.colCount;
|
col1 = cast(int) 0;
|
||||||
row1 = 0;
|
row2 = cast(int) map.rowCount;
|
||||||
row2 = cast(int) tileMap.rowCount;
|
col2 = cast(int) map.colCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (row1 == row2 || col1 == col2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
foreach (row; row1 .. row2) {
|
foreach (row; row1 .. row2) {
|
||||||
foreach (col; col1 .. col2) {
|
foreach (col; col1 .. col2) {
|
||||||
if (tileMap[row, col] == -1) {
|
if (map[row, col] == -1) continue;
|
||||||
continue;
|
drawTile(texture, position + Vec2(col, row) * targetTileSize, map[row, col], map.tileWidth, map.tileHeight, options);
|
||||||
}
|
|
||||||
drawTile(texture, position + Vec2(col, row) * tileMap.tileSize * options.scale, tileMap[row, col], tileMap.tileSize, options);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue