-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.go
More file actions
181 lines (159 loc) · 4.06 KB
/
engine.go
File metadata and controls
181 lines (159 loc) · 4.06 KB
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
package main
import (
"bytes"
"fmt"
"strings"
)
type Engine struct {
canvas Canvas
roomsCanvas Canvas
uiBack Canvas
uiFront Canvas
player Player
world World
logs []string
}
func NewEngine() *Engine {
return &Engine{
canvas: Canvas{},
roomsCanvas: Canvas{},
uiBack: Canvas{},
uiFront: Canvas{},
player: Player{},
logs: []string{},
}
}
var firstInput bool = true
var uiWidth int = 25
// This is the main loop
func (e *Engine) MainLoop(channel <-chan []byte) {
e.Init()
for {
r := e.ReadInput(channel)
if firstInput {
r = []byte(K_EMPTY)
firstInput = false
}
if bytes.Equal(r, []byte(K_ESC)) {
break
}
e.Update(r)
e.Render()
}
}
// This goroutine reads the input
func (e *Engine) InputLoop(buffer chan<- []byte) {
for {
r := ReadRaw()
if len(r) > 0 {
buffer <- r
}
}
}
func (e *Engine) ReadInput(channel <-chan []byte) []byte {
// For now it will be a pure input based tick rate
return <-channel
// TODO: non-blocking input in the future
// select {
// case r := <-channel:
// return r
// default:
// return []byte(K_EMPTY)
// }
}
// Initialize logic
func (e *Engine) Init() {
// Room canvas
e.roomsCanvas.DrawSquare(Vector2d{1, 1}, Vector2d{term.width - uiWidth - 2, term.height})
// We generate the world
e.world = *NewWorld()
e.world.Generate()
e.world.Draw(&e.roomsCanvas)
// Generate the player
e.player = Player{
worldPosition: V.Zero,
roomPosition: V.Identity,
currentRoom: V.Zero,
char: '⍤',
health: 3,
}
startingRoom, _ := e.world.GetRoom(e.player.currentRoom)
e.player.roomPosition = startingRoom.GetSpawnLocation()
// UI Static stuff
e.uiBack.ClearBuffer()
e.uiBack.DrawSquare(Vector2d{terminal.width - uiWidth, 1}, Vector2d{uiWidth, term.height})
// This is the "Ready to play screen"
fmt.Print(CLEAR_SCREEN)
fmt.Print(terminal.pos(1, 1))
fmt.Printf("Press any key...")
}
// Update logic
func (e *Engine) Update(r []byte) {
key := string(r)
e.UpdatePlayer(key)
e.UpdateUi()
}
func (e *Engine) UpdatePlayer(key string) {
// Player movement
direction := V.Zero
if key == K_ARROW_UP || key == KEY_k {
direction = V.Up
}
if key == K_ARROW_DOWN || key == KEY_j {
direction = V.Down
}
if key == K_ARROW_LEFT || key == KEY_h {
direction = V.Left
}
if key == K_ARROW_RIGHT || key == KEY_l {
direction = V.Right
}
// Updating the room position
nextPosition := V.Sum(e.player.roomPosition, direction)
r, _ := e.world.GetRoom(e.player.currentRoom)
if r.IsDoor(nextPosition) {
doorDirection := r.GetDoorDirection(nextPosition)
e.player.currentRoom = V.Sum(e.player.currentRoom, doorDirection)
e.player.roomPosition = r.GetNextRoomEnterPosition(nextPosition)
// Reveal next room
nextRoom, _ := e.world.GetRoom(e.player.currentRoom)
if !nextRoom.visible {
nextRoom.visible = true
e.roomsCanvas.ClearBuffer()
e.world.Draw(&e.roomsCanvas)
}
return
}
if r.IsValidPosition(nextPosition) {
e.player.roomPosition = nextPosition
}
}
func (e *Engine) UpdateUi() {
// Some UI updates
e.uiFront.ClearBuffer()
// UI info
uiStrings := []string{
"Health: " + strings.Repeat("♥", e.player.health),
"------- DEBUG: -------",
fmt.Sprintf("Terminal: %3dx%d", terminal.width, terminal.height),
fmt.Sprintf("UI: %3dx%d", uiWidth, terminal.height),
fmt.Sprintf("World: %3dx%d", terminal.width-uiWidth, terminal.height),
fmt.Sprintf("Pos. Room: %3dx%d", e.player.roomPosition.x, e.player.roomPosition.y),
fmt.Sprintf("Cur. Room: %3dx%d", e.player.currentRoom.x, e.player.currentRoom.y),
}
for i, s := range uiStrings {
e.uiFront.AddString(terminal.pos(terminal.width-23, 2+i))
e.uiFront.AddString(s)
}
}
// Rendering logic
func (e *Engine) Render() {
fmt.Print(CLEAR_SCREEN)
fmt.Print(e.canvas.ToString())
fmt.Print(e.roomsCanvas.ToString())
fmt.Print(e.uiBack.ToString())
fmt.Print(e.uiFront.ToString())
renderPos := V.Sum(e.player.roomPosition, e.world.position)
renderPos = V.Sum(renderPos, e.world.GetRoomWoldPosition(e.player.currentRoom))
e.player.RenderAt(renderPos)
}