-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
158 lines (137 loc) · 3.47 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
package main
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/text"
)
type Game struct {
states []State
world World
// player Player
looker Actor
// chars []*Character
log Log
}
type State int
const (
StateDefault State = 0
StateLooking State = iota
)
func NewGame() *Game {
world := NewWorld(100, 60)
world.Tiles().Point(20, 20).Line(10, 10, 10, 15).Do(func(e *Entity) {
e.ObstructsView = true
e.Walkable = false
e.Image = Images["wall"]
e.Description = "wall"
})
world.CalculateVisibility()
log := NewLog()
log.WriteLine("Welcome to SlayRL")
looker := Actor{
Entity: Entity{
X: 0,
Y: 0,
Image: Images["looker"],
Visibility: 1.0,
},
}
return &Game{
states: []State{StateDefault},
world: world,
looker: looker,
log: log,
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return ScreenSizeX, ScreenSizeY
}
func (g *Game) Update() error {
switch g.TopState() {
case StateDefault:
// Movement
if repeatingKeyPressed(ebiten.KeyUp) {
g.world.player.Move(&g.world, DirectionUp)
g.LogicalUpdate(1 / g.world.player.Speed)
} else if repeatingKeyPressed(ebiten.KeyDown) {
g.world.player.Move(&g.world, DirectionDown)
g.LogicalUpdate(1 / g.world.player.Speed)
} else if repeatingKeyPressed(ebiten.KeyLeft) {
g.world.player.Move(&g.world, DirectionLeft)
g.LogicalUpdate(1 / g.world.player.Speed)
} else if repeatingKeyPressed(ebiten.KeyRight) {
g.world.player.Move(&g.world, DirectionRight)
g.LogicalUpdate(1 / g.world.player.Speed)
}
// Wait
if repeatingKeyPressed(ebiten.KeyPeriod) {
g.LogicalUpdate(1)
}
// Look
if repeatingKeyPressed(ebiten.KeyK) {
g.PushState(StateLooking)
g.looker.X = g.world.player.X
g.looker.Y = g.world.player.Y
}
case StateLooking:
// Move the looker on the map, ignoring collision.
if repeatingKeyPressed(ebiten.KeyUp) {
g.looker.Move(&g.world, DirectionUp, false)
} else if repeatingKeyPressed(ebiten.KeyDown) {
g.looker.Move(&g.world, DirectionDown, false)
} else if repeatingKeyPressed(ebiten.KeyLeft) {
g.looker.Move(&g.world, DirectionLeft, false)
} else if repeatingKeyPressed(ebiten.KeyRight) {
g.looker.Move(&g.world, DirectionRight, false)
}
cx, cy := ebiten.CursorPosition()
x, y := cx/TileSizeX, cy/TileSizeY
if x >= 0 && y >= 0 {
g.looker.X = x
g.looker.Y = y
}
}
// Escape
if g.TopState() != StateDefault && repeatingKeyPressed(ebiten.KeyEscape) {
g.PopState()
}
return nil
}
func (g *Game) LogicalUpdate(d float64) {
for _, c := range g.world.chars {
c.Update(d, &g.world)
}
g.world.CalculateVisibility()
}
func (g *Game) Draw(screen *ebiten.Image) {
g.world.Draw(screen)
for _, c := range g.world.chars {
c.Draw(screen)
}
g.world.player.Draw(screen)
switch g.TopState() {
case StateDefault:
g.log.Draw(screen)
case StateLooking:
g.looker.Draw(screen)
desc := g.world.At(g.looker.X, g.looker.Y).Description
if desc == "" {
desc = "unknown"
}
text.Draw(screen, desc, DefaultFontFace,
g.looker.X*TileSizeX, g.looker.Y*TileSizeY-8,
color.White)
}
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
}
func (g *Game) TopState() State {
return g.states[len(g.states)-1]
}
func (g *Game) PushState(s State) {
g.states = append(g.states, s)
}
func (g *Game) PopState() {
g.states = g.states[:len(g.states)-1]
}