@@ -11,16 +11,16 @@ use crate::{
11
11
chat:: { ChatKind , ChatMessage } ,
12
12
chunk_entities:: ChunkEntities ,
13
13
events:: { BlockChangeEvent , EntityCreateEvent , EntityRemoveEvent , PlayerJoinEvent } ,
14
- ChatBox , World ,
14
+ ChatBox , Level ,
15
15
} ;
16
16
17
17
type EntitySpawnCallback = Box < dyn FnMut ( & mut EntityBuilder , & EntityInit ) > ;
18
18
19
19
/// Stores the entire state of a Minecraft game.
20
20
///
21
21
/// This contains:
22
- /// * A [`World`](base::World ) containing chunks and blocks .
23
- /// * An [`Ecs`](ecs::Ecs) containing entities .
22
+ /// * A [`World`](vane::Ecs ) containing entities .
23
+ /// * A [`Level`] containing blocks .
24
24
/// * A [`Resources`](ecs::Resources) containing additional, user-defined data.
25
25
/// * A [`SystemExecutor`] to run systems.
26
26
///
@@ -29,14 +29,9 @@ type EntitySpawnCallback = Box<dyn FnMut(&mut EntityBuilder, &EntityInit)>;
29
29
/// should be preferred over raw interaction with the ECS.
30
30
pub struct Game {
31
31
/// Contains chunks and blocks.
32
- ///
33
- /// NB: use methods on `Game` to update
34
- /// blocks, not direct methods on `World`.
35
- /// The `Game` methods will automatically
36
- /// trigger the necessary `BlockChangeEvent`s.
37
- pub world : World ,
32
+ pub level : Level ,
38
33
/// Contains entities, including players.
39
- pub ecs : Ecs ,
34
+ pub world : Ecs ,
40
35
/// Contains systems.
41
36
pub system_executor : Rc < RefCell < SystemExecutor < Game > > > ,
42
37
@@ -66,8 +61,8 @@ impl Game {
66
61
/// Creates a new, empty `Game`.
67
62
pub fn new ( ) -> Self {
68
63
Self {
69
- world : World :: new ( ) ,
70
- ecs : Ecs :: new ( ) ,
64
+ level : Level :: new ( ) ,
65
+ world : Ecs :: new ( ) ,
71
66
system_executor : Rc :: new ( RefCell :: new ( SystemExecutor :: new ( ) ) ) ,
72
67
resources : Arc :: new ( Resources :: new ( ) ) ,
73
68
chunk_entities : ChunkEntities :: default ( ) ,
@@ -123,7 +118,7 @@ impl Game {
123
118
///
124
119
/// Also triggers necessary events, like `EntitySpawnEvent` and `PlayerJoinEvent`.
125
120
pub fn spawn_entity ( & mut self , mut builder : EntityBuilder ) -> Entity {
126
- let entity = self . ecs . spawn ( builder. build ( ) ) ;
121
+ let entity = self . world . spawn ( builder. build ( ) ) ;
127
122
self . entity_builder = builder;
128
123
129
124
self . trigger_entity_spawn_events ( entity) ;
@@ -140,11 +135,11 @@ impl Game {
140
135
}
141
136
142
137
fn trigger_entity_spawn_events ( & mut self , entity : Entity ) {
143
- self . ecs
138
+ self . world
144
139
. insert_entity_event ( entity, EntityCreateEvent )
145
140
. unwrap ( ) ;
146
- if self . ecs . get :: < Player > ( entity) . is_ok ( ) {
147
- self . ecs
141
+ if self . world . get :: < Player > ( entity) . is_ok ( ) {
142
+ self . world
148
143
. insert_entity_event ( entity, PlayerJoinEvent )
149
144
. unwrap ( ) ;
150
145
}
@@ -153,38 +148,38 @@ impl Game {
153
148
/// Causes the given entity to be removed on the next tick.
154
149
/// In the meantime, triggers `EntityRemoveEvent`.
155
150
pub fn remove_entity ( & mut self , entity : Entity ) -> Result < ( ) , NoSuchEntity > {
156
- self . ecs . defer_despawn ( entity) ;
157
- self . ecs . insert_entity_event ( entity, EntityRemoveEvent )
151
+ self . world . defer_despawn ( entity) ;
152
+ self . world . insert_entity_event ( entity, EntityRemoveEvent )
158
153
}
159
154
160
155
/// Broadcasts a chat message to all entities with
161
156
/// a `ChatBox` component (usually just players).
162
157
pub fn broadcast_chat ( & self , kind : ChatKind , message : impl Into < Text > ) {
163
158
let message = message. into ( ) ;
164
- for ( _, mailbox) in self . ecs . query :: < & mut ChatBox > ( ) . iter ( ) {
159
+ for ( _, mailbox) in self . world . query :: < & mut ChatBox > ( ) . iter ( ) {
165
160
mailbox. send ( ChatMessage :: new ( kind, message. clone ( ) ) ) ;
166
161
}
167
162
}
168
163
169
164
/// Utility method to send a message to an entity.
170
165
pub fn send_message ( & mut self , entity : Entity , message : ChatMessage ) -> SysResult {
171
- let mut mailbox = self . ecs . get_mut :: < ChatBox > ( entity) ?;
166
+ let mut mailbox = self . world . get_mut :: < ChatBox > ( entity) ?;
172
167
mailbox. send ( message) ;
173
168
Ok ( ( ) )
174
169
}
175
170
176
171
/// Gets the block at the given position.
177
172
pub fn block ( & self , pos : BlockPosition ) -> Option < BlockId > {
178
- self . world . block_at ( pos)
173
+ self . level . block_at ( pos)
179
174
}
180
175
181
176
/// Sets the block at the given position.
182
177
///
183
178
/// Triggers necessary `BlockChangeEvent`s.
184
179
pub fn set_block ( & mut self , pos : BlockPosition , block : BlockId ) -> bool {
185
- let was_successful = self . world . set_block_at ( pos, block) ;
180
+ let was_successful = self . level . set_block_at ( pos, block) ;
186
181
if was_successful {
187
- self . ecs . insert_event ( BlockChangeEvent :: single ( pos) ) ;
182
+ self . world . insert_event ( BlockChangeEvent :: single ( pos) ) ;
188
183
}
189
184
was_successful
190
185
}
@@ -198,7 +193,7 @@ impl Game {
198
193
section_y : usize ,
199
194
block : BlockId ,
200
195
) -> bool {
201
- let mut chunk = match self . world . chunk_map ( ) . chunk_at_mut ( chunk_pos) {
196
+ let mut chunk = match self . level . chunk_map ( ) . chunk_at_mut ( chunk_pos) {
202
197
Some ( chunk) => chunk,
203
198
None => return false ,
204
199
} ;
@@ -209,10 +204,11 @@ impl Game {
209
204
return false ;
210
205
}
211
206
212
- self . ecs . insert_event ( BlockChangeEvent :: fill_chunk_section (
213
- chunk_pos,
214
- section_y as u32 ,
215
- ) ) ;
207
+ self . world
208
+ . insert_event ( BlockChangeEvent :: fill_chunk_section (
209
+ chunk_pos,
210
+ section_y as u32 ,
211
+ ) ) ;
216
212
217
213
true
218
214
}
@@ -232,10 +228,10 @@ impl HasResources for Game {
232
228
233
229
impl HasEcs for Game {
234
230
fn ecs ( & self ) -> & Ecs {
235
- & self . ecs
231
+ & self . world
236
232
}
237
233
238
234
fn ecs_mut ( & mut self ) -> & mut Ecs {
239
- & mut self . ecs
235
+ & mut self . world
240
236
}
241
237
}
0 commit comments