252 lines
4.9 KiB
Go
252 lines
4.9 KiB
Go
// main.go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
_ "image/png"
|
|
"math"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/faiface/pixel"
|
|
"github.com/faiface/pixel/pixelgl"
|
|
"golang.org/x/image/colornames"
|
|
)
|
|
|
|
type Player struct {
|
|
sprite *pixel.Sprite
|
|
x, y,
|
|
drag,
|
|
maxSpeed,
|
|
xV,
|
|
yV,
|
|
angularVelocity,
|
|
acceleration,
|
|
angleRad,
|
|
angleDeg float64
|
|
vec pixel.Vec
|
|
}
|
|
|
|
func playerDrag(player *Player) {
|
|
if player.yV > 0 {
|
|
player.yV -= player.drag
|
|
if player.yV < 0 {
|
|
player.yV = 0
|
|
}
|
|
}
|
|
|
|
if player.yV < 0 {
|
|
player.yV += player.drag
|
|
if player.yV > 0 {
|
|
player.yV = 0
|
|
}
|
|
}
|
|
|
|
if player.xV > 0 {
|
|
player.xV -= player.drag
|
|
if player.xV < 0 {
|
|
player.xV = 0
|
|
}
|
|
}
|
|
|
|
if player.xV < 0 {
|
|
player.xV += player.drag
|
|
if player.xV > 0 {
|
|
player.xV = 0
|
|
}
|
|
}
|
|
//return player
|
|
}
|
|
|
|
func loadSprite(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
|
|
}
|
|
|
|
func playerAdjustSpeed(player *Player, dt float64) {
|
|
player.x += player.xV * dt
|
|
player.y += player.yV * dt
|
|
}
|
|
|
|
func createSpriteSheet(SpriteSheetImg pixel.Picture,
|
|
spriteSizeX, spriteSizeY float64) []pixel.Rect {
|
|
|
|
var frames []pixel.Rect
|
|
|
|
for x := SpriteSheetImg.Bounds().Min.X; x < SpriteSheetImg.Bounds().Max.X; x += spriteSizeX {
|
|
for y := SpriteSheetImg.Bounds().Min.Y; y < SpriteSheetImg.Bounds().Max.Y; y += spriteSizeY {
|
|
frames = append(frames, pixel.R(x, y, x+256, y+256))
|
|
}
|
|
}
|
|
return frames
|
|
}
|
|
|
|
func run() {
|
|
|
|
cfgMain := pixelgl.WindowConfig{
|
|
Title: "AsterCal",
|
|
Bounds: pixel.R(0, 0, 1024, 768),
|
|
VSync: true,
|
|
}
|
|
|
|
win, err := pixelgl.NewWindow(cfgMain)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
spritesheet, err := loadSprite("assets/ship_sprite_sheet.png")
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
shipFrames := createSpriteSheet(spritesheet, 256, 256)
|
|
|
|
var player Player
|
|
player.x = win.Bounds().Max.X / 2
|
|
player.y = win.Bounds().Max.Y / 8
|
|
player.sprite = pixel.NewSprite(spritesheet, shipFrames[0])
|
|
player.maxSpeed = 350
|
|
player.acceleration = 600 //0.15 * player.maxSpeed
|
|
player.drag = 1
|
|
player.xV = 0
|
|
player.yV = 0
|
|
player.vec.X = player.x
|
|
player.vec.Y = player.y
|
|
player.angularVelocity = 3
|
|
player.angleRad = 0.0
|
|
player.angleDeg = 0
|
|
|
|
var (
|
|
frames = 0
|
|
second = time.Tick(time.Second)
|
|
)
|
|
|
|
last := time.Now()
|
|
for !win.Closed() {
|
|
dt := time.Since(last).Seconds()
|
|
last = time.Now()
|
|
win.Clear(colornames.Whitesmoke)
|
|
|
|
player.vec = pixel.Vec{player.x, player.y}
|
|
|
|
mat := pixel.IM
|
|
mat = mat.Rotated(pixel.ZV, player.angleRad)
|
|
mat = mat.Scaled(pixel.ZV, 0.25)
|
|
mat = mat.Moved(player.vec)
|
|
|
|
player.sprite.Draw(win, mat)
|
|
|
|
if win.Pressed(pixelgl.KeyLeft) {
|
|
|
|
player.angleRad += player.angularVelocity * dt
|
|
}
|
|
if win.Pressed(pixelgl.KeyRight) {
|
|
player.angleRad -= player.angularVelocity * dt
|
|
|
|
}
|
|
|
|
if win.Pressed(pixelgl.KeyEscape) {
|
|
win.SetClosed(true)
|
|
}
|
|
|
|
if win.JustReleased(pixelgl.KeyF12) {
|
|
fmt.Printf("Player.x: %f \n", player.x)
|
|
fmt.Printf("Player.y: %f \n", player.y)
|
|
fmt.Printf("Max Speed: %f \n", player.maxSpeed)
|
|
fmt.Printf("Acceleration: %f \n", player.acceleration)
|
|
fmt.Printf("Drag: %f \n", player.drag)
|
|
fmt.Printf("X Velocity: %f \n", player.xV)
|
|
fmt.Printf("Y Velocity: %f \n", player.yV)
|
|
fmt.Printf("Angular Velocity: %f \n", player.angularVelocity)
|
|
fmt.Printf("Current Angle Rad: %f \n", player.angleRad)
|
|
fmt.Printf("Current Angle Deg: %f \n\n\n\n", player.angleDeg)
|
|
}
|
|
if win.Pressed(pixelgl.KeyUp) {
|
|
|
|
player.xV +=
|
|
math.Cos(player.angleRad-(270*math.Pi)/180) * player.acceleration * dt
|
|
player.yV +=
|
|
math.Sin(player.angleRad-(270*math.Pi)/180) * player.acceleration * dt
|
|
|
|
if player.xV > player.maxSpeed {
|
|
player.xV = player.maxSpeed
|
|
}
|
|
if player.xV < player.maxSpeed*-1 {
|
|
player.xV = player.maxSpeed * -1
|
|
}
|
|
|
|
if player.yV > player.maxSpeed {
|
|
player.yV = player.maxSpeed
|
|
}
|
|
if player.yV < player.maxSpeed*-1 {
|
|
player.yV = player.maxSpeed * -1
|
|
}
|
|
|
|
}
|
|
if win.Pressed(pixelgl.KeyDown) {
|
|
|
|
}
|
|
|
|
if player.angleRad == 0 {
|
|
player.angleDeg = 0
|
|
} else {
|
|
|
|
player.angleDeg = (player.angleRad) * 180 / math.Pi
|
|
}
|
|
|
|
if player.angleDeg == 360 || player.angleDeg == -360 {
|
|
player.angleRad = 0
|
|
}
|
|
|
|
if player.angleDeg > 360 {
|
|
player.angleRad = player.angleRad - (360*math.Pi)/180
|
|
}
|
|
if player.angleDeg < -360 {
|
|
player.angleRad = player.angleRad + (360*math.Pi)/180
|
|
}
|
|
|
|
if player.x >= win.Bounds().Max.X-10 {
|
|
player.xV = -25
|
|
}
|
|
if player.x <= win.Bounds().Min.X+10 {
|
|
player.xV = 25
|
|
}
|
|
|
|
if player.y >= win.Bounds().Max.Y-10 {
|
|
player.yV = -25
|
|
}
|
|
if player.y <= win.Bounds().Min.Y+10 {
|
|
player.yV = 25
|
|
}
|
|
|
|
playerAdjustSpeed(&player, dt)
|
|
|
|
playerDrag(&player)
|
|
|
|
win.Update()
|
|
|
|
frames++
|
|
select {
|
|
case <-second:
|
|
win.SetTitle(fmt.Sprintf("%s | FPS: %d | playerYSpeed: %f", cfgMain.Title,
|
|
frames, player.yV))
|
|
frames = 0
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
pixelgl.Run(run)
|
|
}
|