gopher-battle/screen_main.go

473 lines
12 KiB
Go
Raw Permalink Normal View History

2016-01-06 18:24:08 +00:00
package main
import (
"fmt"
2016-01-08 00:35:13 +00:00
"math"
2016-01-06 18:24:08 +00:00
"math/rand"
2016-01-09 04:56:46 +00:00
"strconv"
2016-01-06 18:24:08 +00:00
"time"
"gogs.bullercodeworks.com/brian/termbox-util"
"github.com/nsf/termbox-go"
)
const (
modeInit = iota
modeRun
modeRunInput
2016-01-07 04:04:42 +00:00
modePause
2016-01-09 00:04:09 +00:00
modeGameOver
2016-01-06 18:24:08 +00:00
)
type mainScreen struct {
GameMode int
PlayerTurn int
2016-01-07 04:04:42 +00:00
Player1 *player
Player2 *player
Bullet *projectile
2016-01-06 18:24:08 +00:00
Buildings []building
2016-01-07 04:04:42 +00:00
PauseMenu *termboxUtil.Menu
r *rand.Rand
animating bool
2016-01-08 00:35:13 +00:00
gravity float64
2016-01-06 18:24:08 +00:00
}
2016-01-07 04:04:42 +00:00
func (screen *mainScreen) handleKeyPress(event termbox.Event) int {
if event.Key == termbox.KeyEsc {
if screen.GameMode != modePause {
screen.GameMode = modePause
} else {
screen.GameMode = modeRun
}
}
2016-01-08 04:39:51 +00:00
if screen.GameMode == modePause || screen.GameMode == modeGameOver {
screen.PauseMenu.HandleEvent(event)
2016-01-07 04:04:42 +00:00
if screen.PauseMenu.IsDone() {
selOpt := screen.PauseMenu.GetSelectedOption()
if selOpt.GetText() == "Exit" {
return exitScreenIndex
} else if selOpt.GetText() == "Resume" {
screen.GameMode = modeRun
} else if selOpt.GetText() == "Restart" {
2016-01-08 21:44:35 +00:00
screen.GameMode = modeInit
return titleScreenIndex
2016-01-07 04:04:42 +00:00
}
}
}
2016-01-06 18:24:08 +00:00
if screen.GameMode == modeRunInput {
2016-01-08 04:39:51 +00:00
p := screen.Player1
if screen.PlayerTurn == 2 {
p = screen.Player2
}
2016-01-07 04:04:42 +00:00
if event.Key == termbox.KeySpace {
2016-01-08 04:39:51 +00:00
screen.Bullet.init(p)
2016-01-09 04:56:46 +00:00
if p.projSpeed <= 5 {
//screen.Bullet.angle = 270
//screen.Bullet.speed = 8
}
2016-01-07 04:04:42 +00:00
screen.animating = true
screen.GameMode = modeRun
} else if event.Key == termbox.KeyArrowLeft || event.Ch == 'a' {
2016-01-08 21:44:35 +00:00
p.projAngle--
2016-01-07 04:04:42 +00:00
} else if event.Key == termbox.KeyArrowRight || event.Ch == 'd' {
2016-01-08 21:44:35 +00:00
p.projAngle++
2016-01-07 04:04:42 +00:00
} else if event.Key == termbox.KeyArrowUp || event.Ch == 'w' {
2016-01-08 04:39:51 +00:00
p.projSpeed++
2016-01-07 04:04:42 +00:00
} else if event.Key == termbox.KeyArrowDown || event.Ch == 's' {
2016-01-08 04:39:51 +00:00
p.projSpeed--
2016-01-07 04:04:42 +00:00
}
2016-01-06 18:24:08 +00:00
}
return mainScreenIndex
}
func (screen *mainScreen) performLayout(style style) {
var bldCity int
if screen.GameMode == modeInit {
2016-01-08 00:35:13 +00:00
// Default screen gravity:
screen.gravity = float64(-9.81)
b := projectile{gravity: screen.gravity}
2016-01-07 04:04:42 +00:00
screen.Bullet = &b
2016-01-08 21:44:35 +00:00
screen.Buildings = []building{}
2016-01-06 18:24:08 +00:00
// Create a random seed (from time)
seed := fmt.Sprintf("%d", time.Now().UnixNano())
2016-01-07 04:04:42 +00:00
screen.r = rand.New(rand.NewSource(getSeedFromString(seed + "-world")))
2016-01-08 21:44:35 +00:00
screen.PlayerTurn = (screen.r.Int() % 2) + 1
2016-01-06 18:24:08 +00:00
for bldCity < ScreenWidth {
2016-01-07 04:04:42 +00:00
w := screen.r.Intn(8) + 6
h := screen.r.Intn(ScreenHeight-10) + 2
2016-01-06 18:24:08 +00:00
var c termbox.Attribute
2016-01-07 04:04:42 +00:00
switch screen.r.Intn(3) {
2016-01-06 18:24:08 +00:00
case 0:
c = termbox.ColorRed
case 1:
c = termbox.ColorGreen
case 2:
c = termbox.ColorBlue
}
2016-01-09 04:56:46 +00:00
2016-01-07 04:04:42 +00:00
if ScreenWidth-(bldCity+w) < 5 {
w += ScreenWidth - (bldCity + w)
}
2016-01-09 04:56:46 +00:00
b := building{startX: bldCity, width: w, height: h, color: c, windowsAt: screen.r.Intn(3)}
2016-01-06 18:24:08 +00:00
screen.Buildings = append(screen.Buildings, b)
bldCity += w
}
2016-01-07 04:04:42 +00:00
// Player 1 should be on the first 1/3 of the buildings
bld := screen.r.Intn(len(screen.Buildings) / 3)
p1x, p1y := screen.getRndPosForBuilding(bld)
screen.Player1 = createPlayer(p1x, p1y)
2016-01-08 21:44:35 +00:00
screen.Player1.gravity = screen.gravity
2016-01-07 04:04:42 +00:00
// Player 2 should be on the third 1/3 of the buildings
bld = screen.r.Intn(len(screen.Buildings)/3) + ((len(screen.Buildings) * 2) / 3)
p2x, p2y := screen.getRndPosForBuilding(bld)
if ScreenWidth-p2x < 5 {
p2x, p2y = screen.getRndPosForBuilding(bld - 1)
2016-01-06 18:24:08 +00:00
}
2016-01-07 04:04:42 +00:00
screen.Player2 = createPlayer(p2x, p2y)
2016-01-08 21:44:35 +00:00
screen.Player2.projAngle = 225
screen.Player2.gravity = screen.gravity
2016-01-07 04:04:42 +00:00
screen.PauseMenu = termboxUtil.CreateMenu("** GOPHER BATTLE **",
[]string{"Resume", "Restart", "Exit"},
(ScreenWidth/2)-11, (ScreenHeight/2)-3, 22, 6,
style.defaultFg, style.defaultBg)
screen.PauseMenu.SetBordered(true)
2016-01-08 22:06:21 +00:00
screen.PauseMenu.EnableVimMode()
2016-01-07 04:04:42 +00:00
screen.GameMode = modeRun
2016-01-06 18:24:08 +00:00
}
2016-01-07 04:04:42 +00:00
}
func createPlayer(px, py int) *player {
2016-01-08 21:44:35 +00:00
p := player{
x: px,
y: py,
projSpeed: 10,
projAngle: 315,
baseColor: termbox.ColorCyan,
}
2016-01-07 04:04:42 +00:00
return &p
2016-01-06 18:24:08 +00:00
}
// Trigger all updates
func (screen *mainScreen) update() {
if screen.GameMode == modeRun {
2016-01-07 04:04:42 +00:00
if !screen.animating {
2016-01-08 04:39:51 +00:00
if screen.PlayerTurn == 1 {
screen.PlayerTurn = 2
2016-01-08 21:44:35 +00:00
screen.Player1.turn = false
screen.Player2.turn = true
2016-01-08 04:39:51 +00:00
} else {
screen.PlayerTurn = 1
2016-01-08 21:44:35 +00:00
screen.Player1.turn = true
screen.Player2.turn = false
2016-01-08 04:39:51 +00:00
}
2016-01-07 04:04:42 +00:00
screen.GameMode = modeRunInput
} else {
2016-01-09 04:56:46 +00:00
if screen.Bullet.x > 0 && screen.Bullet.x < ScreenWidth && screen.Bullet.y < ScreenHeight {
2016-01-08 00:35:13 +00:00
screen.Bullet.update()
// Check for collisions
2016-01-08 04:39:51 +00:00
if screen.Player1.didCollide(screen.Bullet) {
2016-01-08 21:44:35 +00:00
screen.Player1.dead = true
2016-01-08 04:39:51 +00:00
screen.PauseMenu.SetOptionDisabled(0)
if screen.PlayerTurn == 1 {
// Self kill
screen.PauseMenu.SetTitle("Player 1 LOSES.")
} else {
// Victory!
2016-01-09 00:04:09 +00:00
screen.PauseMenu.SetTitle("Player 2 Wins!")
2016-01-08 04:39:51 +00:00
}
screen.GameMode = modePause
}
if screen.Player2.didCollide(screen.Bullet) {
2016-01-08 21:44:35 +00:00
screen.Player2.dead = true
2016-01-08 04:39:51 +00:00
screen.PauseMenu.SetOptionDisabled(0)
if screen.PlayerTurn == 2 {
// Self kill
screen.PauseMenu.SetTitle("Player 2 LOSES.")
} else {
// Victory!
2016-01-09 00:04:09 +00:00
screen.PauseMenu.SetTitle("Player 1 Wins!")
2016-01-08 04:39:51 +00:00
}
screen.GameMode = modePause
}
for i := range screen.Buildings {
if screen.Buildings[i].didCollide(screen.Bullet) {
// Collision
screen.animating = false
break
}
}
2016-01-07 04:04:42 +00:00
} else {
// Bullet went off the sides of the screen
screen.animating = false
}
}
2016-01-06 18:24:08 +00:00
}
}
func (screen *mainScreen) drawScreen(style style) {
// Draw Landscape
var x int
for i := range screen.Buildings {
screen.Buildings[i].draw(x)
x += screen.Buildings[i].width
}
if screen.GameMode == modeRun {
// Draw Projectile(s)
2016-01-07 04:04:42 +00:00
if screen.Bullet != nil {
screen.Bullet.draw()
}
2016-01-06 18:24:08 +00:00
} else if screen.GameMode == modeRunInput {
// Draw inputs
if screen.PlayerTurn == 1 {
2016-01-09 04:56:46 +00:00
termboxUtil.DrawStringAtPoint(
"Angle: "+strconv.Itoa(screen.Player1.projAngle),
0, 0, style.defaultFg, style.defaultBg)
termboxUtil.DrawStringAtPoint(
"Speed: "+strconv.Itoa(screen.Player1.projSpeed),
0, 1, style.defaultFg, style.defaultBg)
2016-01-07 04:04:42 +00:00
termboxUtil.DrawStringAtPoint("'space' to fire", 0, 2, style.defaultFg, style.defaultBg)
2016-01-06 18:24:08 +00:00
} else {
2016-01-09 04:56:46 +00:00
termboxUtil.DrawStringAtPoint(
"Angle: "+strconv.Itoa(screen.Player2.projAngle),
ScreenWidth-11, 0, style.defaultFg, style.defaultBg)
termboxUtil.DrawStringAtPoint(
"Speed: "+strconv.Itoa(screen.Player2.projSpeed),
ScreenWidth-11, 1, style.defaultFg, style.defaultBg)
2016-01-07 04:04:42 +00:00
termboxUtil.DrawStringAtPoint("'space' to fire", ScreenWidth-15, 2, style.defaultFg, style.defaultBg)
2016-01-06 18:24:08 +00:00
}
}
2016-01-08 04:39:51 +00:00
// Draw Player 1
screen.Player1.draw()
// Draw Player 2
screen.Player2.draw()
2016-01-07 04:04:42 +00:00
if screen.GameMode == modePause {
// Draw the pause menu
screen.PauseMenu.Draw()
}
2016-01-06 18:24:08 +00:00
}
2016-01-07 04:04:42 +00:00
func (screen *mainScreen) getRndPosForBuilding(i int) (int, int) {
retY := ScreenHeight - screen.Buildings[i].height
bldStX := 0
for idx := 0; idx < i; idx++ {
bldStX += screen.Buildings[idx].width
}
retX := screen.r.Intn(screen.Buildings[i].width-5) + bldStX
return retX, retY
2016-01-06 18:24:08 +00:00
}
func getSeedFromString(seed string) int64 {
2016-01-07 04:04:42 +00:00
// Does this algorithm work for string->seed generation?
// ~\_(o.o)_/~ - seems to.
2016-01-06 18:24:08 +00:00
var ret int64
for i, k := range seed {
ret += int64(k) * int64(i)
}
return ret
}
2016-01-08 00:35:13 +00:00
func degToRad(d int) float64 {
return (float64(d) * (math.Pi / float64(180)))
}
2016-01-07 04:04:42 +00:00
type projectile struct {
2016-01-08 00:35:13 +00:00
x, y int
startX, startY int
speed, angle int
gravity float64
launchTime time.Time
2016-01-09 04:56:46 +00:00
movingUp bool
2016-01-08 00:35:13 +00:00
}
2016-01-08 04:39:51 +00:00
func (p *projectile) init(pl *player) {
p.launchTime = time.Now()
p.angle = pl.projAngle
p.speed = pl.projSpeed
2016-01-09 04:56:46 +00:00
if p.speed <= 5 {
p.angle = 270
p.speed = 10
p.startX = pl.x + 2
p.startY = pl.y - 5
} else {
p.startX, p.startY = pl.GetClosestTrajectory()
}
2016-01-08 04:39:51 +00:00
p.x = p.startX
p.y = p.startY
}
2016-01-08 00:35:13 +00:00
func (p *projectile) update() {
tm := time.Since(p.launchTime).Seconds()
useSpeed := float64(p.speed)
useX, useY := float64(p.startX), float64(p.startY)
2016-01-08 21:44:35 +00:00
useA := degToRad(p.angle)
2016-01-09 04:56:46 +00:00
lastY := p.y
2016-01-08 04:39:51 +00:00
p.x = int(useX + (useSpeed * math.Cos(useA) * tm))
p.y = int(useY + (useSpeed*math.Sin(useA)*tm - p.gravity*tm*tm/2))
2016-01-09 04:56:46 +00:00
p.movingUp = p.y < lastY
2016-01-07 04:04:42 +00:00
}
func (p *projectile) draw() {
2016-01-09 00:04:09 +00:00
if p.y < 0 {
2016-01-09 04:56:46 +00:00
if p.movingUp {
termbox.SetCell(p.x, 0, '^', termbox.ColorYellow, termbox.ColorBlack)
} else {
termbox.SetCell(p.x, 0, 'v', termbox.ColorYellow, termbox.ColorBlack)
}
2016-01-09 00:04:09 +00:00
} else {
termbox.SetCell(p.x, p.y, '0', termbox.ColorYellow, termbox.ColorBlack)
}
2016-01-07 04:04:42 +00:00
}
type player struct {
x, y int
projSpeed int
projAngle int
2016-01-08 21:44:35 +00:00
gravity float64
2016-01-07 04:04:42 +00:00
baseColor termbox.Attribute
2016-01-08 21:44:35 +00:00
turn bool
dead bool
2016-01-07 04:04:42 +00:00
}
func (p *player) draw() {
termbox.SetCell(p.x, p.y-4, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+1, p.y-4, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+2, p.y-4, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+3, p.y-4, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+4, p.y-4, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x, p.y-3, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+2, p.y-3, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+4, p.y-3, ' ', p.baseColor, p.baseColor)
2016-01-08 21:44:35 +00:00
if p.dead {
termbox.SetCell(p.x+1, p.y-3, 'X', termbox.ColorRed, termbox.ColorBlack)
termbox.SetCell(p.x+3, p.y-3, 'X', termbox.ColorRed, termbox.ColorBlack)
} else {
termbox.SetCell(p.x+1, p.y-3, '@', termbox.ColorBlack, termbox.ColorWhite)
termbox.SetCell(p.x+3, p.y-3, '@', termbox.ColorBlack, termbox.ColorWhite)
}
2016-01-07 04:04:42 +00:00
termbox.SetCell(p.x, p.y-2, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+1, p.y-2, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+2, p.y-2, 'w', termbox.ColorWhite, p.baseColor)
termbox.SetCell(p.x+3, p.y-2, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+4, p.y-2, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x, p.y-1, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+1, p.y-1, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+2, p.y-1, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+3, p.y-1, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+4, p.y-1, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x, p.y, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+1, p.y, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+2, p.y, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+3, p.y, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+4, p.y, ' ', p.baseColor, p.baseColor)
2016-01-09 04:56:46 +00:00
// Now draw the target
2016-01-08 21:44:35 +00:00
if p.turn {
bulletHandX, bulletHandY := p.GetBulletHandPos()
2016-01-09 04:56:46 +00:00
if p.coordCollide(bulletHandX, bulletHandY) {
termbox.SetCell(bulletHandX, bulletHandY,
'!', termbox.ColorRed|termbox.AttrBold, termbox.ColorBlack)
} else {
termbox.SetCell(bulletHandX, bulletHandY,
'X', termbox.ColorRed|termbox.AttrBold, termbox.ColorBlack)
}
2016-01-08 21:44:35 +00:00
}
2016-01-07 04:04:42 +00:00
}
2016-01-09 04:56:46 +00:00
func (p *player) coordCollide(x, y int) bool {
if x >= p.x && x <= p.x+4 {
return y <= p.y && y >= p.y-4
2016-01-08 04:39:51 +00:00
}
return false
}
2016-01-09 04:56:46 +00:00
func (p *player) didCollide(pr *projectile) bool {
return p.coordCollide(pr.x, pr.y)
}
2016-01-09 00:04:09 +00:00
func (p *player) GetClosestTrajectory() (int, int) {
// Find the spot closest to the player in a direct line with the target
bHx, bHy := p.GetBulletHandPos()
gx, gy := p.x+2, p.y-2
2016-01-09 04:56:46 +00:00
for p.coordCollide(gx, gy) {
2016-01-09 00:04:09 +00:00
if math.Abs(float64(bHx-gx)) > math.Abs(float64(bHy-gy)) {
if bHx > gx {
gx++
} else {
gx--
}
} else {
if bHy > gy {
gy++
} else {
gy--
}
}
}
return gx, gy
}
2016-01-07 04:04:42 +00:00
func (p *player) GetBulletHandPos() (int, int) {
2016-01-08 21:44:35 +00:00
tm := 0.5
useSpeed := float64(p.projSpeed)
useX, useY := float64(p.x+2), float64(p.y-2)
useA := degToRad(p.projAngle)
retX := int(useX + (useSpeed * math.Cos(useA) * tm))
retY := int(useY + (useSpeed*math.Sin(useA)*tm - p.gravity*tm*tm/2))
return retX, retY
2016-01-07 04:04:42 +00:00
}
type building struct {
2016-01-08 04:39:51 +00:00
startX int
2016-01-07 04:04:42 +00:00
width, height int
color termbox.Attribute
2016-01-09 04:56:46 +00:00
windowsAt int
2016-01-07 04:04:42 +00:00
}
func (b *building) draw(x int) {
for i := 0; i < b.height; i++ {
for j := 0; j < b.width; j++ {
c := b.color
2016-01-09 04:56:46 +00:00
if b.width%2 == 1 {
// Individual windows
if i > 0 && i < b.height-1 && j > 0 && j < b.width-1 {
if i%3 == b.windowsAt && j%2 == 1 {
if i%5 == b.windowsAt || j%5 == b.windowsAt || (i+j)%5 == b.windowsAt {
c = termbox.ColorYellow
} else {
c = termbox.ColorBlack
}
}
}
} else {
// Floor windows
if i > 0 && i < b.height-1 && j > 0 && j < b.width-1 {
if i%3 == b.windowsAt {
if i%4 == b.windowsAt {
c = termbox.ColorYellow
} else {
c = termbox.ColorBlack
}
}
2016-01-07 04:04:42 +00:00
}
}
termbox.SetCell(x+j, ScreenHeight-i, ' ', c, c)
}
}
2016-01-06 18:24:08 +00:00
}
2016-01-08 04:39:51 +00:00
func (b *building) didCollide(p *projectile) bool {
if p.x >= b.startX && p.x <= b.startX+b.width {
return p.y >= ScreenHeight-b.height
}
return false
}