About Social Code
summaryrefslogtreecommitdiff
path: root/chunk.rhm
blob: 3ded42d6b6b858b97ef00d5397cb988ab5070930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#lang rhombus/static

export:
    Direction
    TileType
    Tile
    Chunk
    flip_block

enum Direction:
    north
    north_east
    east
    south_east
    south
    south_west
    west
    north_west

enum TileType:
    empty
    wall_north

fun flip_block(block :: maybe(Direction)) :: maybe(Direction):
    match block
    | Direction.north: Direction.south
    | Direction.north_east: Direction.south_west
    | Direction.east: Direction.west
    | Direction.south_east: Direction.north_west
    | Direction.south: Direction.north
    | Direction.south_west: Direction.north_east
    | Direction.west: Direction.east
    | Direction.north_west: Direction.south_east
    | ~else: #false

class Tile(type :: TileType):
    method get_block() :: maybe(Direction):
        match type
        | TileType.empty: #false
        | TileType.wall_north: Direction.north

class Chunk(width :: Int,
            height :: Int,
            offset_x :: Int,
            offset_y :: Int,
            tiles :: Array.now_of(Tile),
            neighbours :: MutableMap.now_of(Direction, Chunk)):
    constructor(width :: Int, height :: Int, offset_x :: Int, offset_y :: Int):
        super(width, height, offset_x, offset_y, Array.make(width * height, Tile(TileType.empty)), MutableMap())

    method add_neighbour(chunk :: Chunk, direction :: Direction):
        neighbours[direction] := chunk

    method get_tile(x :: Int, y :: Int) :: Tile:
        tiles[y * width + x]

    method set_tile(x :: Int, y :: Int, tile :: Tile):
        tiles[y * width + x] := tile