Bind camera and sprite

This commit is contained in:
mollusk 2018-01-19 07:40:48 -07:00
parent 311a15e3e0
commit ea62e2dca4

111
main.go
View File

@ -1,29 +1,25 @@
// main
package main
import (
"fmt"
"image"
_ "image/png"
"math/rand"
"math"
"os"
"time"
_ "image/png"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
//open file from the system
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
//user 'defer' to close the file later on
defer file.Close()
//decode the file to find it's type
img, _, err := image.Decode(file)
if err != nil {
return nil, err
@ -31,16 +27,12 @@ func loadPicture(path string) (pixel.Picture, error) {
return pixel.PictureDataFromImage(img), nil
}
//Pixel's main function (because GO forced them to do it this way)
func run() {
//set the Window parameters
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Bounds: pixel.R(0, 0, 1024, 768),
VSync: true,
}
//create a window and pass the parameters
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
@ -51,6 +43,13 @@ func run() {
panic(err)
}
background, err := loadPicture("background.png")
if err != nil {
panic(err)
}
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
var treesFrames []pixel.Rect
for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 32 {
for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 32 {
@ -58,52 +57,72 @@ func run() {
}
}
//camera
var (
camPos = pixel.ZV
camSpeed = 500.0
trees []*pixel.Sprite
matrices []pixel.Matrix
camPos = pixel.ZV
camSpeed = 500.0
camZoom = 1.0
camZoomSpeed = 1.2
)
var (
frames = 0
second = time.Tick(time.Second)
)
playerX := win.Bounds().Max.X / 2
playerY := win.Bounds().Max.Y / 2
var (
playerXY = pixel.Vec{
playerX,
playerY,
}
)
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))
playerXY = pixel.Vec{
playerX,
playerY,
}
cam := pixel.IM.Scaled(camPos, camZoom).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)
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
bg := pixel.NewSprite(background, background.Bounds())
tree := pixel.NewSprite(spritesheet, treesFrames[0])
//mouse := cam.Unproject(win.MousePosition())
win.Clear(colornames.Forestgreen)
for i, tree := range trees {
tree.Draw(win, matrices[i])
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
tree.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(playerXY))
if win.Pressed(pixelgl.KeyA) {
playerX -= camSpeed * dt
}
//update the window
camPos.X = playerX
camPos.Y = playerY
if win.Pressed(pixelgl.KeyD) {
playerX += camSpeed * dt
}
if win.Pressed(pixelgl.KeyS) {
playerY -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyW) {
playerY += camSpeed * dt
}
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
//batch.Draw(win)
win.Update()
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
}
}