~sircmpwn/cells

ref: a0c7f20043108cdef497538b2b4b0ce6724d69eb cells/map.go -rw-r--r-- 781 bytes View raw
a0c7f200Drew DeVault Implement map storage and updates 3 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
package main

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

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

func SetCell(coords Coordinates, c Color) {
	// TODO: Lock mutex
	loc := Coordinates{
		X: coords.X / CHUNK_SIZE,
		Y: coords.Y / CHUNK_SIZE,
	}
	if coords.X < 0 {
		loc.X--
	}
	if coords.Y < 0 {
		loc.Y--
	}
	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
}