summaryrefslogtreecommitdiff
path: root/src/level.cpp
blob: 4df1d5a1cc744fe397fbc80a4d46623fcb0e714d (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
#include "level.h"

Level::Level(int width, int height)
	: m_width(width), m_height(height)
{
	m_tiles = new uint8_t[width * height];
	memset(m_tiles, 0, width * height);
}

uint8_t Level::get(int x, int y)
{
	if (inBounds(x, y)) {
		return m_tiles[x + y * m_width];
	} else {
		return 0;
	}
}

void Level::set(int x, int y, uint8_t tile)
{
	if (inBounds(x, y)) 
		m_tiles[x + y * m_width] = tile;
}

RailDirection ChooseDirection(Level& level, int x, int y)
{
	if (!level.inBounds(x, y)) return NorthSouth;

	bool n = level.get(x, y - 1) > 0;
	bool e = level.get(x + 1, y) > 0;
	bool s = level.get(x, y + 1) > 0;
	bool w = level.get(x - 1, y) > 0;

	if ((n || s) && !(e || w)) {
		return NorthSouth;
	}
	if ((e || w) && !(n || s)) {
		return EastWest;
	}
	else if (s && e) {
		return SouthEast;
	}
	else if (s && w) {
		return SouthWest;
	}
	else if (n && w) {
		return NorthWest;
	}
	else if (n && e) {
		return NorthEast;
	}

	return NorthSouth;
}