Compare commits
26 Commits
release-v0
...
added-tile
Author | SHA1 | Date | |
---|---|---|---|
67af93b83c | |||
f07d741077 | |||
64d8907741 | |||
c9feadf547 | |||
7d9f54c013 | |||
93fd0c714d | |||
423243302c | |||
f35e404703 | |||
748f605bff | |||
297e33d89e | |||
452bd9cdc3 | |||
965b7701bc | |||
6d65d6b783 | |||
4ad0995076 | |||
c6b9153782 | |||
7ed2ca2ca0 | |||
0f232e3c92 | |||
a51d02daf6 | |||
69977a3096 | |||
373f5800ea | |||
ea62e2dca4 | |||
90daf028e1 | |||
311a15e3e0 | |||
a0de8fd135 | |||
3720389fef | |||
b2e8ec4f3d |
21
LICENSE
Normal file
21
LICENSE
Normal 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
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
|
||||
}
|
BIN
hiking.png
BIN
hiking.png
Binary file not shown.
Before Width: | Height: | Size: 68 KiB |
105
main.go
105
main.go
@ -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
BIN
res/background.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
BIN
res/sprites.png
Normal file
BIN
res/sprites.png
Normal file
Binary file not shown.
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 |
Reference in New Issue
Block a user