diff --git a/idle.go b/idle.go index 9f6f6b7..e14c1b6 100644 --- a/idle.go +++ b/idle.go @@ -130,6 +130,8 @@ func (i *IdleState) maek(c *Connection, args ...string) { case "colony": MakeColony(c, i.System) return + case "shield": + MakeShield(c, i.System) default: c.Printf("I don't know how to make a %v.\n", args[0]) } diff --git a/main.go b/main.go index 058875b..ba999a4 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ var options struct { lightSpeed float64 makeBombTime time.Duration makeColonyTime time.Duration + makeShieldTime time.Duration moneyMean float64 moneySigma float64 playerSpeed float64 @@ -126,4 +127,5 @@ func init() { flag.DurationVar(&options.makeColonyTime, "colony-time", 15*time.Second, "time it takes to make a colony") flag.IntVar(&options.startBombs, "start-bombs", 0, "number of bombs a player has at game start") flag.IntVar(&options.startMoney, "start-money", 1000, "amount of money a player has to start") + flag.DurationVar(&options.makeShieldTime, "shield-time", 15*time.Second, "time it takes to make a shield") } diff --git a/shield.go b/shield.go new file mode 100644 index 0000000..a7268e5 --- /dev/null +++ b/shield.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" +) + +func MakeShield(c *Connection, s *System) { + m := &MakeShieldState{ + System: s, + CommandSuite: CommandSet{ + balCommand, + BroadcastCommand(s), + helpCommand, + NearbyCommand(s), + playersCommand, + }, + } + c.SetState(m) +} + +type MakeShieldState struct { + CommandSuite + *System + start int64 +} + +func (m *MakeShieldState) Enter(c *Connection) { + c.Printf("Making shield on %v...\n", m.System) +} + +func (m *MakeShieldState) Tick(c *Connection, frame int64) ConnectionState { + if m.start == 0 { + m.start = frame + } + if framesToDur(frame-m.start) >= options.makeShieldTime { + return Idle(m.System) + } + return m +} + +func (m *MakeShieldState) Exit(c *Connection) { + c.Printf("Done! System %v is now shielded.\n", m.System) + m.System.Shield = new(Shield) +} + +func (m *MakeShieldState) String() string { + return fmt.Sprintf("Making shield on %v", m.System) +} + +type Shield struct { + energy float64 +} + +func (s *Shield) Tick(frame int64) { + if s.energy < 1000 { + s.energy += (1000 - s.energy) * 0.0005 + } +} + +func (s *Shield) Hit() bool { + if s.energy > 750 { + s.energy -= 750 + return true + } + return false +} + +func (s *Shield) Dead() bool { + return false +} diff --git a/system.go b/system.go index 741668e..89835db 100644 --- a/system.go +++ b/system.go @@ -15,6 +15,7 @@ var ( ) type System struct { + *Shield id int x, y, z float64 planets int @@ -46,6 +47,9 @@ func (s *System) Tick(frame int64) { s.colonizedBy.Deposit(1) s.money -= 1 } + if s.Shield != nil { + s.Shield.Tick(frame) + } } func (s *System) Dead() bool { @@ -166,6 +170,17 @@ func (s *System) Distances() []Ray { } func (s *System) Bombed(bomber *Connection, frame int64) { + if s.Shield != nil { + if s.Shield.Hit() { + s.EachConn(func(conn *Connection) { + conn.Printf("A bomb has hit %v but it was stopped by the system's shield.\n", s) + conn.Printf("Shield remaining: %v.\n", s.energy) + conn.Printf("Shield is recharing....\n") + }) + return + } + } + s.EachConn(func(conn *Connection) { conn.Die(frame) bomber.MadeKill(conn)