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

2
.gitignore vendored
View File

@ -21,3 +21,5 @@ _testmain.go
*.exe
gopher-battle
gopher-battle.log

View File

@ -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() {