You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
4 years ago
|
package sim
|
||
|
|
||
4 years ago
|
import (
|
||
4 years ago
|
"github.com/jordanorelli/astro-domu/internal/wire"
|
||
4 years ago
|
"github.com/jordanorelli/blammo"
|
||
|
)
|
||
|
|
||
4 years ago
|
// player is a player session in the simulation, eek
|
||
4 years ago
|
type player struct {
|
||
4 years ago
|
*blammo.Log
|
||
4 years ago
|
*room
|
||
4 years ago
|
name string
|
||
|
outbox chan wire.Response
|
||
|
pending []Request
|
||
|
entity *Entity
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
type Move [2]int
|
||
|
|
||
|
func (Move) NetTag() string { return "move" }
|
||
|
|
||
4 years ago
|
func (m *Move) exec(r *room, p *player, seq int) result {
|
||
|
pos := p.entity.Position
|
||
|
target := [2]int{pos[0] + m[0], pos[1] + m[1]}
|
||
|
p.Info("running move for player %s from %v to %v", p.name, *m, target)
|
||
|
if target[0] >= r.width || target[0] < 0 {
|
||
|
return result{reply: wire.Errorf("target cell (%d, %d) is out of bounds", target[0], target[1])}
|
||
|
}
|
||
|
if target[1] >= r.height || target[1] < 0 {
|
||
|
return result{reply: wire.Errorf("target cell (%d, %d) is out of bounds", target[0], target[1])}
|
||
|
}
|
||
|
n := target[1]*r.width + target[0]
|
||
|
if r.tiles[n].here != nil {
|
||
|
return result{reply: wire.Errorf("target cell (%d, %d) is occupied", target[0], target[1])}
|
||
|
}
|
||
4 years ago
|
r.tiles[p.entity.Position[1]*r.width+p.entity.Position[0]].here = nil
|
||
4 years ago
|
p.entity.Position = target
|
||
4 years ago
|
r.tiles[n].here = p.entity
|
||
4 years ago
|
return result{reply: p.entity, announce: p.entity}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
// SpawnPlayer is a request to spawn a player
|
||
|
type SpawnPlayer struct {
|
||
|
Outbox chan wire.Response
|
||
4 years ago
|
Name string
|
||
4 years ago
|
queued bool
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
var lastEntityID = 0
|
||
|
|
||
4 years ago
|
func (s *SpawnPlayer) exec(r *room, _ *player, seq int) result {
|
||
4 years ago
|
if !s.queued {
|
||
4 years ago
|
r.Info("spawn player requested for: %s", s.Name)
|
||
4 years ago
|
|
||
4 years ago
|
if _, ok := r.players[s.Name]; ok {
|
||
|
s.Outbox <- wire.ErrorResponse(seq, "a player is already logged in as %q", s.Name)
|
||
4 years ago
|
return result{}
|
||
|
}
|
||
|
|
||
4 years ago
|
lastEntityID++
|
||
4 years ago
|
p := &player{
|
||
4 years ago
|
Log: r.Log.Child("players").Child(s.Name),
|
||
4 years ago
|
room: r,
|
||
4 years ago
|
name: s.Name,
|
||
4 years ago
|
outbox: s.Outbox,
|
||
|
pending: make([]Request, 0, 32),
|
||
4 years ago
|
entity: &Entity{
|
||
4 years ago
|
ID: lastEntityID,
|
||
4 years ago
|
Position: [2]int{0, 0},
|
||
|
Glyph: '@',
|
||
4 years ago
|
behavior: doNothing{},
|
||
4 years ago
|
},
|
||
4 years ago
|
}
|
||
4 years ago
|
p.pending = append(p.pending, Request{Seq: seq, From: s.Name, Wants: s})
|
||
|
r.players[s.Name] = p
|
||
4 years ago
|
r.tiles[0].here = p.entity
|
||
4 years ago
|
s.queued = true
|
||
|
return result{}
|
||
|
}
|
||
|
|
||
|
return result{
|
||
4 years ago
|
reply: Welcome{
|
||
|
Room: r.name,
|
||
|
Size: [2]int{r.width, r.height},
|
||
|
Contents: r.allEntities(),
|
||
|
},
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
func (SpawnPlayer) NetTag() string { return "player/spawn" }
|
||
|
|
||
|
// PlayerSpawned is an announcement that a player has spawned
|
||
|
type PlayerSpawned struct {
|
||
4 years ago
|
Name string `json:"name"`
|
||
|
Position [2]int `json:"position"`
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func (PlayerSpawned) NetTag() string { return "player/spawned" }
|
||
|
|
||
4 years ago
|
type Welcome struct {
|
||
4 years ago
|
Room string `json:"room"`
|
||
|
Size [2]int `json:"size"`
|
||
|
Contents []Entity `json:"contents"`
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
/*
|
||
|
|
||
|
{
|
||
|
"name": "foyer",
|
||
|
"width": 10,
|
||
|
"height": 10,
|
||
|
"contents": [
|
||
|
[5, 3, 10],
|
||
|
],
|
||
|
"entities": [
|
||
|
[3, "pawn", {"name": "bones"}],
|
||
|
[10, "pawn", {"name": "steve"}]
|
||
|
]
|
||
|
}
|
||
|
|
||
|
*/
|
||
|
|
||
4 years ago
|
func (Welcome) NetTag() string { return "player/welcome" }
|
||
|
|
||
4 years ago
|
func init() {
|
||
|
wire.Register(func() wire.Value { return new(Move) })
|
||
4 years ago
|
wire.Register(func() wire.Value { return new(Welcome) })
|
||
4 years ago
|
// wire.Register(func() wire.Value { return new(pawn) })
|
||
4 years ago
|
}
|