-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
255 lines (214 loc) · 5.76 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
screenWidth = 1000
screenHeight = 480
fps = 60
)
var (
running = true
bkgColor = rl.NewColor(147, 211, 196, 255)
grassSprite rl.Texture2D
hillSprite rl.Texture2D
fenceSprite rl.Texture2D
houseSprite rl.Texture2D
waterSprite rl.Texture2D
tilledSprite rl.Texture2D
tex rl.Texture2D
playerSprite rl.Texture2D
playerSrc rl.Rectangle
playerDest rl.Rectangle
playerMoving bool
playerDir int
playerUp, playerDown, playerRight, playerLeft bool
playerSpeed float32 = 1.4
playerFrame int
frameCount int
tileDest rl.Rectangle
tileSrc rl.Rectangle
tileMap []int
srcMap []string
mapWidth int
mapHeight int
musicPaused bool
music rl.Music
cam rl.Camera2D
)
func drawScene() {
// rl.DrawTexture(grassSprite, 100, 50, rl.White)
for i := 0; i < len(tileMap); i++ {
if tileMap[i] != 0 {
tileDest.X = tileDest.Width * float32(i%mapWidth)
tileDest.Y = tileDest.Height * float32(i/mapWidth)
switch srcMap[i] {
case "g":
tex = grassSprite
case "l":
tex = hillSprite
case "f":
tex = fenceSprite
case "h":
tex = houseSprite
case "w":
tex = waterSprite
case "t":
tex = tilledSprite
}
// you need to draw grass below a fence or house part first
if srcMap[i] == "f" || srcMap[i] == "h" {
tileSrc.X = 0
tileSrc.Y = 0
rl.DrawTexturePro(grassSprite, tileSrc, tileDest, rl.NewVector2(tileDest.Width, tileDest.Height), 0, rl.White)
}
tileSrc.X = tileSrc.Width * float32((tileMap[i]-1)%int(tex.Width/int32(tileSrc.Width)))
tileSrc.Y = tileSrc.Height * float32((tileMap[i]-1)/int(tex.Width/int32(tileSrc.Width)))
rl.DrawTexturePro(tex, tileSrc, tileDest, rl.NewVector2(tileDest.Width, tileDest.Height), 0, rl.White)
}
}
rl.DrawTexturePro(playerSprite, playerSrc, playerDest, rl.NewVector2(playerDest.Width, playerDest.Height), 0, rl.White)
}
func input() {
if rl.IsKeyDown(rl.KeyW) || rl.IsKeyDown(rl.KeyUp) {
playerMoving = true
playerDir = 1
playerUp = true
}
if rl.IsKeyDown(rl.KeyS) || rl.IsKeyDown(rl.KeyDown) {
playerMoving = true
playerDir = 0
playerDown = true
}
if rl.IsKeyDown(rl.KeyA) || rl.IsKeyDown(rl.KeyLeft) {
playerMoving = true
playerDir = 2
playerLeft = true
}
if rl.IsKeyDown(rl.KeyD) || rl.IsKeyDown(rl.KeyRight) {
playerMoving = true
playerDir = 3
playerRight = true
}
if rl.IsKeyPressed(rl.KeyQ) {
musicPaused = !musicPaused
}
}
func update() {
running = !rl.WindowShouldClose()
playerSrc.X = playerSrc.Width * float32(playerFrame)
if playerMoving {
if playerUp {
playerDest.Y -= playerSpeed
}
if playerDown {
playerDest.Y += playerSpeed
}
if playerLeft {
playerDest.X -= playerSpeed
}
if playerRight {
playerDest.X += playerSpeed
}
if frameCount%8 == 1 {
playerFrame++
}
} else if frameCount%45 == 1 {
playerFrame++
}
frameCount++
if playerFrame > 3 {
playerFrame = 0
}
if !playerMoving && playerFrame > 1 {
playerFrame = 0
}
playerSrc.X = playerSrc.Width * float32(playerFrame)
playerSrc.Y = playerSrc.Height * float32(playerDir)
rl.UpdateMusicStream(music)
if musicPaused {
rl.PauseMusicStream(music)
} else {
rl.ResumeMusicStream(music)
}
cam.Target = rl.NewVector2(float32(playerDest.X-(playerDest.Width/2)), float32(playerDest.Y-(playerDest.Height/2)))
playerMoving = false
playerUp, playerDown, playerRight, playerLeft = false, false, false, false
}
func render() {
rl.BeginDrawing()
rl.ClearBackground(bkgColor)
rl.BeginMode2D(cam)
drawScene()
rl.EndMode2D()
rl.EndDrawing()
}
func loadMap(mapFile string) {
file, err := ioutil.ReadFile(mapFile)
if err != nil {
fmt.Errorf("Map File did not load: %v", err)
os.Exit(1)
}
remNewLines := strings.Replace(string(file), "\n", " ", -1)
sliced := strings.Split(remNewLines, " ")
mapWidth = -1
mapHeight = -1
for i, me := range sliced {
s, _ := strconv.ParseInt(me, 10, 64)
m := int(s)
if mapWidth == -1 {
mapWidth = m
} else if mapHeight == -1 {
mapHeight = m
} else if i < mapHeight*mapWidth+2 {
tileMap = append(tileMap, m)
} else {
srcMap = append(srcMap, sliced[i])
}
}
}
func init() {
rl.InitWindow(screenWidth, screenHeight, "Sproutlings!")
rl.SetExitKey(0)
rl.SetTargetFPS(fps)
grassSprite = rl.LoadTexture("res/SproutLands/Tilesets/Grass.png")
hillSprite = rl.LoadTexture("res/SproutLands/Tilesets/Hills.png")
fenceSprite = rl.LoadTexture("res/SproutLands/Tilesets/Basic Fences.png")
houseSprite = rl.LoadTexture("res/SproutLands/Tilesets/Basic Wooden House.png")
waterSprite = rl.LoadTexture("res/SproutLands/Tilesets/Water.png")
tilledSprite = rl.LoadTexture("res/SproutLands/Tilesets/Tilled Dirt.png")
tileDest = rl.NewRectangle(0, 0, 16, 16)
tileSrc = rl.NewRectangle(0, 0, 16, 16)
playerSprite = rl.LoadTexture("res/SproutLands/characters/Basic Charakter Spritesheet.png")
playerSrc = rl.NewRectangle(0, 0, 48, 48)
playerDest = rl.NewRectangle(200, 200, 60, 60)
rl.InitAudioDevice()
music = rl.LoadMusicStream("res/sproutlings_loopable.mp3")
musicPaused = false
rl.PlayMusicStream(music)
cam = rl.NewCamera2D(rl.NewVector2(float32(screenWidth/2), float32(screenHeight/2)),
rl.NewVector2(float32(playerDest.X-(playerDest.Width/2)), float32(playerDest.Y-(playerDest.Height/2))),
0, 1.5)
cam.Zoom = 3
loadMap("one.map")
}
func quit() {
rl.UnloadTexture(grassSprite)
rl.UnloadTexture(playerSprite)
rl.UnloadMusicStream(music)
rl.CloseAudioDevice()
rl.CloseWindow()
}
func main() {
for running {
input()
update()
render()
}
quit()
}