~johanvandegriff/ruegolike

ref: f2dd2fa0ed66a17e5f9ae7829aa1ce22f4e94452 ruegolike/dungeon.go -rw-r--r-- 3.7 KiB
f2dd2fa0Johan Vandegriff tweaked the chars displayed and the movement keys, updated roadmap 1 year, 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main

//Position - the x, y, and z (depth) of a location in the dungeon
type Position struct {
	x int
	y int
	z int
}

//Point - the x, and y location on a level
type Point struct {
	x int
	y int
}

//Dungeon - the layout of the entire dungeon, including all the levels and metadata
type Dungeon struct {
	levels [depth]*Level
}

//Level - one layer of the dungeon, made of many tiles
type Level struct {
	tiles [height][width]*Tile
}

//Tile - one square on a level
type Tile struct {
	char int32 //the character that is displayed
	// isSolid     bool  //are you prevented from walking through it?
	// blocksLight bool  //are you prevented from seeing through it?
	// isRoom      bool  //does it define the walls of a room?
	// isCorner    bool  //is it the corner of a room?
}

//GetTile - retrieve a tile from the dungeon using an x, y, z position
func (d *Dungeon) GetTile(p Position) *Tile {
	return d.levels[p.z].tiles[p.y][p.x]
}

//SetTile - set a tile in the dungeon using an x, y, z position
func (d *Dungeon) SetTile(p Position, t *Tile) {
	d.levels[p.z].tiles[p.y][p.x] = t
}

//GetTile - retrieve a tile from a level using an x, y, z position
func (l *Level) GetTile(p Point) *Tile {
	return l.tiles[p.y][p.x]
}

//SetTile - set a tile in a level using an x, y, z position
func (l *Level) SetTile(p Point, t *Tile) {
	l.tiles[p.y][p.x] = t
}

//GetChar - retrieve a char from a level using an x, y position
func (l *Level) GetChar(p Point) int32 {
	return l.tiles[p.y][p.x].char
}

//IsSolid - returns whether or not a creature can move through the tile
func (t *Tile) IsSolid() bool {
	c := t.char
	return c == ' ' || c == '#' || c == '─' || c == '│' || t.IsCorner()
}

//IsCorner - is the tile the corner of a room border?
func (t *Tile) IsCorner() bool {
	c := t.char
	return c == '├' || c == '┤' || c == '┬' || c == '┴' || c == '┼' || c == '┌' || c == '└' || c == '┐' || c == '┘'
}

//IsRoom - is the tile part of a room border, including doors?
func (t *Tile) IsRoom() bool {
	c := t.char
	return c == '─' || c == '│' || c == '*' || t.IsCorner()
}

//IsRoomFloor - is the tile one that is normally generated inside a room?
func (t *Tile) IsRoomFloor() bool {
	c := t.char
	return c == '·' || c == '>' || c == '<' //|| c == '*'
}

//BlocksLight - returns whether or not a creature can see through the tile
func (t *Tile) BlocksLight() bool {
	return t.IsSolid()
}

//SetChar - set a char in a level using an x, y, z position
func (l *Level) SetChar(p Point, c int32) {
	l.tiles[p.y][p.x] = &Tile{c}
}

//GetChar - retrieve a char from the dungeon using an x, y, z position
func (d *Dungeon) GetChar(p Position) int32 {
	return d.GetTile(p).char
}

//SetChar - set a char in a level using an x, y, z position
func (d *Dungeon) SetChar(p Position, c int32) {
	d.SetTile(p, &Tile{c})
}

//GetLevel - get one level from the dungeon
func (d *Dungeon) GetLevel(z int) *Level {
	return d.levels[z]
}

//NewLevel - create a new empty level
func NewLevel() *Level {
	var tiles [height][width]*Tile
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			tiles[y][x] = &Tile{' '}
		}
	}
	return &Level{tiles}
}

//NewDungeon - create a new dungeon
func NewDungeon() *Dungeon {
	var levels [depth]*Level
	for z := 0; z < depth; z++ {
		levels[z] = NewLevel()
	}
	return &Dungeon{levels}
}

//FindChar - find the location of a particular character in the level
func (l *Level) FindChar(c int32) *Point {
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			if l.GetChar(Point{x, y}) == c {
				return &Point{x, y}
			}
		}
	}
	return nil
}

//DistSquaredTo - return the square of the distance to another point
func (p1 *Point) DistSquaredTo(p2 *Point) int {
	return (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)
}