~sircmpwn/cells

cells/map.go -rw-r--r-- 867 bytes View raw
99dbe4f3Drew DeVault Refactor chunk coords into separate func 2 months ago
                                                                                
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
package main

var Map map[Coordinates]*[CHUNK_SIZE*CHUNK_SIZE]Color

func init() {
	Map = make(map[Coordinates]*[CHUNK_SIZE*CHUNK_SIZE]Color)
}

func ChunkAt(coords Coordinates) Coordinates {
	loc := Coordinates{
		X: coords.X / CHUNK_SIZE,
		Y: coords.Y / CHUNK_SIZE,
	}
	if coords.X < 0 {
		loc.X--
	}
	if coords.Y < 0 {
		loc.Y--
	}
	return loc
}

func SetCell(coords Coordinates, c Color) {
	// TODO: Lock mutex
	loc := ChunkAt(coords)
	chunk, ok := Map[loc]
	if !ok {
		var blank [CHUNK_SIZE*CHUNK_SIZE]Color
		for n := 0; n < len(blank); n += 1 {
			blank[n] = Color{255, 255, 255};
		}
		chunk = &blank
		Map[loc] = chunk
	}
	local := Coordinates{
		X: coords.X % CHUNK_SIZE,
		Y: coords.Y % CHUNK_SIZE,
	}
	if local.X < 0 {
		local.X = CHUNK_SIZE + local.X
	}
	if local.Y < 0 {
		local.Y = CHUNK_SIZE + local.Y
	}
	chunk[local.Y * CHUNK_SIZE + local.X] = c
}