Add angles to Player struct; Move drag and speed change to own functions

This commit is contained in:
Logen Kain 2018-01-19 15:02:54 -07:00
parent 4a1204e5c2
commit 5b78da7abf

123
main.go
View File

@ -22,10 +22,43 @@ type Player struct {
xV, xV,
yV, yV,
angularVelocity, angularVelocity,
acceleration float64 acceleration,
angleRad,
angleDeg float64
vec pixel.Vec 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) { func loadSprite(path string) (pixel.Picture, error) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
@ -39,7 +72,14 @@ func loadSprite(path string) (pixel.Picture, error) {
return pixel.PictureDataFromImage(img), nil return pixel.PictureDataFromImage(img), nil
} }
func createSpriteSheet(SpriteSheetImg pixel.Picture, spriteSizeX, spriteSizeY float64) []pixel.Rect { 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 var frames []pixel.Rect
for x := SpriteSheetImg.Bounds().Min.X; x < SpriteSheetImg.Bounds().Max.X; x += spriteSizeX { for x := SpriteSheetImg.Bounds().Min.X; x < SpriteSheetImg.Bounds().Max.X; x += spriteSizeX {
@ -82,8 +122,9 @@ func run() {
player.yV = 0 player.yV = 0
player.vec.X = player.x player.vec.X = player.x
player.vec.Y = player.y player.vec.Y = player.y
var angle float64 = 0.0 player.angularVelocity = 3
var angleInDegrees float64 = 0 player.angleRad = 0.0
player.angleDeg = 0
var ( var (
frames = 0 frames = 0
@ -94,13 +135,12 @@ func run() {
for !win.Closed() { for !win.Closed() {
dt := time.Since(last).Seconds() dt := time.Since(last).Seconds()
last = time.Now() last = time.Now()
win.Clear(colornames.Whitesmoke) win.Clear(colornames.Whitesmoke)
player.vec = pixel.Vec{player.x, player.y} 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, player.angleRad)
mat = mat.Scaled(pixel.ZV, 0.25) mat = mat.Scaled(pixel.ZV, 0.25)
mat = mat.Moved(player.vec) mat = mat.Moved(player.vec)
@ -108,10 +148,10 @@ func run() {
if win.Pressed(pixelgl.KeyLeft) { if win.Pressed(pixelgl.KeyLeft) {
angle += 3 * dt player.angleRad += player.angularVelocity * dt
} }
if win.Pressed(pixelgl.KeyRight) { if win.Pressed(pixelgl.KeyRight) {
angle -= 3 * dt player.angleRad -= player.angularVelocity * dt
} }
@ -119,12 +159,24 @@ func run() {
win.SetClosed(true) 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) { if win.Pressed(pixelgl.KeyUp) {
player.xV += player.xV +=
math.Cos(angle-(270*math.Pi)/180) * player.acceleration * dt math.Cos(player.angleRad-(270*math.Pi)/180) * player.acceleration * dt
player.yV += player.yV +=
math.Sin(angle-(270*math.Pi)/180) * player.acceleration * dt math.Sin(player.angleRad-(270*math.Pi)/180) * player.acceleration * dt
if player.xV > player.maxSpeed { if player.xV > player.maxSpeed {
player.xV = player.maxSpeed player.xV = player.maxSpeed
@ -145,22 +197,22 @@ func run() {
} }
if angle == 0 { if player.angleRad == 0 {
angleInDegrees = 0 player.angleDeg = 0
} else { } else {
angleInDegrees = (angle) * 180 / math.Pi player.angleDeg = (player.angleRad) * 180 / math.Pi
} }
if angleInDegrees == 360 || angleInDegrees == -360 { if player.angleDeg == 360 || player.angleDeg == -360 {
angle = 0 player.angleRad = 0
} }
if angleInDegrees > 360 { if player.angleDeg > 360 {
angle = angle - (360*math.Pi)/180 player.angleRad = player.angleRad - (360*math.Pi)/180
} }
if angleInDegrees < -360 { if player.angleDeg < -360 {
angle = angle + (360*math.Pi)/180 player.angleRad = player.angleRad + (360*math.Pi)/180
} }
if player.x >= win.Bounds().Max.X-10 { if player.x >= win.Bounds().Max.X-10 {
@ -177,36 +229,9 @@ func run() {
player.yV = 25 player.yV = 25
} }
player.x += player.xV * dt playerAdjustSpeed(&player, dt)
player.y += player.yV * dt
if player.yV > 0 { playerDrag(&player)
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
}
}
win.Update() win.Update()
@ -218,9 +243,7 @@ func run() {
frames = 0 frames = 0
default: default:
} }
} }
} }
func main() { func main() {