This repository was archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.go
157 lines (126 loc) · 2.69 KB
/
level.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
package main
import (
"image/color"
"path/filepath"
"sync"
"github.com/bcvery1/tilepix"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
const (
nonCollionOGName = "objs"
)
var (
Level = level{}
tmxMap *tilepix.Map
tilemapPic pixel.Picture
firstUp sync.Once
firstDown sync.Once
firstLeft sync.Once
firstRight sync.Once
// pixels per second
speed = 16. * 8
)
func init() {
var err error
tmxMap, err = tilepix.ReadFile(filepath.Join(binPath, "assets/level1.tmx"))
if err != nil {
panic(err)
}
for _, l := range tmxMap.TileLayers {
l.SetStatic(true)
}
if err := tmxMap.GenerateTileObjectLayer(); err != nil {
panic(err)
}
for _, og := range tmxMap.ObjectGroups {
// only get collision groups
if og.Name == nonCollionOGName {
continue
}
for _, obj := range og.Objects {
r, err := obj.GetRect()
if err != nil {
panic(err)
}
collisionRs = append(collisionRs, r)
}
}
}
type leveler interface {
update(dt float64, win *pixelgl.Window) leveler
draw(target pixel.Target)
}
// level is probably going to be the only level
type level struct {
}
func (l *level) update(dt float64, win *pixelgl.Window) leveler {
if isPaused() {
return currentLvl
}
if win.JustPressed(pixelgl.KeyU) && Nuke == nil {
firstOpen.Do(func() {
addCoins(1)
})
return &UpgradeScreen
}
if lvl := Player.update(dt, win); lvl == &DeathScreen {
return lvl
}
_ = updateEnemies(dt, win)
updateProjectiles(dt)
updateSwipes(dt)
if !movementControls.acquired {
return currentLvl
}
deltaV := pixel.ZV
// up
if win.Pressed(pixelgl.KeyW) {
firstUp.Do(func() {
addCoins(1)
})
deltaV = deltaV.Add(pixel.V(0, speed*dt))
}
// down
if win.Pressed(pixelgl.KeyS) {
firstDown.Do(func() {
addCoins(1)
})
deltaV = deltaV.Add(pixel.V(0, -speed*dt))
}
// Allow vertical movement separate to horizontal
if seeLevel.acquired && deltaV != pixel.ZV && !rectCollides(Player.collisionBox().Moved(deltaV)) {
camPos = camPos.Add(deltaV)
}
deltaV = pixel.ZV
// left
if win.Pressed(pixelgl.KeyA) {
firstLeft.Do(func() {
addCoins(1)
})
deltaV = deltaV.Add(pixel.V(-speed*dt, 0))
}
// right
if win.Pressed(pixelgl.KeyD) {
firstRight.Do(func() {
addCoins(1)
})
deltaV = deltaV.Add(pixel.V(speed*dt, 0))
}
if seeLevel.acquired && deltaV != pixel.ZV && !rectCollides(Player.collisionBox().Moved(deltaV)) {
camPos = camPos.Add(deltaV)
}
return l
}
func (l *level) draw(target pixel.Target) {
if seeLevel.acquired {
if err := tmxMap.DrawAll(target, color.Transparent, pixel.IM); err != nil {
panic(err)
}
drawCoins(target)
}
Player.draw(target)
drawEnemeies(target)
drawProjectiles(target)
drawSwipes(target)
}