Animate sprite
This commit is contained in:
76
gameobjects/animate.go
Normal file
76
gameobjects/animate.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package gameobjects
|
||||
|
||||
import (
|
||||
"github.com/faiface/pixel"
|
||||
"github.com/faiface/pixel/pixelgl"
|
||||
)
|
||||
|
||||
type Animate struct {
|
||||
//PlayerVec pixel.Vec
|
||||
Sprite *pixel.Sprite
|
||||
Up []pixel.Rect
|
||||
Down []pixel.Rect
|
||||
Left []pixel.Rect
|
||||
Right []pixel.Rect
|
||||
Idle bool
|
||||
Dir int
|
||||
Frame int
|
||||
Current []pixel.Rect
|
||||
SprSheet pixel.Picture
|
||||
}
|
||||
|
||||
func NewAnimation(spriteSheet pixel.Picture, sprFrames []pixel.Rect) Animate {
|
||||
var animation Animate
|
||||
for i := 0; i < 3; i++ {
|
||||
animation.Up = append(animation.Up, sprFrames[i])
|
||||
}
|
||||
for i := 3; i < 6; i++ {
|
||||
animation.Right = append(animation.Right, sprFrames[i])
|
||||
}
|
||||
for i := 6; i < 9; i++ {
|
||||
animation.Left = append(animation.Left, sprFrames[i])
|
||||
}
|
||||
for i := 9; i < 12; i++ {
|
||||
animation.Down = append(animation.Down, sprFrames[i])
|
||||
}
|
||||
|
||||
animation.Current = animation.Down
|
||||
animation.SprSheet = spriteSheet
|
||||
animation.Sprite = pixel.NewSprite(spriteSheet, animation.Current[0])
|
||||
animation.Frame = 0
|
||||
animation.Idle = false
|
||||
return animation
|
||||
}
|
||||
|
||||
var elapsedTime int64 = 0
|
||||
|
||||
func (a *Animate) Update(dt int64) {
|
||||
var direction int = a.Dir
|
||||
switch direction {
|
||||
case 0:
|
||||
a.Current = a.Up
|
||||
case 1:
|
||||
a.Current = a.Down
|
||||
case 2:
|
||||
a.Current = a.Left
|
||||
case 3:
|
||||
a.Current = a.Right
|
||||
}
|
||||
elapsedTime += dt
|
||||
if elapsedTime >= 1000000000/10 {
|
||||
a.Frame++
|
||||
elapsedTime -= 1000000000 / 10
|
||||
}
|
||||
if a.Frame >= 3 {
|
||||
a.Frame = 0
|
||||
}
|
||||
if a.Idle {
|
||||
a.Frame = 0
|
||||
}
|
||||
a.Sprite.Set(a.SprSheet, a.Current[a.Frame])
|
||||
}
|
||||
|
||||
func (a *Animate) Render(win *pixelgl.Window, pVec pixel.Vec) {
|
||||
a.Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(pVec))
|
||||
|
||||
}
|
||||
@@ -7,19 +7,18 @@ import (
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
X, Y float64
|
||||
PlayerVec pixel.Vec
|
||||
sprFrame int
|
||||
spriteSheet pixel.Picture
|
||||
sprFrames []pixel.Rect
|
||||
sprite *pixel.Sprite
|
||||
speed float64
|
||||
dir int
|
||||
animation Animate
|
||||
}
|
||||
|
||||
func NewPlayer(win *pixelgl.Window, spriteSheet pixel.Picture) Player {
|
||||
var p Player
|
||||
p.X = win.Bounds().Max.X / 2
|
||||
p.Y = win.Bounds().Max.Y / 2
|
||||
p.PlayerVec = pixel.Vec{
|
||||
X: win.Bounds().Max.X / 2,
|
||||
Y: win.Bounds().Max.Y / 2,
|
||||
@@ -27,7 +26,8 @@ func NewPlayer(win *pixelgl.Window, spriteSheet pixel.Picture) Player {
|
||||
p.sprFrame = 9
|
||||
p.spriteSheet = spriteSheet
|
||||
p.sprFrames = createSpriteFrames(spriteSheet)
|
||||
p.sprite = pixel.NewSprite(spriteSheet, p.sprFrames[p.sprFrame])
|
||||
p.animation = NewAnimation(p.spriteSheet, p.sprFrames)
|
||||
//p.sprite = pixel.NewSprite(spriteSheet, p.sprFrames[p.sprFrame])
|
||||
p.speed = 10.0
|
||||
return p
|
||||
}
|
||||
@@ -38,28 +38,40 @@ func (p *Player) Update(win *pixelgl.Window, dt int64) {
|
||||
|
||||
if win.Pressed(pixelgl.KeyA) { //left
|
||||
p.sprFrame = 6
|
||||
p.dir = 2
|
||||
vecX -= speed
|
||||
}
|
||||
if win.Pressed(pixelgl.KeyD) { //Right
|
||||
p.sprFrame = 3
|
||||
p.dir = 3
|
||||
vecX += speed
|
||||
}
|
||||
if win.Pressed(pixelgl.KeyS) { //Down
|
||||
p.sprFrame = 9
|
||||
p.dir = 1
|
||||
vecY -= speed
|
||||
}
|
||||
if win.Pressed(pixelgl.KeyW) { //up
|
||||
p.sprFrame = 0
|
||||
p.dir = 0
|
||||
vecY += speed
|
||||
}
|
||||
p.sprite.Set(p.spriteSheet, p.sprFrames[p.sprFrame])
|
||||
if vecY == 0 && vecX == 0 {
|
||||
p.animation.Idle = true
|
||||
} else {
|
||||
p.animation.Idle = false
|
||||
}
|
||||
//p.sprite.Set(p.spriteSheet, p.sprFrames[p.sprFrame])
|
||||
p.animation.Dir = p.dir
|
||||
p.animation.Update(dt)
|
||||
|
||||
p.PlayerVec.X += vecX
|
||||
p.PlayerVec.Y += vecY
|
||||
}
|
||||
|
||||
func (p *Player) Render(win *pixelgl.Window) { //include batch later
|
||||
p.sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(p.PlayerVec))
|
||||
//p.sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(p.PlayerVec))
|
||||
p.animation.Render(win, p.PlayerVec)
|
||||
}
|
||||
|
||||
func createSpriteFrames(spriteSheet pixel.Picture) []pixel.Rect {
|
||||
|
||||
10
main.go
10
main.go
@@ -9,10 +9,10 @@ import (
|
||||
|
||||
_ "image/png"
|
||||
|
||||
"gitbutter.pw/zolfite/project-rpg/gameobjects"
|
||||
"github.com/faiface/pixel"
|
||||
"github.com/faiface/pixel/pixelgl"
|
||||
"golang.org/x/image/colornames"
|
||||
"gitbutter.pw/zolfite/project-rpg/gameobjects"
|
||||
)
|
||||
|
||||
func loadPicture(path string) (pixel.Picture, error) {
|
||||
@@ -54,7 +54,7 @@ func run() {
|
||||
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
|
||||
|
||||
var (
|
||||
camPos = pixel.ZV
|
||||
camPos = pixel.ZV
|
||||
camZoom = 0.3
|
||||
camZoomSpeed = 1.2
|
||||
)
|
||||
@@ -85,9 +85,9 @@ func run() {
|
||||
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
|
||||
|
||||
win.SetMatrix(cam)
|
||||
|
||||
|
||||
player.Update(win, dt)
|
||||
|
||||
|
||||
camPos.X = player.PlayerVec.X
|
||||
camPos.Y = player.PlayerVec.Y
|
||||
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
|
||||
@@ -103,7 +103,7 @@ func run() {
|
||||
win.Update()
|
||||
|
||||
// *** Render End *** //
|
||||
|
||||
dt = 0
|
||||
frames++
|
||||
select {
|
||||
case <-second:
|
||||
|
||||
Reference in New Issue
Block a user