-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
355 lines (296 loc) · 9.42 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"fmt"
"image/color"
"log"
"math/rand"
"errors"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/jakecoffman/cp"
)
const (
screenWidth = 350
screenHeight = 700
iterations = 10
gravity = 75
playerFriction = 1.5
playerElasticity = 0
playerRadius = 0.0
objectsFriction = 1.5
objectsMass = 100.0
objectsRadius = 1.0
objectsElasticity = 0.0
objectsMaxSize = 75
objectsMinSize = 25
zeroScore = screenHeight - 25
numBody = 13
)
// For drawing the rigid bodies on screen with Ebiten
type ebitenDrawer struct {
screen *ebiten.Image
upperPoint float64
lost bool
col color.Color
}
func (d *ebitenDrawer) DrawCircle(pos cp.Vector, angle, radius float64, outline, fill cp.FColor, data interface{}) {}
func (d *ebitenDrawer) DrawSegment(a, b cp.Vector, fill cp.FColor, data interface{}) {
}
func (d *ebitenDrawer) DrawFatSegment(a, b cp.Vector, radius float64, outline, fill cp.FColor, data interface{}) {
}
func (d *ebitenDrawer) DrawPolygon(count int, verts []cp.Vector, radius float64, outline, fill cp.FColor, data interface{}) {
col := d.col
if d.lost {
col = color.RGBA{150, 150, 150, 255}
}
lastX := verts[len(verts)-1].X
lastY := verts[len(verts)-1].Y
for _, v := range verts {
ebitenutil.DrawLine(d.screen, lastX, lastY, v.X, v.Y, col)
lastX = v.X
lastY = v.Y
if v.Y < d.upperPoint && v.X >= 0 && v.X <= screenWidth {
d.upperPoint = v.Y
}
}
}
func (d *ebitenDrawer) DrawDot(size float64, pos cp.Vector, fill cp.FColor, data interface{}) {}
func (d *ebitenDrawer) Flags() uint {return 0}
func (d *ebitenDrawer) OutlineColor() cp.FColor {return cp.FColor{R: 0, G: 0, B: 0, A: 0}}
func (d *ebitenDrawer) ShapeColor(shape *cp.Shape, data interface{}) cp.FColor {return cp.FColor{R: 0, G: 0, B: 0, A: 0}}
func (d *ebitenDrawer) ConstraintColor() cp.FColor {return cp.FColor{R: 0, G: 0, B: 0, A: 0}}
func (d *ebitenDrawer) CollisionPointColor() cp.FColor {return cp.FColor{R: 0, G: 0, B: 0, A: 0}}
func (d *ebitenDrawer) Data() interface{} {return 0}
// Function that builds one box
func makeBox(x, y float64) (*cp.Shape, int) {
w := float64(rand.Intn(objectsMaxSize - objectsMinSize + 1) + objectsMinSize)
h := rand.Intn(objectsMaxSize - objectsMinSize + 1) + objectsMinSize
mass := objectsMass * w * float64(h)
body := cp.NewBody(mass, cp.MomentForBox(mass, w, float64(h)))
body.SetPosition(cp.Vector{X: x, Y: y})
shape := cp.NewBox(body, w, float64(h), objectsRadius)
shape.SetElasticity(objectsElasticity)
shape.SetFriction(objectsFriction)
return shape, h
}
// The game structure
type Game struct {
start bool
space *cp.Space
playerBody *cp.Body
mouseX int
readyMouseX bool
upperPoint float64
wasComputed bool
score uint
bestscore uint
isStable bool
lastUpperPoint float64
upperSince int
isLost bool
maxscore bool
oncemaxscore bool
}
// Method for reseting the game
func (g *Game) Reset() {
space := cp.NewSpace()
space.Iterations = iterations
space.SetGravity(cp.Vector{X: 0, Y: gravity})
var shape *cp.Shape
playerBody := cp.NewKinematicBody()
playerBody.SetPosition(cp.Vector{X: float64(screenWidth/2), Y:float64(screenHeight-10)})
space.AddBody(playerBody)
playerShape := cp.NewBox(
playerBody,
float64(screenWidth/2),
10,
playerRadius,
)
playerShape.SetElasticity(playerElasticity)
playerShape.SetFriction(playerFriction)
space.AddShape(playerShape)
borderBodyLeft := cp.NewStaticBody()
borderBodyLeft.SetPosition(cp.Vector{X:0, Y:0})
space.AddBody(borderBodyLeft)
shape = cp.NewSegment(borderBodyLeft, cp.Vector{X:0, Y:0}, cp.Vector{X:0, Y:float64(screenHeight)}, 1)
shape.SetElasticity(objectsElasticity)
shape.SetFriction(objectsFriction)
space.AddShape(shape)
borderBodyRight := cp.NewStaticBody()
borderBodyRight.SetPosition(cp.Vector{X:float64(screenWidth), Y:0})
space.AddBody(borderBodyRight)
shape = cp.NewSegment(borderBodyRight, cp.Vector{X:0, Y:0}, cp.Vector{X:0, Y:float64(screenHeight)}, 1)
shape.SetElasticity(objectsElasticity)
shape.SetFriction(objectsFriction)
space.AddShape(shape)
addedBody := 0
currentHeight := screenHeight - 25
for addedBody < numBody {
currentHeight -= 15
shape, height := makeBox(float64(screenWidth/2), float64(currentHeight))
currentHeight -= height
space.AddBody(shape.Body())
space.AddShape(shape)
addedBody++
}
g.space = space
g.playerBody = playerBody
g.upperPoint = screenHeight
g.score = 0
g.readyMouseX = false
g.wasComputed = false
g.isStable = false
g.lastUpperPoint = screenHeight
g.upperSince = 0
g.isLost = false
g.maxscore = false
}
// Creation of a new game
func NewGame() *Game {
g := &Game{}
g.Reset()
g.start = true
return g
}
// Update method for Ebiten game interface
func (g *Game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
ebiten.SetCursorMode(ebiten.CursorModeVisible)
return errors.New("The end")
}
if g.start {
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
g.start = false
}
} else {
if !g.isLost && !g.isStable {
x, _ := ebiten.CursorPosition()
if g.readyMouseX {
velocity := 60*(x - g.mouseX)
g.playerBody.SetVelocity(float64(velocity), 0)
} else if x != 0 {
g.readyMouseX = true
}
g.mouseX = x
if g.wasComputed {
addToScore := zeroScore - int(g.upperPoint)
if addToScore > 0 {
oldScore := g.score
g.score += uint(addToScore)
if g.score < oldScore {
g.maxscore = true
g.oncemaxscore = true
}
if g.score > g.bestscore {
g.bestscore = g.score
}
} else {
g.isLost = g.score > 0
log.Print(zeroScore, addToScore, g.upperPoint)
}
if g.upperPoint - g.lastUpperPoint >= 5 || g.lastUpperPoint - g.upperPoint >= 5 {
g.lastUpperPoint = g.upperPoint
g.upperSince = 0
} else {
g.upperSince++
}
if g.upperSince >= 180 {
g.isStable = true
}
}
g.upperPoint = screenHeight
g.wasComputed = false
g.space.Step(1.0 / 60)
} else {
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
g.Reset()
}
}
}
return nil
}
// Draw method for Ebiten game interface
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.RGBA{0, 0, 25, 255})
if g.start {
ebitenutil.DebugPrintAt(screen, "Stay Unstable!", screenWidth/2-40, 70)
ebitenutil.DebugPrintAt(screen, "Developped in 12h for Ludum Dare 49.", 70, 100)
ebitenutil.DebugPrintAt(screen, "Keep the tower unstable as long as you can by using your", 10, 150)
ebitenutil.DebugPrintAt(screen, "moose to move the base of it.", 10, 160)
ebitenutil.DebugPrintAt(screen, "The higher the tower and the longer it remains unstable,", 10, 180)
ebitenutil.DebugPrintAt(screen, "the more points you get.", 10, 190)
ebitenutil.DebugPrintAt(screen, "What score will you be able to reach?", 10, 210)
ebitenutil.DebugPrintAt(screen, "At any time, press escape to quit.", 10, screenHeight-70)
ebitenutil.DebugPrintAt(screen, "Press enter to start.", 220, screenHeight-20)
return
}
// Draw the score level
ebitenutil.DrawLine(screen, float64(screenWidth - 15), 0, float64(screenWidth - 15), zeroScore, color.RGBA{0, 0, 255, 255})
for y := 0; y <= zeroScore; y++ {
if (zeroScore - y) % 50 == 0 {
ebitenutil.DrawLine(screen, float64(screenWidth-10), float64(y), float64(screenWidth-20), float64(y), color.RGBA{0, 0, 255, 255})
}
if (zeroScore - y) % 100 == 0 && (zeroScore - y) != 0{
ebitenutil.DebugPrintAt(screen, fmt.Sprint((zeroScore - y)/100 * 100), screenWidth - 25, y - 8)
}
}
// Draw the boxes
drawer := &ebitenDrawer{
screen: screen,
upperPoint: g.upperPoint,
lost: g.isStable || g.isLost,
}
g.space.EachBody(func(body *cp.Body) {
body.EachShape(func(shape *cp.Shape) {
if body == g.playerBody {
drawer.col = color.RGBA{255, 0, 0, 255}
} else {
drawer.col = color.RGBA{255, 255, 255, 255}
}
cp.DrawShape(shape, drawer)
})
})
g.wasComputed = true
// Draw the upper point
g.upperPoint = drawer.upperPoint
if g.upperPoint > zeroScore + 5 {
g.upperPoint = zeroScore + 5
}
ebitenutil.DrawLine(screen, 0, g.upperPoint, screenWidth, g.upperPoint, color.RGBA{255, 0, 0, 255})
// Draw the score
if g.oncemaxscore {
ebitenutil.DebugPrintAt(screen, fmt.Sprint("Record MAXSCORE"), 10, 10)
} else {
ebitenutil.DebugPrintAt(screen, fmt.Sprint("Record ", g.bestscore), 10, 10)
}
if g.maxscore{
ebitenutil.DebugPrintAt(screen, fmt.Sprint("Score MAXSCORE"), 10, 20)
} else {
ebitenutil.DebugPrintAt(screen, fmt.Sprint("Score ", g.score), 10, 20)
}
// Inform about stability
if g.isStable {
ebitenutil.DebugPrintAt(screen, "Stable.", screenWidth/2-20, screenHeight/2)
ebitenutil.DebugPrintAt(screen, "You lose.", screenWidth/2-25, screenHeight/2+10)
ebitenutil.DebugPrintAt(screen, "Press enter to restart.", 110, screenHeight/2+40)
} else if g.isLost {
ebitenutil.DebugPrintAt(screen, "No more objects.", screenWidth/2-45, screenHeight/2)
ebitenutil.DebugPrintAt(screen, "You lose.", screenWidth/2-25, screenHeight/2+10)
ebitenutil.DebugPrintAt(screen, "Press enter to restart.", 110, screenHeight/2+40)
}
//ebitenutil.DebugPrintAt(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()), 200, 0)
}
// Layout method for Ebiten game interface
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
// Run the game with Ebiten
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Stay unstable!")
ebiten.SetCursorMode(ebiten.CursorModeCaptured)
if err := ebiten.RunGame(NewGame()); err != nil {
log.Fatal(err)
}
}