-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
152 lines (118 loc) · 2.97 KB
/
main.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
package main
import (
"fmt"
"github.com/BigJk/ramen/concolor"
"github.com/BigJk/ramen/console"
"github.com/BigJk/ramen/font"
"github.com/BigJk/ramen/t"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"strings"
)
var player = struct {
X int
Y int
}{3, 3}
var levelLayout = `
#####################
# # # #
# # # #
# ###### # #
# # #
## ############# ##
# # # # #
# # # #
# ###### #
# # #
#####################
`
var world [][]byte
// converts levelLayout to world
func initWorld() {
lines := strings.Split(levelLayout, "\n")
for i := range lines {
if len(lines[i]) == 0 {
continue
}
world = append(world, []byte(lines[i]))
}
}
// checks if a tile is solid (tile content is not a space ' ' character)
func isSolid(x int, y int) bool {
return world[y][x] != ' '
}
func main() {
initWorld()
rootView, err := console.New(60, 35, font.DefaultFont, "ramen - roguelike example")
if err != nil {
panic(err)
}
worldView, err := rootView.CreateSubConsole(0, 1, rootView.Width-20, rootView.Height-1)
if err != nil {
panic(err)
}
playerInfoView, err := rootView.CreateSubConsole(worldView.Width, 1, 20, rootView.Height-1)
if err != nil {
panic(err)
}
/*
move player on key press
*/
rootView.SetTickHook(func(timeElapsed float64) error {
if inpututil.IsKeyJustPressed(ebiten.KeyW) && !isSolid(player.X, player.Y-1) {
player.Y -= 1
}
if inpututil.IsKeyJustPressed(ebiten.KeyS) && !isSolid(player.X, player.Y+1) {
player.Y += 1
}
if inpututil.IsKeyJustPressed(ebiten.KeyA) && !isSolid(player.X-1, player.Y) {
player.X -= 1
}
if inpututil.IsKeyJustPressed(ebiten.KeyD) && !isSolid(player.X+1, player.Y) {
player.X += 1
}
return nil
})
/*
render
*/
rootView.SetPreRenderHook(func(screen *ebiten.Image, timeDelta float64) error {
/*
clear console
*/
rootView.ClearAll()
rootView.TransformAll(t.Background(concolor.RGB(50, 50, 50)))
worldView.ClearAll()
worldView.TransformAll(t.Background(concolor.RGB(55, 55, 55)), t.Char(0))
playerInfoView.ClearAll()
/*
draw header
*/
rootView.TransformArea(0, 0, rootView.Width, 1, t.Background(concolor.RGB(80, 80, 80)))
rootView.Print(2, 0, "World View", t.Foreground(concolor.White))
rootView.Print(worldView.Width+2, 0, "Player Info", t.Foreground(concolor.White))
/*
draw world
*/
midX := worldView.Width / 2
midY := worldView.Height / 2
for y := range world {
for x := range world[y] {
if world[y][x] == ' ' {
continue
}
worldView.Transform(midX-player.X+x, midY-player.Y+y, t.CharByte(world[y][x]))
}
}
/*
draw player in the middle
*/
worldView.Transform(midX, midY, t.CharByte('@'), t.Foreground(concolor.Green))
/*
draw player info
*/
playerInfoView.PrintBounded(1, 1, playerInfoView.Width-2, 2, fmt.Sprintf("X=%d Y=%d", player.X, player.Y))
return nil
})
rootView.Start(2)
}