-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,015 additions
and
317 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
const __asset_cache = {}; | ||
|
||
export function load_image(src) { | ||
const cached_image = __asset_cache[src]; | ||
if (cached_image) return cached_image; | ||
|
||
const image = new Image(); | ||
image.src = src; | ||
__asset_cache[src] = image; | ||
return image; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import gleam/dict | ||
import lib/asset | ||
import lib/sprite | ||
|
||
pub type DemoVariant { | ||
Base | ||
Variant1 | ||
Variant2 | ||
Variant3 | ||
Variant4 | ||
Variant5 | ||
Variant6 | ||
} | ||
|
||
pub fn get_sprite_key(variant: DemoVariant) { | ||
case variant { | ||
Base -> "Base" | ||
Variant1 -> "Variant1" | ||
Variant2 -> "Variant2" | ||
Variant3 -> "Variant3" | ||
Variant4 -> "Variant4" | ||
Variant5 -> "Variant5" | ||
Variant6 -> "Variant6" | ||
} | ||
} | ||
|
||
const sprites = [ | ||
#("Base", sprite.SpriteRegion(0, 0)), #("Variant1", sprite.SpriteRegion(0, 0)), | ||
#("Variant2", sprite.SpriteRegion(0, 1)), | ||
#("Variant3", sprite.SpriteRegion(0, 2)), | ||
#("Variant4", sprite.SpriteRegion(0, 3)), | ||
#("Variant5", sprite.SpriteRegion(0, 4)), | ||
#("Variant6", sprite.SpriteRegion(0, 5)), | ||
] | ||
|
||
pub fn sprite_sheet() -> sprite.SpriteSheet { | ||
sprite.SpriteSheet( | ||
asset: asset.load_image( | ||
"https://pub-e304780d47a742ad9bad4f35844cd6e6.r2.dev/test-tile.png", | ||
), | ||
sprites: dict.from_list(sprites), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import gleam/int | ||
|
||
pub opaque type Coord { | ||
Coord(x: Int, y: Int, width: Int, height: Int) | ||
} | ||
|
||
pub fn new(width: Int, height: Int) -> Coord { | ||
Coord(0, 0, width, height) | ||
} | ||
|
||
pub fn move(from: Coord, x: Int, y: Int) -> Coord { | ||
Coord( | ||
int.clamp(from.x + x, 0, from.width), | ||
int.clamp(from.y + y, 0, from.height), | ||
from.width, | ||
from.height, | ||
) | ||
} | ||
|
||
pub fn next(coord: Coord) -> Coord { | ||
case is_bounded_x(coord), is_bounded_y(coord) { | ||
True, _ -> Coord(..coord, x: coord.x + 1) | ||
_, True -> Coord(..coord, y: coord.y + 1) | ||
_, _ -> coord | ||
} | ||
} | ||
|
||
fn is_bounded_x(coord: Coord) -> Bool { | ||
coord.x < coord.width - 1 | ||
} | ||
|
||
fn is_bounded_y(coord: Coord) -> Bool { | ||
coord.y < coord.height - 1 | ||
} | ||
|
||
pub fn left(coord: Coord) -> Coord { | ||
move(coord, -1, 0) | ||
} | ||
|
||
pub fn right(coord: Coord) -> Coord { | ||
move(coord, 1, 0) | ||
} | ||
|
||
pub fn up(coord: Coord) -> Coord { | ||
move(coord, 0, -1) | ||
} | ||
|
||
pub fn down(coord: Coord) -> Coord { | ||
move(coord, 0, 1) | ||
} | ||
|
||
pub fn x(coord: Coord) { | ||
coord.x | ||
} | ||
|
||
pub fn y(coord: Coord) { | ||
coord.y | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import lib/coord | ||
import lib/sprite | ||
import lib/tile | ||
|
||
pub type Map { | ||
Map( | ||
width: Int, | ||
height: Int, | ||
sprite_sheet: sprite.SpriteSheet, | ||
tiles: List(tile.Tile), | ||
) | ||
} | ||
|
||
pub fn each_tile(map: Map, f: fn(coord.Coord, tile.Tile) -> b) -> Nil { | ||
each_tile_loop(map.tiles, coord.new(map.width, map.height), f) | ||
} | ||
|
||
fn each_tile_loop( | ||
tiles: List(tile.Tile), | ||
coords: coord.Coord, | ||
f: fn(coord.Coord, tile.Tile) -> b, | ||
) -> Nil { | ||
case tiles { | ||
[] -> Nil | ||
[tile, ..rest] -> { | ||
f(coords, tile) | ||
each_tile_loop(rest, coord.next(coords), f) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import lib/asset/demo | ||
import lib/map | ||
import lib/tile | ||
|
||
pub fn new() -> map.Map { | ||
map.Map(width: 8, height: 8, sprite_sheet: demo.sprite_sheet(), tiles: [ | ||
// Row 1 | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant1), | ||
new_tile(demo.Variant6), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant4), | ||
new_tile(demo.Variant5), | ||
new_tile(demo.Variant2), | ||
new_tile(demo.Base), | ||
// Row 2 | ||
new_tile(demo.Variant3), | ||
new_tile(demo.Variant4), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant5), | ||
new_tile(demo.Variant1), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant6), | ||
new_tile(demo.Variant2), | ||
// Row 3 | ||
new_tile(demo.Variant2), | ||
new_tile(demo.Base), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant1), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant3), | ||
new_tile(demo.Base), | ||
// Row 4 | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant4), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant1), | ||
new_tile(demo.Variant3), | ||
new_tile(demo.Base), | ||
new_tile(demo.Base), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant2), | ||
// Row 5 | ||
new_tile(demo.Variant3), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant4), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant5), | ||
new_tile(demo.Base), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant1), | ||
// Row 6 | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant1), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant3), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant2), | ||
new_tile(demo.Variant5), | ||
new_tile(demo.Base), | ||
// Row 7 | ||
new_tile(demo.Variant4), | ||
new_tile(demo.Variant2), | ||
new_tile(demo.Base), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant5), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant6), | ||
new_tile(demo.Base), | ||
// Row 8 | ||
new_tile(demo.Variant6), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant1), | ||
new_tile(demo.Variant4), | ||
new_tile(demo.Base), | ||
new_tile(demo.Base), | ||
new_tile(demo.Base), | ||
new_tile(demo.Variant2), | ||
]) | ||
} | ||
|
||
fn new_tile(variant: demo.DemoVariant) -> tile.Tile { | ||
tile.Tile( | ||
elevation: 0, | ||
terrain: tile.Demo(variant), | ||
passability: tile.Passable, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import gleam/dict | ||
import lib/asset | ||
|
||
pub type SpriteRegion { | ||
SpriteRegion(x: Int, y: Int) | ||
} | ||
|
||
pub type SpriteSheet { | ||
SpriteSheet(asset: asset.Asset, sprites: dict.Dict(String, SpriteRegion)) | ||
} | ||
|
||
pub fn x(sr: SpriteRegion) { | ||
sr.x | ||
} | ||
|
||
pub fn y(sr: SpriteRegion) { | ||
sr.y | ||
} |
Oops, something went wrong.