From 311a15e3e0602dfcf137bf47bf2869b4feacc71e Mon Sep 17 00:00:00 2001 From: mollusk Date: Tue, 16 Jan 2018 21:54:57 -0700 Subject: [PATCH] add camera to move with w,a,s,d --- main.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index e8d7076..edb53c3 100644 --- a/main.go +++ b/main.go @@ -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)