Refactor playerCurrentSpeed; refactor player data into struct

This commit is contained in:
Logen Kain 2018-01-19 14:02:28 -07:00
parent 32b32a88bf
commit 6f6faf7cb9

142
main.go
View File

@ -14,6 +14,18 @@ import (
"golang.org/x/image/colornames"
)
type Player struct {
sprite *pixel.Sprite
x, y,
drag,
maxSpeed,
xV,
yV,
angularVelocity,
acceleration float64
vec pixel.Vec
}
func loadSprite(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
@ -56,10 +68,20 @@ func run() {
if err != nil {
panic(err)
}
shipFrames := createSpriteSheet(spritesheet, 256, 256)
player := pixel.NewSprite(spritesheet, shipFrames[0])
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
var angle float64 = 0.0
var angleInDegrees float64 = 0
@ -68,17 +90,6 @@ func run() {
second = time.Tick(time.Second)
)
playerX := win.Bounds().Max.X / 2
playerY := win.Bounds().Max.Y / 8
var playerTopSpeed float64 = 350
var playerAcceleration float64 = 0.15 * playerTopSpeed
var playerCurrentSpeed float64 = 0
var playerDrag float64 = 1
var playerXVelocity float64 = 0
var playerYVelocity float64 = 0
var myVec = pixel.Vec{playerX, playerY}
last := time.Now()
for !win.Closed() {
dt := time.Since(last).Seconds()
@ -86,14 +97,14 @@ func run() {
win.Clear(colornames.Whitesmoke)
myVec = pixel.Vec{playerX, playerY}
player.vec = pixel.Vec{player.x, player.y}
mat := pixel.IM
mat = mat.Rotated(pixel.ZV, angle)
mat = mat.Scaled(pixel.ZV, 0.25)
mat = mat.Moved(myVec)
mat = mat.Moved(player.vec)
player.Draw(win, mat)
player.sprite.Draw(win, mat)
if win.Pressed(pixelgl.KeyLeft) {
@ -109,27 +120,24 @@ func run() {
}
if win.Pressed(pixelgl.KeyUp) {
if playerCurrentSpeed < playerTopSpeed {
playerCurrentSpeed += playerAcceleration
player.xV +=
math.Cos(angle-(270*math.Pi)/180) * player.acceleration * dt
player.yV +=
math.Sin(angle-(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
}
playerXVelocity +=
math.Cos(angle-(270*math.Pi)/180) * playerCurrentSpeed * dt
playerYVelocity +=
math.Sin(angle-(270*math.Pi)/180) * playerCurrentSpeed * dt
if playerXVelocity > playerTopSpeed {
playerXVelocity = playerTopSpeed
if player.yV > player.maxSpeed {
player.yV = player.maxSpeed
}
if playerXVelocity < playerTopSpeed*-1 {
playerXVelocity = playerTopSpeed * -1
}
if playerYVelocity > playerTopSpeed {
playerYVelocity = playerTopSpeed
}
if playerYVelocity < playerTopSpeed*-1 {
playerYVelocity = playerTopSpeed * -1
if player.yV < player.maxSpeed*-1 {
player.yV = player.maxSpeed * -1
}
}
@ -155,56 +163,48 @@ func run() {
angle = angle + (360*math.Pi)/180
}
if playerCurrentSpeed > 0 {
playerCurrentSpeed -= playerDrag * dt
if player.x >= win.Bounds().Max.X-10 {
player.xV = -25
}
if player.x <= win.Bounds().Min.X+10 {
player.xV = 25
}
if playerCurrentSpeed < 0 {
playerCurrentSpeed = 0
if player.y >= win.Bounds().Max.Y-10 {
player.yV = -25
}
if player.y <= win.Bounds().Min.Y+10 {
player.yV = 25
}
if playerX >= win.Bounds().Max.X-10 {
playerXVelocity = -25
}
if playerX <= win.Bounds().Min.X+10 {
playerXVelocity = 25
}
player.x += player.xV * dt
player.y += player.yV * dt
if playerY >= win.Bounds().Max.Y-10 {
playerYVelocity = -25
}
if playerY <= win.Bounds().Min.Y+10 {
playerYVelocity = 25
}
playerX += playerXVelocity * dt
playerY += playerYVelocity * dt
if playerYVelocity > 0 {
playerYVelocity -= playerDrag
if playerYVelocity < 0 {
playerYVelocity = 0
if player.yV > 0 {
player.yV -= player.drag
if player.yV < 0 {
player.yV = 0
}
}
if playerYVelocity < 0 {
playerYVelocity += playerDrag
if playerYVelocity > 0 {
playerYVelocity = 0
if player.yV < 0 {
player.yV += player.drag
if player.yV > 0 {
player.yV = 0
}
}
if playerXVelocity > 0 {
playerXVelocity -= playerDrag
if playerXVelocity < 0 {
playerXVelocity = 0
if player.xV > 0 {
player.xV -= player.drag
if player.xV < 0 {
player.xV = 0
}
}
if playerXVelocity < 0 {
playerXVelocity += playerDrag
if playerXVelocity > 0 {
playerXVelocity = 0
if player.xV < 0 {
player.xV += player.drag
if player.xV > 0 {
player.xV = 0
}
}
@ -214,7 +214,7 @@ func run() {
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d | playerYSpeed: %f", cfgMain.Title,
frames, playerYVelocity))
frames, player.yV))
frames = 0
default:
}