diff --git a/.gitignore b/.gitignore index 0a9874c..b366e60 100755 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,6 @@ _cgo_export.* _testmain.go *.exe -gopher-battle \ No newline at end of file +gopher-battle + +gopher-battle.log \ No newline at end of file diff --git a/screen_main.go b/screen_main.go index 9ce0e2e..c3bd59f 100644 --- a/screen_main.go +++ b/screen_main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "math" "math/rand" "time" @@ -27,7 +28,7 @@ type mainScreen struct { PauseMenu *termboxUtil.Menu r *rand.Rand animating bool - gravity float32 + gravity float64 } func (screen *mainScreen) handleKeyPress(event termbox.Event) int { @@ -55,14 +56,18 @@ func (screen *mainScreen) handleKeyPress(event termbox.Event) int { if screen.GameMode == modeRunInput { if event.Key == termbox.KeySpace { if screen.PlayerTurn == 1 { + screen.Bullet.launchTime = time.Now() screen.Bullet.angle = screen.Player1.projAngle screen.Bullet.speed = screen.Player1.projSpeed - screen.Bullet.x, screen.Bullet.y = screen.Player1.GetBulletHandPos() + screen.Bullet.startX, screen.Bullet.startY = screen.Player1.GetBulletHandPos() } else { + screen.Bullet.launchTime = time.Now() screen.Bullet.angle = 180 - screen.Player2.projAngle screen.Bullet.speed = screen.Player2.projSpeed - screen.Bullet.x, screen.Bullet.y = screen.Player2.GetBulletHandPos() + screen.Bullet.startX, screen.Bullet.startY = screen.Player2.GetBulletHandPos() } + screen.Bullet.x = screen.Bullet.startX + screen.Bullet.y = screen.Bullet.startY screen.animating = true screen.GameMode = modeRun } else if event.Key == termbox.KeyArrowLeft || event.Ch == 'a' { @@ -87,15 +92,21 @@ func (screen *mainScreen) handleKeyPress(event termbox.Event) int { } } else if event.Key == termbox.KeyArrowUp || event.Ch == 'w' { if screen.PlayerTurn == 1 { + // TODO: Upper speed limit screen.Player1.projSpeed++ } else { + // TODO: Upper speed limit screen.Player2.projSpeed++ } } else if event.Key == termbox.KeyArrowDown || event.Ch == 's' { if screen.PlayerTurn == 1 { - screen.Player1.projSpeed-- + if screen.Player1.projSpeed > 0 { + screen.Player1.projSpeed-- + } } else { - screen.Player2.projSpeed-- + if screen.Player2.projSpeed > 0 { + screen.Player2.projSpeed-- + } } } } @@ -105,7 +116,9 @@ func (screen *mainScreen) handleKeyPress(event termbox.Event) int { func (screen *mainScreen) performLayout(style style) { var bldCity int if screen.GameMode == modeInit { - b := projectile{} + // Default screen gravity: + screen.gravity = float64(-9.81) + b := projectile{gravity: screen.gravity} screen.Bullet = &b // TODO: Reset Buildings //screen.Buildings = []building{} @@ -165,20 +178,9 @@ func (screen *mainScreen) update() { if !screen.animating { screen.GameMode = modeRunInput } else { - if screen.Bullet.x > 0 && screen.Bullet.x < ScreenWidth { - // Figure out where the bullet should move to based - // on its angle and speed - if screen.Bullet.angle < 35 { - screen.Bullet.x++ - } else if screen.Bullet.angle < 90 { - screen.Bullet.x++ - screen.Bullet.y++ - } else if screen.Bullet.angle < 125 { - screen.Bullet.x-- - screen.Bullet.y-- - } else { - screen.Bullet.x-- - } + if screen.Bullet.x > 0 && screen.Bullet.x < ScreenWidth && screen.Bullet.y < ScreenHeight && screen.Bullet.y > -5000 { + screen.Bullet.update() + // Check for collisions } else { // Bullet went off the sides of the screen screen.animating = false @@ -245,9 +247,29 @@ func getSeedFromString(seed string) int64 { return ret } +func degToRad(d int) float64 { + return (float64(d) * (math.Pi / float64(180))) +} + type projectile struct { - x, y int - speed, angle int + x, y int + startX, startY int + speed, angle int + gravity float64 + launchTime time.Time +} + +func (p *projectile) update() { + // On an update we increase the tick + tm := time.Since(p.launchTime).Seconds() + useSpeed := float64(p.speed) + useX, useY := float64(p.startX), float64(p.startY) + halfG := 0.5 * p.gravity + p.x = int(useX + (useSpeed * tm)) + p.y = int(useY + (useSpeed * tm) - (halfG * tm * tm)) + //p.x = int(useX + (math.Cos(degToRad(p.angle)) * useSpeed * tm)) + //p.y = int(useY + (math.Sin(degToRad(p.angle)) * useSpeed * tm * halfG * tm * tm)) + WriteToLog(fmt.Sprintf("(%d) %d,%d (s:%d;a:%d)\n", tm, p.x, p.y, useSpeed, p.angle)) } func (p *projectile) draw() {