134 lines
2.5 KiB
Go
134 lines
2.5 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"github.com/nsf/termbox-go"
|
||
|
//"syscall"
|
||
|
)
|
||
|
|
||
|
const programName = "gopher-battle"
|
||
|
|
||
|
var debugMode bool
|
||
|
var debugText string
|
||
|
var gameMode int
|
||
|
|
||
|
// ScreenWidth is how wide the Actual screen is
|
||
|
var ScreenWidth int
|
||
|
|
||
|
// ScreenHeight is how tall the Actual screen is
|
||
|
var ScreenHeight int
|
||
|
|
||
|
func main() {
|
||
|
debugMode = true
|
||
|
var err error
|
||
|
|
||
|
err = termbox.Init()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer termbox.Close()
|
||
|
|
||
|
style := defaultStyle()
|
||
|
termbox.SetOutputMode(termbox.Output256)
|
||
|
ScreenWidth, ScreenHeight = termbox.Size()
|
||
|
mainLoop(style)
|
||
|
}
|
||
|
|
||
|
func readUserInput(e chan termbox.Event) {
|
||
|
for {
|
||
|
e <- termbox.PollEvent()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func sendNoneEvent(e chan termbox.Event) {
|
||
|
for {
|
||
|
time.Sleep(time.Second / 32)
|
||
|
e <- termbox.Event{Type: termbox.EventNone}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// GameState is that.
|
||
|
type GameState struct {
|
||
|
helpText string
|
||
|
}
|
||
|
|
||
|
var state *GameState
|
||
|
|
||
|
func mainLoop(style style) {
|
||
|
// Initialize the Game
|
||
|
if state == nil {
|
||
|
state = new(GameState)
|
||
|
}
|
||
|
screens := defaultScreens()
|
||
|
displayScreen := screens[titleScreenIndex]
|
||
|
layoutAndDrawScreen(displayScreen, style)
|
||
|
eventchan := make(chan termbox.Event)
|
||
|
go readUserInput(eventchan)
|
||
|
go sendNoneEvent(eventchan)
|
||
|
|
||
|
//var fps, fpsTrack, trackTime int
|
||
|
for {
|
||
|
// Read User Input
|
||
|
event := <-eventchan // := termbox.PollEvent()
|
||
|
/*
|
||
|
if getNowTime()-trackTime >= int(time.Second) {
|
||
|
fps = fpsTrack
|
||
|
fpsTrack = 0
|
||
|
trackTime = getNowTime()
|
||
|
}
|
||
|
fpsTrack++
|
||
|
*/
|
||
|
var newScreenIndex int
|
||
|
if event.Type == termbox.EventKey {
|
||
|
if event.Key == termbox.KeyCtrlC {
|
||
|
break
|
||
|
}
|
||
|
newScreenIndex = displayScreen.handleKeyEvent(event)
|
||
|
if newScreenIndex < len(screens) {
|
||
|
displayScreen = screens[newScreenIndex]
|
||
|
} else if newScreenIndex == exitScreenIndex {
|
||
|
break
|
||
|
}
|
||
|
gameMode = newScreenIndex
|
||
|
}
|
||
|
// Update Game State
|
||
|
if gameMode == mainScreenIndex {
|
||
|
// In game updates
|
||
|
displayScreen.update()
|
||
|
}
|
||
|
// Draw Screen
|
||
|
layoutAndDrawScreen(displayScreen, style)
|
||
|
//debugText = fmt.Sprintf("FPS: %d", fps)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Screens Setup
|
||
|
const (
|
||
|
titleScreenIndex = iota
|
||
|
mainScreenIndex
|
||
|
exitScreenIndex
|
||
|
)
|
||
|
|
||
|
func defaultScreens() []Screen {
|
||
|
//var view_port ViewPort
|
||
|
titleScreen := titleScreen{}
|
||
|
mainScreen := mainScreen{}
|
||
|
screens := [...]Screen{
|
||
|
&titleScreen,
|
||
|
&mainScreen,
|
||
|
}
|
||
|
return screens[:]
|
||
|
}
|
||
|
|
||
|
// WriteToLog Writes to the Log
|
||
|
func WriteToLog(d string) {
|
||
|
f, err := os.OpenFile(programName+".log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0664)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
f.WriteString(d)
|
||
|
f.Close()
|
||
|
}
|