add camera to move with w,a,s,d

This commit is contained in:
mollusk 2018-01-16 21:54:57 -07:00
parent a0de8fd135
commit 311a15e3e0

30
main.go
View File

@ -6,6 +6,7 @@ import (
_ "image/png"
"math/rand"
"os"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
@ -57,17 +58,44 @@ func run() {
}
}
//camera
var (
camPos = pixel.ZV
camSpeed = 500.0
trees []*pixel.Sprite
matrices []pixel.Matrix
)
last := time.Now()
//loop until window is closed by "X" button
for !win.Closed() {
dt := time.Since(last).Seconds()
last = time.Now()
cam := pixel.IM.Moved(win.Bounds().Center().Sub(camPos))
win.SetMatrix(cam)
if win.JustPressed(pixelgl.MouseButtonLeft) {
tree := pixel.NewSprite(spritesheet, treesFrames[rand.Intn(len(treesFrames))])
trees = append(trees, tree)
matrices = append(matrices, pixel.IM.Scaled(pixel.ZV, 4).Moved(win.MousePosition()))
mouse := cam.Unproject(win.MousePosition())
matrices = append(matrices, pixel.IM.Scaled(pixel.ZV, 4).Moved(mouse))
}
if win.Pressed(pixelgl.KeyA) {
camPos.X -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyD) {
camPos.X += camSpeed * dt
}
if win.Pressed(pixelgl.KeyS) {
camPos.Y -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyW) {
camPos.Y += camSpeed * dt
}
//refresh screen and set background color
win.Clear(colornames.Forestgreen)