More work

This commit is contained in:
Brian Buller 2016-01-07 18:35:13 -06:00
parent a19baf1c3b
commit 7b97573711
2 changed files with 47 additions and 23 deletions

4
.gitignore vendored
View File

@ -20,4 +20,6 @@ _cgo_export.*
_testmain.go _testmain.go
*.exe *.exe
gopher-battle gopher-battle
gopher-battle.log

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"math"
"math/rand" "math/rand"
"time" "time"
@ -27,7 +28,7 @@ type mainScreen struct {
PauseMenu *termboxUtil.Menu PauseMenu *termboxUtil.Menu
r *rand.Rand r *rand.Rand
animating bool animating bool
gravity float32 gravity float64
} }
func (screen *mainScreen) handleKeyPress(event termbox.Event) int { 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 screen.GameMode == modeRunInput {
if event.Key == termbox.KeySpace { if event.Key == termbox.KeySpace {
if screen.PlayerTurn == 1 { if screen.PlayerTurn == 1 {
screen.Bullet.launchTime = time.Now()
screen.Bullet.angle = screen.Player1.projAngle screen.Bullet.angle = screen.Player1.projAngle
screen.Bullet.speed = screen.Player1.projSpeed 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 { } else {
screen.Bullet.launchTime = time.Now()
screen.Bullet.angle = 180 - screen.Player2.projAngle screen.Bullet.angle = 180 - screen.Player2.projAngle
screen.Bullet.speed = screen.Player2.projSpeed 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.animating = true
screen.GameMode = modeRun screen.GameMode = modeRun
} else if event.Key == termbox.KeyArrowLeft || event.Ch == 'a' { } 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' { } else if event.Key == termbox.KeyArrowUp || event.Ch == 'w' {
if screen.PlayerTurn == 1 { if screen.PlayerTurn == 1 {
// TODO: Upper speed limit
screen.Player1.projSpeed++ screen.Player1.projSpeed++
} else { } else {
// TODO: Upper speed limit
screen.Player2.projSpeed++ screen.Player2.projSpeed++
} }
} else if event.Key == termbox.KeyArrowDown || event.Ch == 's' { } else if event.Key == termbox.KeyArrowDown || event.Ch == 's' {
if screen.PlayerTurn == 1 { if screen.PlayerTurn == 1 {
screen.Player1.projSpeed-- if screen.Player1.projSpeed > 0 {
screen.Player1.projSpeed--
}
} else { } 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) { func (screen *mainScreen) performLayout(style style) {
var bldCity int var bldCity int
if screen.GameMode == modeInit { if screen.GameMode == modeInit {
b := projectile{} // Default screen gravity:
screen.gravity = float64(-9.81)
b := projectile{gravity: screen.gravity}
screen.Bullet = &b screen.Bullet = &b
// TODO: Reset Buildings // TODO: Reset Buildings
//screen.Buildings = []building{} //screen.Buildings = []building{}
@ -165,20 +178,9 @@ func (screen *mainScreen) update() {
if !screen.animating { if !screen.animating {
screen.GameMode = modeRunInput screen.GameMode = modeRunInput
} else { } else {
if screen.Bullet.x > 0 && screen.Bullet.x < ScreenWidth { if screen.Bullet.x > 0 && screen.Bullet.x < ScreenWidth && screen.Bullet.y < ScreenHeight && screen.Bullet.y > -5000 {
// Figure out where the bullet should move to based screen.Bullet.update()
// on its angle and speed // Check for collisions
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--
}
} else { } else {
// Bullet went off the sides of the screen // Bullet went off the sides of the screen
screen.animating = false screen.animating = false
@ -245,9 +247,29 @@ func getSeedFromString(seed string) int64 {
return ret return ret
} }
func degToRad(d int) float64 {
return (float64(d) * (math.Pi / float64(180)))
}
type projectile struct { type projectile struct {
x, y int x, y int
speed, angle 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() { func (p *projectile) draw() {