mirror of
https://github.com/Kapendev/parin.git
synced 2025-04-26 13:09:56 +03:00
Fixed the map and ui modules and worked on examples.
This commit is contained in:
parent
25acd69a84
commit
ef058e59e1
15 changed files with 505 additions and 501 deletions
|
@ -7,44 +7,41 @@ auto map = TileMap();
|
|||
auto camera = Camera(0, 0, true);
|
||||
auto tile = Tile(16, 16, 145);
|
||||
auto tileSpeed = 120;
|
||||
auto tileLookDirection = -1;
|
||||
auto tileFlip = Flip.none;
|
||||
|
||||
void ready() {
|
||||
lockResolution(320, 180);
|
||||
setBackgroundColor(toRgb(0x0b0b0b));
|
||||
// Load the `atlas.png` file from the assets folder.
|
||||
atlas = loadTexture("atlas.png");
|
||||
// Parse the tile map CSV file.
|
||||
// Parse a CSV string representing a tile map, where each tile is 16x16 pixels in size.
|
||||
map.parse("-1,-1,-1\n21,22,23\n37,38,39\n53,54,55", 16, 16);
|
||||
}
|
||||
|
||||
bool update(float dt) {
|
||||
// Make the drawing options.
|
||||
// Create the drawing options for the map and tile.
|
||||
auto mapOptions = DrawOptions(Hook.center);
|
||||
mapOptions.scale = Vec2(2);
|
||||
auto tileOptions = mapOptions;
|
||||
tileOptions.flip = tileLookDirection > 0 ? Flip.x : Flip.none;
|
||||
// Move the tile and camera.
|
||||
tileOptions.flip = tileFlip;
|
||||
if (wasd.x > 0) tileFlip = Flip.x;
|
||||
else if (wasd.x < 0) tileFlip = Flip.none;
|
||||
|
||||
// Move the tile and the camera.
|
||||
tile.position += wasd * Vec2(tileSpeed * dt);
|
||||
camera.position = tile.position;
|
||||
if (wasd.x != 0) tileLookDirection = cast(int) wasd.normalize.round.x;
|
||||
// Check for collisions.
|
||||
auto collisionArea = Rect();
|
||||
foreach (gridPosition; map.gridPositions(camera.area, mapOptions)) {
|
||||
if (map[gridPosition] == -1) continue;
|
||||
auto gridTileArea = Rect(map.worldPosition(gridPosition, mapOptions), Vec2(16) * mapOptions.scale);
|
||||
while (gridTileArea.hasIntersection(Rect(tile.position, tile.size * mapOptions.scale).area(tileOptions.hook))) {
|
||||
// Check for collisions between the tile and the map and resolve the collision.
|
||||
foreach (position; map.gridPositions(camera.area, mapOptions)) {
|
||||
if (map[position] < 0) continue;
|
||||
auto area = Rect(map.worldPosition(position, mapOptions), map.tileSize * mapOptions.scale);
|
||||
while (area.hasIntersection(Rect(tile.position, tile.size * mapOptions.scale).area(mapOptions.hook))) {
|
||||
tile.position -= wasd * Vec2(dt);
|
||||
camera.position = tile.position;
|
||||
collisionArea = gridTileArea;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the game.
|
||||
// Draw the tile and the map.
|
||||
camera.attach();
|
||||
drawTileMap(atlas, map, camera, mapOptions);
|
||||
drawTile(atlas, tile, tileOptions);
|
||||
drawRect(collisionArea, yellow.alpha(120));
|
||||
drawTileMap(atlas, map, camera, mapOptions);
|
||||
camera.detach();
|
||||
drawDebugText("Move with arrow keys.", Vec2(8));
|
||||
return false;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/// This example shows how to use the scene manager of Parin.
|
||||
|
||||
import parin;
|
||||
import parin.scene;
|
||||
|
||||
auto sceneManager = SceneManager();
|
||||
|
||||
|
|
|
@ -11,36 +11,24 @@ auto walkAnimation = SpriteAnimation(0, 2, 6);
|
|||
|
||||
void ready() {
|
||||
lockResolution(320, 180);
|
||||
setBackgroundColor(toRgb(0x0b0b0b));
|
||||
setIsCursorVisible(false);
|
||||
// Load the `atlas.png` file from the assets folder.
|
||||
atlas = loadTexture("atlas.png");
|
||||
}
|
||||
|
||||
bool update(float dt) {
|
||||
// The sprite should be updated every frame, regardless of whether it is running.
|
||||
sprite.update(dt);
|
||||
// Get some basic info about the mouse.
|
||||
auto mouseDistance = sprite.position.distanceTo(mouse);
|
||||
auto mouseDirection = sprite.position.directionTo(mouse);
|
||||
|
||||
// Move the sprite around in a smooth way.
|
||||
sprite.followPositionWithSlowdown(mouse, 0.2);
|
||||
|
||||
// Play the right animation.
|
||||
// Play the correct animation.
|
||||
auto isWaiting = mouseDistance < 0.2;
|
||||
if (isWaiting) {
|
||||
sprite.play(idleAnimation);
|
||||
} else {
|
||||
sprite.play(walkAnimation);
|
||||
}
|
||||
|
||||
if (isWaiting) sprite.play(idleAnimation);
|
||||
else sprite.play(walkAnimation);
|
||||
// Flip the sprite based on the mouse direction.
|
||||
if (mouseDirection.x > 0) {
|
||||
spriteFlip = Flip.x;
|
||||
} else if (mouseDirection.x < 0) {
|
||||
spriteFlip = Flip.none;
|
||||
}
|
||||
|
||||
if (mouseDirection.x > 0) spriteFlip = Flip.x;
|
||||
else if (mouseDirection.x < 0) spriteFlip = Flip.none;
|
||||
// Check if 1, 2, or 3 is pressed and change the character.
|
||||
foreach (i, digit; digitChars[1 .. 4]) {
|
||||
if (digit.isPressed) {
|
||||
|
@ -49,14 +37,11 @@ bool update(float dt) {
|
|||
}
|
||||
}
|
||||
|
||||
// Set the drawing options for the sprite.
|
||||
// Draw the sprite and some info.
|
||||
auto options = DrawOptions(Hook.center);
|
||||
options.flip = spriteFlip;
|
||||
options.scale = Vec2(2);
|
||||
|
||||
// Draw the sprite, the mouse position and some info.
|
||||
drawSprite(atlas, sprite, options);
|
||||
drawVec2(mouse, 8, isWaiting ? blank : white.alpha(150));
|
||||
drawDebugText("Press 1, 2 or 3 to change the character.", Vec2(8));
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue