2022-05-17 23:29:59 -04:00
|
|
|
package simulator
|
|
|
|
|
|
|
|
import (
|
2022-05-24 18:26:34 -04:00
|
|
|
"fmt"
|
2022-05-19 12:58:37 -04:00
|
|
|
"strings"
|
2022-05-17 23:29:59 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Simulator contains the main game state
|
|
|
|
type Simulator struct {
|
|
|
|
Place *Pod
|
|
|
|
Player *Player
|
|
|
|
Px, Py int
|
|
|
|
Time int
|
|
|
|
stop chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewSimulator creates a new simulator instance
|
|
|
|
func NewSimulator() *Simulator {
|
|
|
|
pod := newPod()
|
|
|
|
player := NewPlayer()
|
2022-05-24 21:12:35 -04:00
|
|
|
loadTechs("data/tech.toml")
|
2022-05-19 17:09:46 -04:00
|
|
|
pod.Place(newPlant(itemPlantTea), 4, 4)
|
2022-05-17 23:29:59 -04:00
|
|
|
pod.Tiles[0][0].User = player
|
2022-05-24 18:26:34 -04:00
|
|
|
player.Announce("Game started")
|
2022-05-19 17:09:46 -04:00
|
|
|
return &Simulator{pod, player, 0, 0, 0, make(chan bool)}
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//Start begins the simulation, non blocking
|
|
|
|
func (s *Simulator) Start() {
|
|
|
|
go s.main()
|
|
|
|
}
|
|
|
|
|
|
|
|
//Stop ends the simulation
|
|
|
|
func (s *Simulator) Stop() {
|
|
|
|
s.stop <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
//Input sends a command to the simulator
|
|
|
|
func (s *Simulator) Input(cmd string) {
|
|
|
|
cur := s.Place.Tiles[s.Px][s.Py]
|
2022-05-19 12:58:37 -04:00
|
|
|
cmdS := strings.Split(cmd, " ")
|
|
|
|
switch cmdS[0] {
|
2022-05-17 23:29:59 -04:00
|
|
|
case "get":
|
2022-05-19 17:09:46 -04:00
|
|
|
if cur.Building != nil {
|
|
|
|
if cur.Building.Type() == producerObject {
|
|
|
|
build := cur.Building.(Producer)
|
|
|
|
prod := build.Get()
|
|
|
|
if prod.Kind != 0 && prod.Value > 0 {
|
|
|
|
s.Player.Resources[prod.Kind] = s.Player.Resources[prod.Kind] + prod.Value
|
2022-05-24 18:26:34 -04:00
|
|
|
s.Player.Announce(fmt.Sprintf("Gathered %v %v", prod.Value, Lookup(prod.Kind).Name()))
|
2022-05-19 17:09:46 -04:00
|
|
|
}
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 12:58:37 -04:00
|
|
|
case "place":
|
|
|
|
if len(cmdS) < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item := cmdS[1]
|
2022-05-19 17:09:46 -04:00
|
|
|
if item == itemPlantTea.String() {
|
|
|
|
res := s.Place.Place(newPlant(itemPlantTea), s.Px, s.Py)
|
2022-05-19 12:58:37 -04:00
|
|
|
if res {
|
2022-05-19 17:09:46 -04:00
|
|
|
s.Player.Resources[itemPlantTea] = s.Player.Resources[itemPlantTea] - 1
|
|
|
|
}
|
|
|
|
} else if item == convertPulper.String() {
|
|
|
|
res := s.Place.Place(newConverter(convertPulper, s.Player), s.Px, s.Py)
|
|
|
|
if res {
|
|
|
|
s.Player.Resources[convertPulper] = s.Player.Resources[convertPulper] - 1
|
2022-05-19 12:58:37 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-24 18:26:34 -04:00
|
|
|
case "craft":
|
|
|
|
if len(cmdS) < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item := cmdS[1]
|
|
|
|
if item == convertPulper.String() {
|
|
|
|
if _, ok := s.Player.Techs[techPulper]; ok {
|
|
|
|
if s.Player.Resources[itemPlantTea] > 5 {
|
2022-05-19 12:58:37 -04:00
|
|
|
|
2022-05-24 18:26:34 -04:00
|
|
|
s.Player.Resources[convertPulper] = s.Player.Resources[convertPulper] + 1
|
|
|
|
s.Player.Resources[itemPlantTea] = s.Player.Resources[itemPlantTea] - 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 23:29:59 -04:00
|
|
|
case "left":
|
|
|
|
res := s.Place.MovePlayer(s.Px, s.Py, s.Px, s.Py-1)
|
2022-05-19 15:18:03 -04:00
|
|
|
if res != nil {
|
2022-05-17 23:29:59 -04:00
|
|
|
s.Py = s.Py - 1
|
2022-05-19 15:18:03 -04:00
|
|
|
s.Player.CurrentTile = res
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
case "right":
|
|
|
|
res := s.Place.MovePlayer(s.Px, s.Py, s.Px, s.Py+1)
|
2022-05-19 15:18:03 -04:00
|
|
|
if res != nil {
|
2022-05-17 23:29:59 -04:00
|
|
|
s.Py = s.Py + 1
|
2022-05-19 15:18:03 -04:00
|
|
|
s.Player.CurrentTile = res
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
case "up":
|
|
|
|
res := s.Place.MovePlayer(s.Px, s.Py, s.Px-1, s.Py)
|
2022-05-19 15:18:03 -04:00
|
|
|
if res != nil {
|
2022-05-17 23:29:59 -04:00
|
|
|
s.Px = s.Px - 1
|
2022-05-19 15:18:03 -04:00
|
|
|
s.Player.CurrentTile = res
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
case "down":
|
|
|
|
res := s.Place.MovePlayer(s.Px, s.Py, s.Px+1, s.Py)
|
2022-05-19 15:18:03 -04:00
|
|
|
if res != nil {
|
2022-05-17 23:29:59 -04:00
|
|
|
s.Px = s.Px + 1
|
2022-05-19 15:18:03 -04:00
|
|
|
s.Player.CurrentTile = res
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
case "quit":
|
|
|
|
s.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Simulator) main() {
|
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.stop:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
s.Time = s.Time + 1
|
|
|
|
s.Place.Tick()
|
2022-05-24 18:26:34 -04:00
|
|
|
s.Player.research()
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|