project-rpg/main.go

121 lines
2.2 KiB
Go
Raw Normal View History

2018-01-15 21:41:59 -07:00
package main
import (
2018-01-19 07:40:48 -07:00
"fmt"
2018-01-15 23:20:34 -07:00
"image"
2018-01-19 07:40:48 -07:00
"math"
2018-01-15 23:20:34 -07:00
"os"
2018-01-16 21:54:57 -07:00
"time"
2018-01-15 23:20:34 -07:00
2018-01-19 07:40:48 -07:00
_ "image/png"
2018-01-24 05:56:48 -07:00
"gitbutter.pw/zolfite/project-rpg/gameobjects"
2018-01-15 22:42:11 -07:00
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
2018-01-15 22:50:18 -07:00
"golang.org/x/image/colornames"
2018-01-15 21:41:59 -07:00
)
2018-01-15 23:20:34 -07:00
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
2018-01-15 22:42:11 -07:00
func run() {
cfg := pixelgl.WindowConfig{
2018-01-23 03:19:02 -07:00
Title: "Project RPG",
2018-01-15 22:42:11 -07:00
Bounds: pixel.R(0, 0, 1024, 768),
2018-01-15 22:50:18 -07:00
VSync: true,
2018-01-15 22:42:11 -07:00
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
spritesheet, err := loadPicture("sprites.png")
2018-01-15 23:20:34 -07:00
if err != nil {
panic(err)
}
2018-01-23 03:15:39 -07:00
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
2018-01-15 23:20:34 -07:00
2018-01-19 07:40:48 -07:00
background, err := loadPicture("background.png")
if err != nil {
panic(err)
}
2018-01-22 10:09:33 -07:00
bg := pixel.NewSprite(background, background.Bounds())
2018-01-19 07:40:48 -07:00
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
2018-01-16 21:33:27 -07:00
var (
2018-01-24 05:56:48 -07:00
camPos = pixel.ZV
camZoom = 0.3
2018-01-19 07:40:48 -07:00
camZoomSpeed = 1.2
)
var (
frames = 0
second = time.Tick(time.Second)
)
2018-01-23 11:58:35 -07:00
player := gameobjects.NewPlayer(win, spritesheet)
2018-01-15 22:50:18 -07:00
2018-01-22 10:09:33 -07:00
var fps int64 = 60
2018-01-23 03:15:39 -07:00
timePerFrame := 1000000000 / fps
2018-01-22 10:09:33 -07:00
var lastTime int64 = time.Now().UnixNano()
var now int64
var dt int64
2018-01-19 07:40:48 -07:00
2018-01-22 10:09:33 -07:00
// Game Loop
2018-01-15 22:42:11 -07:00
for !win.Closed() {
2018-01-16 07:35:04 -07:00
now = time.Now().UnixNano()
2018-01-23 03:15:39 -07:00
dt += (now - lastTime)
2018-01-22 10:09:33 -07:00
lastTime = time.Now().UnixNano()
2018-01-23 03:15:39 -07:00
2018-01-22 10:09:33 -07:00
//Execute a frame.
2018-01-23 03:15:39 -07:00
if dt >= timePerFrame {
2018-01-16 07:35:04 -07:00
2018-01-22 10:09:33 -07:00
// *** Update begins *** //
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
2018-01-23 03:15:39 -07:00
2018-01-22 10:09:33 -07:00
win.SetMatrix(cam)
2018-01-24 05:56:48 -07:00
2018-01-23 11:58:35 -07:00
player.Update(win, dt)
2018-01-24 05:56:48 -07:00
2018-01-23 11:58:35 -07:00
camPos.X = player.PlayerVec.X
camPos.Y = player.PlayerVec.Y
2018-01-22 10:09:33 -07:00
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
// *** Update Ends *** //
// *** Render begin *** //
win.Clear(colornames.Forestgreen)
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
2018-01-23 11:58:35 -07:00
player.Render(win)
2018-01-23 03:15:39 -07:00
2018-01-22 10:09:33 -07:00
//batch.Draw(win)
win.Update()
2018-01-23 03:15:39 -07:00
2018-01-22 10:09:33 -07:00
// *** Render End *** //
2018-01-24 05:56:48 -07:00
dt = 0
2018-01-22 10:09:33 -07:00
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
2018-01-19 07:40:48 -07:00
}
2018-01-15 22:42:11 -07:00
}
}
2018-01-15 21:41:59 -07:00
func main() {
2018-01-15 22:42:11 -07:00
pixelgl.Run(run)
2018-01-15 21:41:59 -07:00
}