26 Commits

Author SHA1 Message Date
67af93b83c moved images to res, changed code accordingly 2018-02-02 05:54:21 -07:00
f07d741077 added tileSpriteSheet.png 2018-02-02 05:46:28 -07:00
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
93fd0c714d fixed non-working player 2018-01-23 12:08:30 -07:00
423243302c added non-working playerObject 2018-01-23 11:58:35 -07:00
f35e404703 slowed down the sprite 2018-01-23 06:12:38 -07:00
748f605bff animated sprite 2018-01-23 06:06:35 -07:00
297e33d89e added frames only concerning sprite 2018-01-23 05:52:53 -07:00
452bd9cdc3 added frames only concerning sprite 2018-01-23 05:38:52 -07:00
965b7701bc Increase speed to 10 2018-01-23 03:20:29 -07:00
6d65d6b783 Change window title 2018-01-23 03:19:02 -07:00
4ad0995076 Merge jalen's changes 2018-01-23 03:15:39 -07:00
c6b9153782 added direction change to sprite 2018-01-22 10:09:33 -07:00
7ed2ca2ca0 Remove unused sprite images 2018-01-19 08:51:03 -07:00
0f232e3c92 Merge branch 'master' of https://gitbutter.pw/zolfite/project-rpg 2018-01-19 08:48:39 -07:00
a51d02daf6 change sprite image to character 2018-01-19 08:47:53 -07:00
69977a3096 Change tree sprite to character sprite; use wasd instead of arrow keys 2018-01-19 08:47:25 -07:00
373f5800ea Add background image for reference 2018-01-19 07:41:05 -07:00
ea62e2dca4 Bind camera and sprite 2018-01-19 07:40:48 -07:00
90daf028e1 Add license 2018-01-17 19:19:06 +00:00
311a15e3e0 add camera to move with w,a,s,d 2018-01-16 21:54:57 -07:00
a0de8fd135 add tree asset 2018-01-16 21:33:37 -07:00
3720389fef draw trees with left mouse click 2018-01-16 21:33:27 -07:00
b2e8ec4f3d Added a handler, update, and render 2018-01-16 07:35:04 -07:00
8 changed files with 252 additions and 38 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Zolfite
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

105
main.go
View File

@ -1,28 +1,26 @@
// main
package main
import (
"fmt"
"image"
_ "image/png"
"math"
"os"
"time"
_ "image/png"
"gitbutter.pw/zolfite/project-rpg/gameobjects"
"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
@ -30,59 +28,90 @@ 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!",
Title: "Project RPG",
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)
}
//smooth the edges of the image (antialiasing)
win.SetSmooth(true)
//load a specific image and catch any errors
pic, err := loadPicture("hiking.png")
spritesheet, err := loadPicture("res/sprites.png")
if err != nil {
panic(err)
}
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
//create a sprite using the loaded image
sprite := pixel.NewSprite(pic, pic.Bounds())
background, err := loadPicture("res/background.png")
//set the angle of the image
angle := 0.0
if err != nil {
panic(err)
}
bg := pixel.NewSprite(background, background.Bounds())
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
//set the time to compare later
last := time.Now()
var (
camPos = pixel.ZV
camZoom = 0.3
camZoomSpeed = 1.2
)
//loop until window is closed by "X" button
var (
frames = 0
second = time.Tick(time.Second)
)
player := gameobjects.NewPlayer(win, spritesheet)
var fps int64 = 60
timePerFrame := 1000000000 / fps
var lastTime int64 = time.Now().UnixNano()
var now int64
var dt int64
// Game Loop
for !win.Closed() {
//compare the last time to delta time
dt := time.Since(last).Seconds()
last = time.Now()
now = time.Now().UnixNano()
dt += (now - lastTime)
lastTime = time.Now().UnixNano()
//set the angle according to delta time
angle += 3 * dt
//Execute a frame.
if dt >= timePerFrame {
//refresh screen and set background color
win.Clear(colornames.Firebrick)
// *** Update begins *** //
//set the matrix and location of spirte in window
mat := pixel.IM
mat = mat.Rotated(pixel.ZV, angle)
mat = mat.Moved(win.Bounds().Center())
sprite.Draw(win, mat)
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
//update the window
win.Update()
win.SetMatrix(cam)
player.Update(win, dt)
camPos.X = player.PlayerVec.X
camPos.Y = player.PlayerVec.Y
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
// *** Update Ends *** //
// *** Render begin *** //
win.Clear(colornames.Forestgreen)
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
player.Render(win)
//batch.Draw(win)
win.Update()
// *** Render End *** //
dt = 0
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
}
}
}

BIN
res/background.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
res/sprites.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

BIN
res/tileSpriteSheet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB