Compare commits
11 Commits
master
...
added-tile
Author | SHA1 | Date | |
---|---|---|---|
|
67af93b83c | ||
|
f07d741077 | ||
|
64d8907741 | ||
c9feadf547 | |||
7d9f54c013 | |||
|
93fd0c714d | ||
|
423243302c | ||
|
f35e404703 | ||
|
748f605bff | ||
|
297e33d89e | ||
|
452bd9cdc3 |
78
gameobjects/animate.go
Normal file
78
gameobjects/animate.go
Normal file
@ -0,0 +1,78 @@
|
||||
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
|
||||
Speed int64
|
||||
}
|
||||
|
||||
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
|
||||
animation.Speed = 6
|
||||
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/ a.Speed{
|
||||
a.Frame++
|
||||
elapsedTime -= 1000000000 / a.Speed
|
||||
}
|
||||
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))
|
||||
|
||||
}
|
86
gameobjects/player.go
Normal file
86
gameobjects/player.go
Normal file
@ -0,0 +1,86 @@
|
||||
package gameobjects
|
||||
|
||||
import (
|
||||
"github.com/faiface/pixel"
|
||||
"github.com/faiface/pixel/pixelgl"
|
||||
//"fmt"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
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.PlayerVec = pixel.Vec{
|
||||
X: win.Bounds().Max.X / 2,
|
||||
Y: win.Bounds().Max.Y / 2,
|
||||
}
|
||||
p.sprFrame = 9
|
||||
p.spriteSheet = spriteSheet
|
||||
p.sprFrames = createSpriteFrames(spriteSheet)
|
||||
p.animation = NewAnimation(p.spriteSheet, p.sprFrames)
|
||||
//p.sprite = pixel.NewSprite(spriteSheet, p.sprFrames[p.sprFrame])
|
||||
p.speed = 10.0
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Player) Update(win *pixelgl.Window, dt int64) {
|
||||
var vecX, vecY float64 = 0, 0
|
||||
speed := p.speed
|
||||
|
||||
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
|
||||
}
|
||||
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.animation.Render(win, p.PlayerVec)
|
||||
}
|
||||
|
||||
func createSpriteFrames(spriteSheet pixel.Picture) []pixel.Rect {
|
||||
var sprFrames []pixel.Rect
|
||||
var frameHeight, frameWidth float64 = 96, 96
|
||||
for y := spriteSheet.Bounds().Min.Y; y < frameHeight*4; y += 96 {
|
||||
for x := spriteSheet.Bounds().Min.X; x < frameWidth*3; x += 96 {
|
||||
sprFrames = append(sprFrames, pixel.R(x, y, x+96, y+96)) // (x, y, width, height) of frame
|
||||
}
|
||||
}
|
||||
return sprFrames
|
||||
}
|
73
main.go
73
main.go
@ -9,6 +9,7 @@ import (
|
||||
|
||||
_ "image/png"
|
||||
|
||||
"gitbutter.pw/zolfite/project-rpg/gameobjects"
|
||||
"github.com/faiface/pixel"
|
||||
"github.com/faiface/pixel/pixelgl"
|
||||
"golang.org/x/image/colornames"
|
||||
@ -38,13 +39,13 @@ func run() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
spritesheet, err := loadPicture("sprites.png")
|
||||
spritesheet, err := loadPicture("res/sprites.png")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
|
||||
|
||||
background, err := loadPicture("background.png")
|
||||
background, err := loadPicture("res/background.png")
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -52,24 +53,8 @@ func run() {
|
||||
bg := pixel.NewSprite(background, background.Bounds())
|
||||
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
|
||||
|
||||
/*
|
||||
When creating the sprite, you need to know big each frame is inside the spritesheet.
|
||||
If you use aseprite or some other pixel art program, you can find the number of pixels
|
||||
within each frame. For our sprite sheet, each frame is 96 px wide and 96 px high. So
|
||||
we increment it by 96 for both x and y so that it can get the position of the frame
|
||||
at the bottom right corner.
|
||||
*/
|
||||
var spritesFrames []pixel.Rect
|
||||
for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 96 {
|
||||
for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 96 {
|
||||
spritesFrames = append(spritesFrames, pixel.R(x, y, x+96, y+96)) // (x, y, width, height) of frame
|
||||
}
|
||||
}
|
||||
Sprite := pixel.NewSprite(spritesheet, spritesFrames[3])
|
||||
|
||||
var (
|
||||
camPos = pixel.ZV
|
||||
//camSpeed = 500.0
|
||||
camPos = pixel.ZV
|
||||
camZoom = 0.3
|
||||
camZoomSpeed = 1.2
|
||||
)
|
||||
@ -78,15 +63,7 @@ func run() {
|
||||
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,
|
||||
}
|
||||
)
|
||||
player := gameobjects.NewPlayer(win, spritesheet)
|
||||
|
||||
var fps int64 = 60
|
||||
timePerFrame := 1000000000 / fps
|
||||
@ -105,44 +82,14 @@ func run() {
|
||||
|
||||
// *** Update begins *** //
|
||||
|
||||
playerXY = pixel.Vec{
|
||||
playerX,
|
||||
playerY,
|
||||
}
|
||||
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
|
||||
|
||||
win.SetMatrix(cam)
|
||||
/*
|
||||
This is where Sprite and bg was originally. Creating them over and over inside
|
||||
the main loop is not very good resource management. When i added my own dt, the game was
|
||||
only going about 22 fps, when it should've been going 60. I moved them
|
||||
out of the loop and created them up top. It fixed it and now runs faster.
|
||||
|
||||
the tutorial only has it in the loop with the exception that it creates it only
|
||||
if you click the button to make a tree. It only created it once and added it.
|
||||
*/
|
||||
player.Update(win, dt)
|
||||
|
||||
//mouse := cam.Unproject(win.MousePosition())
|
||||
|
||||
speed := 10.0
|
||||
if win.Pressed(pixelgl.KeyA) { //left
|
||||
playerX -= speed
|
||||
Sprite.Set(spritesheet, spritesFrames[2])
|
||||
}
|
||||
if win.Pressed(pixelgl.KeyD) { //Right
|
||||
playerX += speed
|
||||
Sprite.Set(spritesheet, spritesFrames[1])
|
||||
}
|
||||
if win.Pressed(pixelgl.KeyS) { //Down
|
||||
playerY -= speed
|
||||
Sprite.Set(spritesheet, spritesFrames[3])
|
||||
}
|
||||
if win.Pressed(pixelgl.KeyW) { //up
|
||||
playerY += speed
|
||||
Sprite.Set(spritesheet, spritesFrames[0])
|
||||
}
|
||||
camPos.X = playerX
|
||||
camPos.Y = playerY
|
||||
camPos.X = player.PlayerVec.X
|
||||
camPos.Y = player.PlayerVec.Y
|
||||
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
|
||||
|
||||
// *** Update Ends *** //
|
||||
@ -150,13 +97,13 @@ func run() {
|
||||
// *** Render begin *** //
|
||||
win.Clear(colornames.Forestgreen)
|
||||
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
|
||||
Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(playerXY))
|
||||
player.Render(win)
|
||||
|
||||
//batch.Draw(win)
|
||||
win.Update()
|
||||
|
||||
// *** Render End *** //
|
||||
|
||||
dt = 0
|
||||
frames++
|
||||
select {
|
||||
case <-second:
|
||||
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
BIN
res/tileSpriteSheet.png
Normal file
BIN
res/tileSpriteSheet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.9 KiB |
Loading…
x
Reference in New Issue
Block a user