1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
use crate::protocol::{Item, PacketC, PacketS, Tile, ID};
use anyhow::{anyhow, Result};
use glam::IVec2;
use log::info;
use std::collections::{HashMap, VecDeque};
struct TileData {
kind: Tile,
items: Vec<ID>,
active: bool,
progress: f32,
}
struct Player {
name: String,
hand: Option<ID>,
}
#[derive(Default)]
pub struct Game {
item_id_counter: ID,
tiles: HashMap<IVec2, TileData>,
items: HashMap<ID, Item>,
players: HashMap<ID, Player>,
packet_out: VecDeque<PacketC>,
}
impl Game {
pub fn new() -> Self {
let mut g = Self::default();
for x in -5..5 {
for y in -5..5 {
g.tiles
.insert(IVec2 { x, y }, Tile("floor".to_string()).into());
}
}
for x in -5..5 {
g.tiles
.insert(IVec2 { x, y: -5 }, Tile("table".to_string()).into());
g.tiles
.insert(IVec2 { x, y: 4 }, Tile("table".to_string()).into());
}
for y in -5..5 {
g.tiles
.insert(IVec2 { x: -5, y }, Tile("table".to_string()).into());
g.tiles
.insert(IVec2 { x: 4, y }, Tile("table".to_string()).into());
}
g.tiles.extend(
[([-5, 1], "pan"), ([-5, 2], "pan"), ([4, 3], "flour_bag")].map(|(k, v)| {
(
IVec2::from_array(k),
TileData {
active: false,
items: vec![],
kind: Tile(v.to_string()).into(),
progress: 0.,
},
)
}),
);
g
}
pub fn packet_out(&mut self) -> Option<PacketC> {
self.packet_out.pop_front()
}
pub fn prime_client(&self, id: ID) -> Vec<PacketC> {
let mut out = Vec::new();
for (&id, player) in &self.players {
out.push(PacketC::AddPlayer {
id,
name: player.name.clone(),
hand: player.hand.map(|i| (i, self.items[&i].clone())),
})
}
for (&pos, tdata) in &self.tiles {
out.push(PacketC::UpdateMap {
pos,
tile: tdata.kind.clone(),
});
for &id in &tdata.items {
out.push(PacketC::ProduceItem {
id,
pos,
kind: self.items[&id].clone(),
})
}
}
out.push(PacketC::Joined { id });
out
}
pub fn packet_in(&mut self, player: ID, packet: PacketS) -> Result<()> {
match packet {
PacketS::Join { name } => {
self.players.insert(
player,
Player {
hand: None,
name: name.clone(),
},
);
self.packet_out.push_back(PacketC::AddPlayer {
id: player,
name,
hand: None,
});
}
PacketS::Leave => {
let p = self
.players
.remove(&player)
.ok_or(anyhow!("player does not exist"))?;
if let Some(id) = p.hand {
self.items.remove(&id).expect("hand item lost");
}
self.packet_out
.push_back(PacketC::RemovePlayer { id: player })
}
PacketS::Position { pos, rot } => {
self.packet_out
.push_back(PacketC::Position { player, pos, rot });
}
PacketS::Interact { pos, edge } => {
if !edge {
return Ok(());
}
let tile = self
.tiles
.get_mut(&pos)
.ok_or(anyhow!("interacting with empty tile"))?;
let player_data = self
.players
.get_mut(&player)
.ok_or(anyhow!("player does not exist"))?;
if tile.kind.0 == "flour_bag" {
info!("new flour");
let item = Item("flour".to_string());
self.items.insert(self.item_id_counter, item.clone());
tile.items.push(self.item_id_counter);
self.packet_out.push_back(PacketC::ProduceItem {
id: self.item_id_counter,
pos,
kind: item,
});
self.item_id_counter += 1;
}
if let Some(item) = player_data.hand.take() {
info!("put {item}");
tile.items.push(item);
self.packet_out.push_back(PacketC::PutItem { item, pos })
} else {
if let Some(item) = tile.items.pop() {
info!("take {item}");
player_data.hand = Some(item);
self.packet_out
.push_back(PacketC::TakeItem { item, player })
}
}
}
}
Ok(())
}
}
impl From<Tile> for TileData {
fn from(kind: Tile) -> Self {
Self {
kind,
progress: 0.,
active: false,
items: vec![],
}
}
}
|