-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities.go
100 lines (86 loc) · 2.59 KB
/
entities.go
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
package main
import (
"github.com/BigJk/ramen/concolor"
"github.com/BigJk/ramen/console"
"github.com/BigJk/ramen/t"
)
// Entity is a generic struct to represent players, enemies, items,etc.
type Entity struct {
dungeon *GameMap
X int
Y int
Char int
Color concolor.Color
name string
blocksMovement bool
}
// Move the entity by a given amount.
func (entity *Entity) Move(dx, dy int) {
entity.X += dx
entity.Y += dy
}
// Spawn makes a copy of Entity and adds it to the GameMap's EntityList if not nil.
func (entity *Entity) Spawn(x, y int, dungeon *GameMap) *Entity {
spawn := *entity
spawn.X = x
spawn.Y = y
if dungeon != nil {
spawn.dungeon = dungeon
dungeon.entities.Add(&spawn)
}
return &spawn
}
// Place moves the Entity to a new location, handles moving across GameMap's, e.g. moving Player between levels
func (entity *Entity) Place(x, y int, dungeon *GameMap) {
entity.X = x
entity.Y = y
if dungeon != nil {
if entity.dungeon != nil {
entity.dungeon.entities.Remove(entity)
}
entity.dungeon = dungeon
dungeon.entities.Add(entity)
}
}
// EntityList is a collection of Entity and provides helper methods
type EntityList struct {
Entities []*Entity
}
// Add adds a new Entity to EntityList
func (list *EntityList) Add(entity *Entity) {
list.Entities = append(list.Entities, entity)
}
func (list *EntityList) Remove(entity *Entity) {
// TODO remove entity from list.Entities
}
// Render loops through all Entity in the EntityList and draws them to the console
func (list *EntityList) Render(dungeon *GameMap, con *console.Console) {
for _, entity := range list.Entities {
if dungeon.fov.IsVisible(entity.X, entity.Y) {
// Only print entities that are in the FOV
con.Transform(entity.X, entity.Y, t.Foreground(entity.Color), t.Char(entity.Char))
}
}
}
// AtLocation returns the first Entity found at location.
func (list *EntityList) AtLocation(x, y int) *Entity {
for _, entity := range list.Entities {
if entity.X == x && entity.Y == y {
return entity
}
}
return nil
}
// NewEntity returns a new Entity pointer
func NewEntity(char rune, color concolor.Color, name string, blocksMovement bool) *Entity {
return &Entity{
Char: int(char),
Color: color,
name: name,
blocksMovement: blocksMovement,
}
}
// Definitions for various Entities to be spawned into the GameMap
var Player = NewEntity('@', concolor.RGB(255, 255, 255), "Player", true)
var Orc = NewEntity('o', concolor.RGB(63, 127, 63), "Orc", true)
var Troll = NewEntity('T', concolor.RGB(0, 127, 0), "Troll", true)