/
game.go
197 lines (168 loc) · 2.99 KB
/
game.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package game
import (
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
type Game struct {
Repo string
debug bool
drawables []Drawable
Screen tcell.Screen
DefaultStyle tcell.Style
Style tcell.Style
MaxWidth int
// Logger *log.Logger
}
func (g *Game) DrawStr(x, y int, str string, style *tcell.Style) {
var st tcell.Style
if style == nil {
st = g.DefaultStyle
} else {
st = *style
}
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
g.Screen.SetContent(x, y, c, comb, st)
x += w
}
}
func (g *Game) AddDrawable(d Drawable) {
g.drawables = append(g.drawables, d)
}
func (g *Game) Destroy(d Drawable) {
newDrawables := []Drawable{}
for _, dd := range g.drawables {
if dd == d {
continue
}
newDrawables = append(newDrawables, dd)
}
g.drawables = newDrawables
}
func (g *Game) Update() {
for _, gobj := range g.drawables {
gobj.Update()
}
}
func (g *Game) Draw() {
for _, gobj := range g.drawables {
gobj.Draw()
}
}
func (g *Game) FindGameObject(fn func(Drawable) bool) Drawable {
for _, gobj := range g.drawables {
if fn(gobj) {
return gobj
}
}
return nil
}
func (g *Game) FilterGameObjects(fn func(Drawable) bool) []Drawable {
out := []Drawable{}
for _, gobj := range g.drawables {
if fn(gobj) {
out = append(out, gobj)
}
}
return out
}
type Drawable interface {
Draw()
Update()
}
type GameObject struct {
X int
Y int
W int
H int
Sprite string
Game *Game
StyleOverride *tcell.Style
}
func (g *GameObject) Update() {}
func (g *GameObject) Transform(x, y int) {
g.X += x
g.Y += y
}
func (g *GameObject) Point() Point {
return Point{g.X, g.Y}
}
func (g *GameObject) Draw() {
var style *tcell.Style
if g.StyleOverride != nil {
style = g.StyleOverride
}
lines := strings.Split(g.Sprite, "\n")
for i, line := range lines {
l := line
w := runewidth.StringWidth(line)
if g.X+w > g.Game.MaxWidth {
space := g.Game.MaxWidth - g.X
comb := []rune{}
for i, r := range line {
if i > space {
break
}
comb = append(comb, r)
}
l = string(comb)
}
g.Game.DrawStr(g.X, g.Y+i, l, style)
}
}
type Point struct {
X int
Y int
}
func (p Point) String() string {
return fmt.Sprintf("<%d, %d>", p.X, p.Y)
}
func (p Point) Equals(o Point) bool {
return p.X == o.X && p.Y == o.Y
}
type Ray struct {
Points []Point
}
func NewRay(a Point, b Point) *Ray {
r := &Ray{
Points: []Point{},
}
if a.Equals(b) {
return r
}
xDir := 1
if a.X > b.X {
xDir = -1
}
yDir := 1
if a.Y > b.Y {
yDir = -1
}
x := a.X
y := a.Y
for x != b.X || y != b.Y {
r.AddPoint(x, y)
if x != b.X {
x += xDir * 1
}
if y != b.Y {
y += yDir * 1
}
}
r.AddPoint(x, y)
return r
}
func (r *Ray) AddPoint(x, y int) {
r.Points = append(r.Points, Point{X: x, Y: y})
}
func (r *Ray) Length() int {
return len(r.Points)
}