add camera to move with w,a,s,d
This commit is contained in:
parent
a0de8fd135
commit
311a15e3e0
30
main.go
30
main.go
@ -6,6 +6,7 @@ import (
|
|||||||
_ "image/png"
|
_ "image/png"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/faiface/pixel"
|
"github.com/faiface/pixel"
|
||||||
"github.com/faiface/pixel/pixelgl"
|
"github.com/faiface/pixel/pixelgl"
|
||||||
@ -57,17 +58,44 @@ func run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//camera
|
||||||
var (
|
var (
|
||||||
|
camPos = pixel.ZV
|
||||||
|
camSpeed = 500.0
|
||||||
trees []*pixel.Sprite
|
trees []*pixel.Sprite
|
||||||
matrices []pixel.Matrix
|
matrices []pixel.Matrix
|
||||||
)
|
)
|
||||||
|
|
||||||
|
last := time.Now()
|
||||||
//loop until window is closed by "X" button
|
//loop until window is closed by "X" button
|
||||||
for !win.Closed() {
|
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) {
|
if win.JustPressed(pixelgl.MouseButtonLeft) {
|
||||||
tree := pixel.NewSprite(spritesheet, treesFrames[rand.Intn(len(treesFrames))])
|
tree := pixel.NewSprite(spritesheet, treesFrames[rand.Intn(len(treesFrames))])
|
||||||
trees = append(trees, tree)
|
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
|
//refresh screen and set background color
|
||||||
win.Clear(colornames.Forestgreen)
|
win.Clear(colornames.Forestgreen)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user