Refactor playerCurrentSpeed; refactor player data into struct
This commit is contained in:
parent
32b32a88bf
commit
6f6faf7cb9
142
main.go
142
main.go
@ -14,6 +14,18 @@ import (
|
|||||||
"golang.org/x/image/colornames"
|
"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) {
|
func loadSprite(path string) (pixel.Picture, error) {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -56,10 +68,20 @@ func run() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
shipFrames := createSpriteSheet(spritesheet, 256, 256)
|
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 angle float64 = 0.0
|
||||||
var angleInDegrees float64 = 0
|
var angleInDegrees float64 = 0
|
||||||
|
|
||||||
@ -68,17 +90,6 @@ func run() {
|
|||||||
second = time.Tick(time.Second)
|
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()
|
last := time.Now()
|
||||||
for !win.Closed() {
|
for !win.Closed() {
|
||||||
dt := time.Since(last).Seconds()
|
dt := time.Since(last).Seconds()
|
||||||
@ -86,14 +97,14 @@ func run() {
|
|||||||
|
|
||||||
win.Clear(colornames.Whitesmoke)
|
win.Clear(colornames.Whitesmoke)
|
||||||
|
|
||||||
myVec = pixel.Vec{playerX, playerY}
|
player.vec = pixel.Vec{player.x, player.y}
|
||||||
|
|
||||||
mat := pixel.IM
|
mat := pixel.IM
|
||||||
mat = mat.Rotated(pixel.ZV, angle)
|
mat = mat.Rotated(pixel.ZV, angle)
|
||||||
mat = mat.Scaled(pixel.ZV, 0.25)
|
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) {
|
if win.Pressed(pixelgl.KeyLeft) {
|
||||||
|
|
||||||
@ -109,27 +120,24 @@ func run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if win.Pressed(pixelgl.KeyUp) {
|
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 +=
|
if player.yV > player.maxSpeed {
|
||||||
math.Cos(angle-(270*math.Pi)/180) * playerCurrentSpeed * dt
|
player.yV = player.maxSpeed
|
||||||
playerYVelocity +=
|
|
||||||
math.Sin(angle-(270*math.Pi)/180) * playerCurrentSpeed * dt
|
|
||||||
|
|
||||||
if playerXVelocity > playerTopSpeed {
|
|
||||||
playerXVelocity = playerTopSpeed
|
|
||||||
}
|
}
|
||||||
if playerXVelocity < playerTopSpeed*-1 {
|
if player.yV < player.maxSpeed*-1 {
|
||||||
playerXVelocity = playerTopSpeed * -1
|
player.yV = player.maxSpeed * -1
|
||||||
}
|
|
||||||
|
|
||||||
if playerYVelocity > playerTopSpeed {
|
|
||||||
playerYVelocity = playerTopSpeed
|
|
||||||
}
|
|
||||||
if playerYVelocity < playerTopSpeed*-1 {
|
|
||||||
playerYVelocity = playerTopSpeed * -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -155,56 +163,48 @@ func run() {
|
|||||||
angle = angle + (360*math.Pi)/180
|
angle = angle + (360*math.Pi)/180
|
||||||
}
|
}
|
||||||
|
|
||||||
if playerCurrentSpeed > 0 {
|
if player.x >= win.Bounds().Max.X-10 {
|
||||||
playerCurrentSpeed -= playerDrag * dt
|
player.xV = -25
|
||||||
|
}
|
||||||
|
if player.x <= win.Bounds().Min.X+10 {
|
||||||
|
player.xV = 25
|
||||||
}
|
}
|
||||||
|
|
||||||
if playerCurrentSpeed < 0 {
|
if player.y >= win.Bounds().Max.Y-10 {
|
||||||
playerCurrentSpeed = 0
|
player.yV = -25
|
||||||
|
}
|
||||||
|
if player.y <= win.Bounds().Min.Y+10 {
|
||||||
|
player.yV = 25
|
||||||
}
|
}
|
||||||
|
|
||||||
if playerX >= win.Bounds().Max.X-10 {
|
player.x += player.xV * dt
|
||||||
playerXVelocity = -25
|
player.y += player.yV * dt
|
||||||
}
|
|
||||||
if playerX <= win.Bounds().Min.X+10 {
|
|
||||||
playerXVelocity = 25
|
|
||||||
}
|
|
||||||
|
|
||||||
if playerY >= win.Bounds().Max.Y-10 {
|
if player.yV > 0 {
|
||||||
playerYVelocity = -25
|
player.yV -= player.drag
|
||||||
}
|
if player.yV < 0 {
|
||||||
if playerY <= win.Bounds().Min.Y+10 {
|
player.yV = 0
|
||||||
playerYVelocity = 25
|
|
||||||
}
|
|
||||||
|
|
||||||
playerX += playerXVelocity * dt
|
|
||||||
playerY += playerYVelocity * dt
|
|
||||||
|
|
||||||
if playerYVelocity > 0 {
|
|
||||||
playerYVelocity -= playerDrag
|
|
||||||
if playerYVelocity < 0 {
|
|
||||||
playerYVelocity = 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if playerYVelocity < 0 {
|
if player.yV < 0 {
|
||||||
playerYVelocity += playerDrag
|
player.yV += player.drag
|
||||||
if playerYVelocity > 0 {
|
if player.yV > 0 {
|
||||||
playerYVelocity = 0
|
player.yV = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if playerXVelocity > 0 {
|
if player.xV > 0 {
|
||||||
playerXVelocity -= playerDrag
|
player.xV -= player.drag
|
||||||
if playerXVelocity < 0 {
|
if player.xV < 0 {
|
||||||
playerXVelocity = 0
|
player.xV = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if playerXVelocity < 0 {
|
if player.xV < 0 {
|
||||||
playerXVelocity += playerDrag
|
player.xV += player.drag
|
||||||
if playerXVelocity > 0 {
|
if player.xV > 0 {
|
||||||
playerXVelocity = 0
|
player.xV = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,7 +214,7 @@ func run() {
|
|||||||
select {
|
select {
|
||||||
case <-second:
|
case <-second:
|
||||||
win.SetTitle(fmt.Sprintf("%s | FPS: %d | playerYSpeed: %f", cfgMain.Title,
|
win.SetTitle(fmt.Sprintf("%s | FPS: %d | playerYSpeed: %f", cfgMain.Title,
|
||||||
frames, playerYVelocity))
|
frames, player.yV))
|
||||||
frames = 0
|
frames = 0
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user