Compare commits

...

11 Commits

Author SHA1 Message Date
Jalen Winslow
67af93b83c moved images to res, changed code accordingly 2018-02-02 05:54:21 -07:00
Jalen Winslow
f07d741077 added tileSpriteSheet.png 2018-02-02 05:46:28 -07:00
Jalen Winslow
64d8907741 added speed to animation 2018-01-24 06:04:24 -07:00
c9feadf547 Animate sprite 2018-01-24 05:56:48 -07:00
7d9f54c013 format and remove unused pointer 2018-01-24 02:14:19 -07:00
Jalen Winslow
93fd0c714d fixed non-working player 2018-01-23 12:08:30 -07:00
Jalen Winslow
423243302c added non-working playerObject 2018-01-23 11:58:35 -07:00
Jalen Winslow
f35e404703 slowed down the sprite 2018-01-23 06:12:38 -07:00
Jalen Winslow
748f605bff animated sprite 2018-01-23 06:06:35 -07:00
Jalen Winslow
297e33d89e added frames only concerning sprite 2018-01-23 05:52:53 -07:00
Jalen Winslow
452bd9cdc3 added frames only concerning sprite 2018-01-23 05:38:52 -07:00
6 changed files with 174 additions and 63 deletions

78
gameobjects/animate.go Normal file
View 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
View 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
}

71
main.go
View File

@ -9,6 +9,7 @@ import (
_ "image/png" _ "image/png"
"gitbutter.pw/zolfite/project-rpg/gameobjects"
"github.com/faiface/pixel" "github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl" "github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames" "golang.org/x/image/colornames"
@ -38,13 +39,13 @@ func run() {
panic(err) panic(err)
} }
spritesheet, err := loadPicture("sprites.png") spritesheet, err := loadPicture("res/sprites.png")
if err != nil { if err != nil {
panic(err) panic(err)
} }
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet) //batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
background, err := loadPicture("background.png") background, err := loadPicture("res/background.png")
if err != nil { if err != nil {
panic(err) panic(err)
@ -52,24 +53,8 @@ func run() {
bg := pixel.NewSprite(background, background.Bounds()) bg := pixel.NewSprite(background, background.Bounds())
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet) //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 ( var (
camPos = pixel.ZV camPos = pixel.ZV
//camSpeed = 500.0
camZoom = 0.3 camZoom = 0.3
camZoomSpeed = 1.2 camZoomSpeed = 1.2
) )
@ -78,15 +63,7 @@ func run() {
frames = 0 frames = 0
second = time.Tick(time.Second) second = time.Tick(time.Second)
) )
playerX := win.Bounds().Max.X / 2 player := gameobjects.NewPlayer(win, spritesheet)
playerY := win.Bounds().Max.Y / 2
var (
playerXY = pixel.Vec{
playerX,
playerY,
}
)
var fps int64 = 60 var fps int64 = 60
timePerFrame := 1000000000 / fps timePerFrame := 1000000000 / fps
@ -105,44 +82,14 @@ func run() {
// *** Update begins *** // // *** Update begins *** //
playerXY = pixel.Vec{
playerX,
playerY,
}
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos)) cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
win.SetMatrix(cam) 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 player.Update(win, dt)
if you click the button to make a tree. It only created it once and added it.
*/
//mouse := cam.Unproject(win.MousePosition()) camPos.X = player.PlayerVec.X
camPos.Y = player.PlayerVec.Y
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
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y) camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
// *** Update Ends *** // // *** Update Ends *** //
@ -150,13 +97,13 @@ func run() {
// *** Render begin *** // // *** Render begin *** //
win.Clear(colornames.Forestgreen) win.Clear(colornames.Forestgreen)
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center())) 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) //batch.Draw(win)
win.Update() win.Update()
// *** Render End *** // // *** Render End *** //
dt = 0
frames++ frames++
select { select {
case <-second: case <-second:

View File

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

BIN
res/tileSpriteSheet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB