/
game.go
73 lines (61 loc) · 1.62 KB
/
game.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
package main
import (
"image/color"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/svera/quarter/physic"
)
type Game struct {
hero *Hero
level *Level
canvas *pixelgl.Canvas
imd *imdraw.IMDraw
paused bool
}
func NewGame(canvas *pixelgl.Canvas, imd *imdraw.IMDraw) *Game {
h, err := NewHero("hero.json")
if err != nil {
panic(err)
}
l, err := NewLevel("levels.json")
if err != nil {
panic(err)
}
g := Game{
hero: h,
level: l,
// Canvas origin of coordinates will be at its center
canvas: canvas,
imd: imd,
}
return &g
}
func (g *Game) Loop(win *pixelgl.Window, dt float64) (string, error) {
g.readInput(win, dt)
if g.paused {
return "game", nil
}
g.imd.Clear()
delta := g.hero.Displacement(dt)
sol := g.hero.boundingShape().Resolve(delta, g.level.Bounds["level1-background"]...)
g.hero.updatePosition(sol, delta)
g.level.Draw(g.canvas, &color.RGBA{0, 0, 255, 16}, g.imd)
g.hero.Draw(g.canvas, &color.RGBA{255, 0, 0, 16}, g.imd, dt)
g.imd.Draw(g.canvas)
g.canvas.Draw(win, pixel.IM.Moved(win.Bounds().Center()).Scaled(win.Bounds().Center(), zoom))
return "game", nil
}
func (g *Game) readInput(win *pixelgl.Window, dt float64) {
if win.JustPressed(pixelgl.KeyUp) {
g.hero.Jump(dt)
} else if win.Pressed(pixelgl.KeyLeft) {
g.hero.Left(dt)
} else if win.Pressed(pixelgl.KeyRight) {
g.hero.Right(dt)
} else if !win.Pressed(pixelgl.KeyLeft) && !win.Pressed(pixelgl.KeyRight) && g.hero.Velocity(physic.AxisX) != 0 {
g.hero.Decelerate(physic.AxisX, dt)
} else if win.JustPressed(pixelgl.KeyP) {
g.paused = !g.paused
}
}