mirror of
https://github.com/Kapendev/parin.git
synced 2025-04-28 22:19:55 +03:00
Sprite works now and changed drawing function.
This commit is contained in:
parent
c4b8465f5f
commit
b4b47d8dea
14 changed files with 163 additions and 83 deletions
|
@ -11,7 +11,7 @@ void ready() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool update(float dt) {
|
bool update(float dt) {
|
||||||
drawDebugText("Hello world!");
|
drawDebugText("Hello world!", Vec2(8.0));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
TOUR.md
14
TOUR.md
|
@ -12,7 +12,7 @@ void ready() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool update(float dt) {
|
bool update(float dt) {
|
||||||
drawDebugText("Hello world!");
|
drawDebugText("Hello world!", Vec2(8.0));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,13 +39,13 @@ Here is a breakdown of how this code works:
|
||||||
|
|
||||||
```d
|
```d
|
||||||
bool update(float dt) {
|
bool update(float dt) {
|
||||||
drawDebugText("Hello world!");
|
drawDebugText("Hello world!", Vec2(8.0));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This function is the main loop of the game.
|
This function is the main loop of the game.
|
||||||
It is called every frame while the game is running and, in this example, draws the message "Hello world!".
|
It is called every frame while the game is running and, in this example, draws the message "Hello world!" at position `Vec2(8.0)`.
|
||||||
The `return false` statement indicates that the game should continue running.
|
The `return false` statement indicates that the game should continue running.
|
||||||
If `true` were returned, the game would stop running.
|
If `true` were returned, the game would stop running.
|
||||||
|
|
||||||
|
@ -83,12 +83,12 @@ void drawVec2(Vec2 point, float size, Color color = white);
|
||||||
void drawCirc(Circ area, Color color = white);
|
void drawCirc(Circ area, Color color = white);
|
||||||
void drawLine(Line area, float size, Color color = white);
|
void drawLine(Line area, float size, Color color = white);
|
||||||
|
|
||||||
void drawTexture(Texture texture, Vec2 position, Rect area, DrawOptions options = DrawOptions());
|
|
||||||
void drawTexture(Texture texture, Vec2 position, DrawOptions options = DrawOptions());
|
void drawTexture(Texture texture, Vec2 position, DrawOptions options = DrawOptions());
|
||||||
|
void drawTextureArea(Texture texture, Rect area, Vec2 position, DrawOptions options = DrawOptions());
|
||||||
|
|
||||||
void drawRune(Font font, Vec2 position, dchar rune, DrawOptions options = DrawOptions());
|
void drawRune(Font font, dchar rune, Vec2 position, DrawOptions options = DrawOptions());
|
||||||
void drawText(Font font, Vec2 position, IStr text, DrawOptions options = DrawOptions());
|
void drawText(Font font, IStr text, Vec2 position, DrawOptions options = DrawOptions());
|
||||||
void drawDebugText(IStr text, Vec2 position = Vec2(8.0f), DrawOptions options = DrawOptions());
|
void drawDebugText(IStr text, Vec2 position, DrawOptions options = DrawOptions());
|
||||||
```
|
```
|
||||||
|
|
||||||
Additional drawing functions can be found in other modules, such as `popka.tilemap`.
|
Additional drawing functions can be found in other modules, such as `popka.tilemap`.
|
||||||
|
|
|
@ -18,12 +18,12 @@ bool update(float dt) {
|
||||||
// Draw the game world.
|
// Draw the game world.
|
||||||
auto cameraArea = Rect(camera.position, resolution).area(camera.hook).subAll(3);
|
auto cameraArea = Rect(camera.position, resolution).area(camera.hook).subAll(3);
|
||||||
camera.attach();
|
camera.attach();
|
||||||
drawDebugText("Move with arrow keys.");
|
drawDebugText("Move with arrow keys.", Vec2(8.0));
|
||||||
drawRect(cameraArea, Color(50, 50, 40, 130));
|
drawRect(cameraArea, Color(50, 50, 40, 130));
|
||||||
camera.detach();
|
camera.detach();
|
||||||
|
|
||||||
// Draw the game UI.
|
// Draw the game UI.
|
||||||
drawDebugText("I am UI!");
|
drawDebugText("I am UI!", Vec2(8.0));
|
||||||
drawDebugText("+", resolution * Vec2(0.5));
|
drawDebugText("+", resolution * Vec2(0.5));
|
||||||
drawDebugText("+", resolution * Vec2(0.5) + (cameraTarget - camera.position));
|
drawDebugText("+", resolution * Vec2(0.5) + (cameraTarget - camera.position));
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -48,9 +48,9 @@ bool update(float dt) {
|
||||||
}
|
}
|
||||||
drawRect(player);
|
drawRect(player);
|
||||||
if (coins.length == 0) {
|
if (coins.length == 0) {
|
||||||
drawDebugText("You collected all the coins!");
|
drawDebugText("You collected all the coins!", Vec2(8.0));
|
||||||
} else {
|
} else {
|
||||||
drawDebugText("Coins: {}/{}\nMove with arrow keys.".format(maxCoinCount - coins.length, maxCoinCount));
|
drawDebugText("Coins: {}/{}\nMove with arrow keys.".format(maxCoinCount - coins.length, maxCoinCount), Vec2(8.0));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,9 +63,9 @@ bool update(float dt) {
|
||||||
drawDebugText(" | {}".format(choice), choicePosition);
|
drawDebugText(" | {}".format(choice), choicePosition);
|
||||||
}
|
}
|
||||||
} else if (dialogue.canUpdate) {
|
} else if (dialogue.canUpdate) {
|
||||||
drawDebugText("{}: {}".format(dialogue.actor, dialogue.text));
|
drawDebugText("{}: {}".format(dialogue.actor, dialogue.text), Vec2(8.0));
|
||||||
} else {
|
} else {
|
||||||
drawDebugText("The dialogue has ended.");
|
drawDebugText("The dialogue has ended.", Vec2(8.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the game info.
|
// Draw the game info.
|
||||||
|
|
|
@ -3,13 +3,9 @@ import popka;
|
||||||
|
|
||||||
// The game variables.
|
// The game variables.
|
||||||
auto atlas = TextureId();
|
auto atlas = TextureId();
|
||||||
auto frame = 0.0;
|
auto sprite = Sprite(16, 16, 0, 128, 2, 8);
|
||||||
auto frameCount = 2;
|
auto spritePosition = Vec2();
|
||||||
auto frameSpeed = 8;
|
auto spriteSlowdown = 0.2;
|
||||||
auto framePosition = Vec2();
|
|
||||||
auto frameSize = Vec2(16);
|
|
||||||
auto frameDirection = 1;
|
|
||||||
auto frameSlowdown = 0.2;
|
|
||||||
|
|
||||||
void ready() {
|
void ready() {
|
||||||
lockResolution(320, 180);
|
lockResolution(320, 180);
|
||||||
|
@ -21,29 +17,25 @@ void ready() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool update(float dt) {
|
bool update(float dt) {
|
||||||
// Move the frame around in a smooth way and update the current frame.
|
// Move the sprite around in a smooth way.
|
||||||
framePosition = framePosition.moveToWithSlowdown(mouseScreenPosition, Vec2(dt), frameSlowdown);
|
spritePosition = spritePosition.moveToWithSlowdown(mouseScreenPosition, Vec2(dt), spriteSlowdown);
|
||||||
frame = wrap(frame + dt * frameSpeed, 0, frameCount);
|
|
||||||
|
|
||||||
// Check the mouse move direction and make the sprite look at that direction.
|
// Update the frame of the sprite.
|
||||||
auto mouseDirection = framePosition.directionTo(mouseScreenPosition);
|
auto isWaiting = spritePosition.distanceTo(mouseScreenPosition) < 0.2;
|
||||||
if (framePosition.distanceTo(mouseScreenPosition) < 0.2) {
|
if (isWaiting) {
|
||||||
frame = 0;
|
sprite.reset();
|
||||||
} else if (mouseDirection.x < 0) {
|
} else {
|
||||||
frameDirection = -1;
|
sprite.update(dt);
|
||||||
} else if (mouseDirection.x > 0) {
|
|
||||||
frameDirection = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The drawing options can change the way something is drawn.
|
// Set the drawing options for the sprite.
|
||||||
auto options = DrawOptions();
|
auto options = DrawOptions();
|
||||||
options.hook = Hook.center;
|
|
||||||
options.scale = Vec2(2);
|
options.scale = Vec2(2);
|
||||||
options.flip = frameDirection == 1 ? Flip.x : Flip.none;
|
options.hook = Hook.center;
|
||||||
|
options.flip = (spritePosition.directionTo(mouseScreenPosition).x > 0) ? Flip.x : Flip.none;
|
||||||
// Draw the frame and the mouse position.
|
// Draw the sprite and the mouse position.
|
||||||
drawTexture(atlas, framePosition, Rect(frameSize.x * floor(frame), 128, frameSize), options);
|
drawSprite(atlas, sprite, spritePosition, options);
|
||||||
drawVec2(mouseScreenPosition, 8, frame == 0 ? blank : white.alpha(150));
|
drawVec2(mouseScreenPosition, 8, isWaiting ? blank : white.alpha(130));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ void ready() {
|
||||||
// The update function. This is called every frame while the game is running.
|
// The update function. This is called every frame while the game is running.
|
||||||
// If true is returned, then the game will stop running.
|
// If true is returned, then the game will stop running.
|
||||||
bool update(float dt) {
|
bool update(float dt) {
|
||||||
drawDebugText("Hello world!");
|
drawDebugText("Hello world!", Vec2(8.0));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,21 +3,23 @@ import popka;
|
||||||
|
|
||||||
// The game variables.
|
// The game variables.
|
||||||
auto atlas = TextureId();
|
auto atlas = TextureId();
|
||||||
auto map = TileMap();
|
auto tileMap = TileMap();
|
||||||
|
|
||||||
void ready() {
|
void ready() {
|
||||||
lockResolution(320, 180);
|
lockResolution(320, 180);
|
||||||
setBackgroundColor(toRgb(0x0b0b0b));
|
setBackgroundColor(toRgb(0x0b0b0b));
|
||||||
// Load the `atlas.png` file from the assets folder.
|
// Load the `atlas.png` file from the assets folder.
|
||||||
atlas = loadTexture("atlas.png").unwrap();
|
atlas = loadTexture("atlas.png").unwrap();
|
||||||
// Parse the map CSV file.
|
// Parse the tile map CSV file.
|
||||||
map.parse("145,0,65\n21,22,23\n37,38,39\n53,54,55", 16, 16);
|
tileMap.parse("145,0,65\n21,22,23\n37,38,39\n53,54,55", 16, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool update(float dt) {
|
bool update(float dt) {
|
||||||
|
// Set the drawing options for the tile map.
|
||||||
auto options = DrawOptions();
|
auto options = DrawOptions();
|
||||||
options.scale = Vec2(2.0f);
|
options.scale = Vec2(2.0f);
|
||||||
drawTileMap(atlas, Vec2(), map, Camera(), options);
|
// Draw the tile map.
|
||||||
|
drawTileMap(atlas, tileMap, Vec2(), Camera(), options);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ void ready() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool update(float dt) {
|
bool update(float dt) {
|
||||||
drawDebugText("Hello world!");
|
drawDebugText("Hello world!", Vec2(8.0));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
// TODO: Test the resources code and the tag thing.
|
// TODO: Test the resources code and the tag thing.
|
||||||
// TODO: Make a sprite struct. Something that should help with animation maybe.
|
|
||||||
|
|
||||||
/// The `engine` module functions as a lightweight 2D game engine.
|
/// The `engine` module functions as a lightweight 2D game engine.
|
||||||
module popka.engine;
|
module popka.engine;
|
||||||
|
@ -1600,7 +1599,7 @@ void drawLine(Line area, float size, Color color = white) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@trusted
|
@trusted
|
||||||
void drawTexture(Texture texture, Vec2 position, Rect area, DrawOptions options = DrawOptions()) {
|
void drawTextureArea(Texture texture, Rect area, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
if (texture.isEmpty) {
|
if (texture.isEmpty) {
|
||||||
return;
|
return;
|
||||||
} else if (area.size.x <= 0.0f || area.size.y <= 0.0f) {
|
} else if (area.size.x <= 0.0f || area.size.y <= 0.0f) {
|
||||||
|
@ -1645,12 +1644,12 @@ void drawTexture(Texture texture, Vec2 position, Rect area, DrawOptions options
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTexture(TextureId texture, Vec2 position, Rect area, DrawOptions options = DrawOptions()) {
|
void drawTextureArea(TextureId texture, Rect area, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
drawTexture(texture.getOr(), position, area, options);
|
drawTextureArea(texture.getOr(), area, position, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTexture(Texture texture, Vec2 position, DrawOptions options = DrawOptions()) {
|
void drawTexture(Texture texture, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
drawTexture(texture, position, Rect(texture.size), options);
|
drawTextureArea(texture, Rect(texture.size), position, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTexture(TextureId texture, Vec2 position, DrawOptions options = DrawOptions()) {
|
void drawTexture(TextureId texture, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
|
@ -1658,7 +1657,7 @@ void drawTexture(TextureId texture, Vec2 position, DrawOptions options = DrawOpt
|
||||||
}
|
}
|
||||||
|
|
||||||
@trusted
|
@trusted
|
||||||
void drawRune(Font font, Vec2 position, dchar rune, DrawOptions options = DrawOptions()) {
|
void drawRune(Font font, dchar rune, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
if (font.isEmpty) {
|
if (font.isEmpty) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1682,12 +1681,12 @@ void drawRune(Font font, Vec2 position, dchar rune, DrawOptions options = DrawOp
|
||||||
rl.rlPopMatrix();
|
rl.rlPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawRune(FontId font, Vec2 position, dchar rune, DrawOptions options = DrawOptions()) {
|
void drawRune(FontId font, dchar rune, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
drawRune(font.getOr(), position, rune, options);
|
drawRune(font.getOr(), rune, position, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@trusted
|
@trusted
|
||||||
void drawText(Font font, Vec2 position, IStr text, DrawOptions options = DrawOptions()) {
|
void drawText(Font font, IStr text, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
if (font.isEmpty || text.length == 0) {
|
if (font.isEmpty || text.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1722,7 +1721,7 @@ void drawText(Font font, Vec2 position, IStr text, DrawOptions options = DrawOpt
|
||||||
if (codepoint != ' ' && codepoint != '\t') {
|
if (codepoint != ' ' && codepoint != '\t') {
|
||||||
auto runeOptions = DrawOptions();
|
auto runeOptions = DrawOptions();
|
||||||
runeOptions.color = options.color;
|
runeOptions.color = options.color;
|
||||||
drawRune(font, Vec2(textOffsetX, textOffsetY), codepoint, runeOptions);
|
drawRune(font, codepoint, Vec2(textOffsetX, textOffsetY), runeOptions);
|
||||||
}
|
}
|
||||||
if (font.data.glyphs[index].advanceX == 0) {
|
if (font.data.glyphs[index].advanceX == 0) {
|
||||||
textOffsetX += font.data.recs[index].width + font.runeSpacing;
|
textOffsetX += font.data.recs[index].width + font.runeSpacing;
|
||||||
|
@ -1736,12 +1735,12 @@ void drawText(Font font, Vec2 position, IStr text, DrawOptions options = DrawOpt
|
||||||
rl.rlPopMatrix();
|
rl.rlPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawText(FontId font, Vec2 position, IStr text, DrawOptions options = DrawOptions()) {
|
void drawText(FontId font, IStr text, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
drawText(font.getOr(), position, text, options);
|
drawText(font.getOr(), text, position, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawDebugText(IStr text, Vec2 position = Vec2(8.0f), DrawOptions options = DrawOptions()) {
|
void drawDebugText(IStr text, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
drawText(engineFont, position, text, options);
|
drawText(engineFont, text, position, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
mixin template runGame(alias readyFunc, alias updateFunc, alias finishFunc, int width = 960, int height = 540, IStr title = "Popka") {
|
mixin template runGame(alias readyFunc, alias updateFunc, alias finishFunc, int width = 960, int height = 540, IStr title = "Popka") {
|
||||||
|
|
|
@ -11,5 +11,6 @@ module popka;
|
||||||
public import joka;
|
public import joka;
|
||||||
public import popka.dialogue;
|
public import popka.dialogue;
|
||||||
public import popka.engine;
|
public import popka.engine;
|
||||||
|
public import popka.sprite;
|
||||||
public import popka.tilemap;
|
public import popka.tilemap;
|
||||||
public import popka.timer;
|
public import popka.timer;
|
||||||
|
|
69
source/popka/sprite.d
Normal file
69
source/popka/sprite.d
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
// ---
|
||||||
|
// Copyright 2024 Alexandros F. G. Kapretsos
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
// Email: alexandroskapretsos@gmail.com
|
||||||
|
// Project: https://github.com/Kapendev/popka
|
||||||
|
// Version: v0.0.18
|
||||||
|
// ---
|
||||||
|
|
||||||
|
/// The `sprite` module provides a simple and extensible sprite.
|
||||||
|
module popka.sprite;
|
||||||
|
|
||||||
|
import popka.engine;
|
||||||
|
|
||||||
|
@safe @nogc nothrow:
|
||||||
|
|
||||||
|
// TODO: Think about gaps in an atlas texture.
|
||||||
|
|
||||||
|
struct Sprite {
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
int atlasLeft;
|
||||||
|
int atlasTop;
|
||||||
|
int frameCount = 1;
|
||||||
|
float frameSpeed = 1.0f;
|
||||||
|
float frameProgress = 0.0f;
|
||||||
|
|
||||||
|
@safe @nogc nothrow:
|
||||||
|
|
||||||
|
this(int width, int height, int atlasLeft, int atlasTop, int frameCount = 1, float frameSpeed = 1.0f) {
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.atlasLeft = atlasLeft;
|
||||||
|
this.atlasTop = atlasTop;
|
||||||
|
this.frameCount = frameCount;
|
||||||
|
this.frameSpeed = frameSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(int width, int height) {
|
||||||
|
this(width, height, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int frame() {
|
||||||
|
return cast(int) frameProgress;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
frameProgress = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update(float dt) {
|
||||||
|
frameProgress = wrap(frameProgress + frameSpeed * dt, 0.0f, frameCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawSprite(Texture texture, Sprite sprite, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
|
auto gridWidth = max(texture.width - sprite.atlasLeft, 0) / sprite.width;
|
||||||
|
auto gridHeight = max(texture.height - sprite.atlasTop, 0) / sprite.height;
|
||||||
|
if (gridWidth == 0 || gridHeight == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto row = sprite.frame / gridWidth;
|
||||||
|
auto col = sprite.frame % gridWidth;
|
||||||
|
auto area = Rect(sprite.atlasLeft + col * sprite.width, sprite.atlasTop + row * sprite.height, sprite.width, sprite.height);
|
||||||
|
drawTextureArea(texture, area, position, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawSprite(TextureId texture, Sprite sprite, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
|
drawSprite(texture.getOr(), sprite, position, options);
|
||||||
|
}
|
|
@ -18,11 +18,26 @@ public import joka.types;
|
||||||
|
|
||||||
@safe @nogc nothrow:
|
@safe @nogc nothrow:
|
||||||
|
|
||||||
|
// TODO: Think about gaps in an atlas texture.
|
||||||
|
|
||||||
|
struct Tile {
|
||||||
|
int id;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
|
||||||
|
@safe @nogc nothrow:
|
||||||
|
|
||||||
|
this(int id, int width, int height) {
|
||||||
|
this.id = id;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct TileMap {
|
struct TileMap {
|
||||||
Grid!short data;
|
Grid!short data;
|
||||||
int tileWidth;
|
int tileWidth;
|
||||||
int tileHeight;
|
int tileHeight;
|
||||||
|
|
||||||
alias data this;
|
alias data this;
|
||||||
|
|
||||||
@safe @nogc nothrow:
|
@safe @nogc nothrow:
|
||||||
|
@ -110,28 +125,28 @@ Result!TileMap loadRawTileMap(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, int tileWidth, int tileHeight, DrawOptions options = DrawOptions()) {
|
void drawTile(Texture texture, Tile tile, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
auto gridWidth = cast(int) (texture.width / tileWidth);
|
auto gridWidth = texture.width / tile.width;
|
||||||
auto gridHeight = cast(int) (texture.height / tileHeight);
|
auto gridHeight = texture.height / tile.height;
|
||||||
if (gridWidth == 0 || gridHeight == 0) {
|
if (gridWidth == 0 || gridHeight == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto row = tileID / gridWidth;
|
auto row = tile.id / gridWidth;
|
||||||
auto col = tileID % gridWidth;
|
auto col = tile.id % gridWidth;
|
||||||
auto area = Rect(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
|
auto area = Rect(col * tile.width, row * tile.height, tile.width, tile.height);
|
||||||
drawTexture(texture, position, area, options);
|
drawTextureArea(texture, area, position, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTile(TextureId texture, Vec2 position, int tileID, int tileWidth, int tileHeight, DrawOptions options = DrawOptions()) {
|
void drawTile(TextureId texture, Tile tile, Vec2 position, DrawOptions options = DrawOptions()) {
|
||||||
drawTile(texture.getOr(), position, tileID, tileWidth, tileHeight, options);
|
drawTile(texture.getOr(), tile, position, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTileMap(Texture texture, Vec2 position, TileMap map, Camera camera, DrawOptions options = DrawOptions()) {
|
void drawTileMap(Texture texture, TileMap tileMap, Vec2 position, Camera camera, DrawOptions options = DrawOptions()) {
|
||||||
auto cameraArea = Rect(camera.position, resolution).area(camera.hook);
|
auto cameraArea = Rect(camera.position, resolution).area(camera.hook);
|
||||||
auto topLeft = cameraArea.topLeftPoint;
|
auto topLeft = cameraArea.topLeftPoint;
|
||||||
auto bottomRight = cameraArea.bottomRightPoint;
|
auto bottomRight = cameraArea.bottomRightPoint;
|
||||||
auto targetTileWidth = cast(int) (map.tileWidth * options.scale.x);
|
auto targetTileWidth = cast(int) (tileMap.tileWidth * options.scale.x);
|
||||||
auto targetTileHeight = cast(int) (map.tileHeight * options.scale.y);
|
auto targetTileHeight = cast(int) (tileMap.tileHeight * options.scale.y);
|
||||||
auto targetTileSize = Vec2(targetTileWidth, targetTileHeight);
|
auto targetTileSize = Vec2(targetTileWidth, targetTileHeight);
|
||||||
|
|
||||||
auto row1 = 0;
|
auto row1 = 0;
|
||||||
|
@ -139,15 +154,15 @@ void drawTileMap(Texture texture, Vec2 position, TileMap map, Camera camera, Dra
|
||||||
auto row2 = 0;
|
auto row2 = 0;
|
||||||
auto col2 = 0;
|
auto col2 = 0;
|
||||||
if (camera.isAttached) {
|
if (camera.isAttached) {
|
||||||
row1 = cast(int) floor(clamp((topLeft.y - position.y) / targetTileHeight, 0, map.rowCount));
|
row1 = cast(int) floor(clamp((topLeft.y - position.y) / targetTileHeight, 0, tileMap.rowCount));
|
||||||
col1 = cast(int) floor(clamp((topLeft.x - position.x) / targetTileWidth, 0, map.colCount));
|
col1 = cast(int) floor(clamp((topLeft.x - position.x) / targetTileWidth, 0, tileMap.colCount));
|
||||||
row2 = cast(int) floor(clamp((bottomRight.y - position.y) / targetTileHeight + 1, 0, map.rowCount));
|
row2 = cast(int) floor(clamp((bottomRight.y - position.y) / targetTileHeight + 1, 0, tileMap.rowCount));
|
||||||
col2 = cast(int) floor(clamp((bottomRight.x - position.x) / targetTileWidth + 1, 0, map.colCount));
|
col2 = cast(int) floor(clamp((bottomRight.x - position.x) / targetTileWidth + 1, 0, tileMap.colCount));
|
||||||
} else {
|
} else {
|
||||||
row1 = cast(int) 0;
|
row1 = cast(int) 0;
|
||||||
col1 = cast(int) 0;
|
col1 = cast(int) 0;
|
||||||
row2 = cast(int) map.rowCount;
|
row2 = cast(int) tileMap.rowCount;
|
||||||
col2 = cast(int) map.colCount;
|
col2 = cast(int) tileMap.colCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row1 == row2 || col1 == col2) {
|
if (row1 == row2 || col1 == col2) {
|
||||||
|
@ -156,12 +171,14 @@ void drawTileMap(Texture texture, Vec2 position, TileMap map, Camera camera, Dra
|
||||||
|
|
||||||
foreach (row; row1 .. row2) {
|
foreach (row; row1 .. row2) {
|
||||||
foreach (col; col1 .. col2) {
|
foreach (col; col1 .. col2) {
|
||||||
if (map[row, col] == -1) continue;
|
if (tileMap[row, col] == -1) continue;
|
||||||
drawTile(texture, position + Vec2(col, row) * targetTileSize, map[row, col], map.tileWidth, map.tileHeight, options);
|
auto tile = Tile(tileMap[row, col], tileMap.tileWidth, tileMap.tileHeight);
|
||||||
|
auto tilePosition = position + Vec2(col, row) * targetTileSize;
|
||||||
|
drawTile(texture, tile, tilePosition, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTileMap(TextureId texture, Vec2 position, TileMap map, Camera camera, DrawOptions options = DrawOptions()) {
|
void drawTileMap(TextureId texture, TileMap tileMap, Vec2 position, Camera camera, DrawOptions options = DrawOptions()) {
|
||||||
drawTileMap(texture.getOr(), position, map, camera, options);
|
drawTileMap(texture.getOr(), tileMap, position, camera, options);
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ struct Timer {
|
||||||
|
|
||||||
void stop() {
|
void stop() {
|
||||||
time = duration;
|
time = duration;
|
||||||
prevTime = duration;
|
prevTime = duration - 0.1f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pause() {
|
void pause() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue