Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/crops/ambient.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,30 @@ name = "MediumCrop"
attributes = ["Debuggable"]
description = "A tag for instantiated medium crop entities."

[components."crops::medium_crop_occupant"]
type = "EntityId"
[components."crops::medium_crop_occupants"]
type = { type = "Vec", element_type = "EntityId" }
name = "MediumCropOccupant"
attributes = ["Debuggable"]
description = "A reference to the medium crop that occupies this tile. Can be null for no occupant."
description = """
A list of the medium crops occupying this chunk.
Each element may be null if there is no occupant.
"""

[components."crops::last_medium_crop_occupants"]
type = { type = "Vec", element_type = "EntityId" }
name = "MediumCropOccupant"
attributes = ["Debuggable"]
description = """
A list of the medium crops occupying this chunk during the last network update.
This is diffed against medium_crop_occupants to get new network updates.
"""

[components."crops::class"]
type = "EntityId"
name = "Class"
attributes = ["Debuggable"]
description = "A reference to the crop class that this crop is an instance of."

[components."crops::on_tile"]
type = "EntityId"
name = "OnTile"
attributes = ["Debuggable"]
description = "A reference to the tile that this crop is on."

[messages.update_medium_crops]
name = "UpdateMediumCrops"
description = "Server-to-client batch update for the medium crops in a chunk."
Expand Down
47 changes: 31 additions & 16 deletions src/crops/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ambient_api::{
components::core::{
prefab::prefab_from_url,
primitives::cube,
transform::{local_to_parent, local_to_world, rotation, translation},
},
concepts::make_transformable,
Expand All @@ -10,7 +9,7 @@ use ambient_api::{

use components::{
crops::*,
map::{chunk_tile_index, chunk_tile_refs, in_chunk, position},
map::{chunk_tile_index, in_chunk, position},
terrain::height,
};
use flowerpot::CHUNK_SIZE;
Expand All @@ -20,6 +19,14 @@ mod shared;

#[main]
fn main() {
spawn_query(components::map::chunk()).bind(move |entities| {
for (e, _) in entities {
let tile_num = CHUNK_SIZE * CHUNK_SIZE;
let occupants = vec![EntityId::null(); tile_num];
entity::add_component(e, medium_crop_occupants(), occupants.clone());
}
});

let chunks = flowerpot::init_map(components::map::chunk());

UpdateMediumCrops::subscribe({
Expand All @@ -34,27 +41,33 @@ fn main() {
break Some(chunk);
}
} else {
eprintln!(
"warning: dropping medium crop update on unloaded chunk {}",
data.chunk
);

break None;
}

sleep(0.1).await;
};

let Some(chunk) = chunk else { return };
let Some(tiles) = entity::get_component(chunk, chunk_tile_refs()) else { return };
let Some(mut occupants) = entity::get_component(chunk, medium_crop_occupants()) else { return };

eprintln!("update: {:#?}", data.crop_tiles);

for (tile_idx, class) in data
.crop_tiles
.into_iter()
.zip(data.crop_classes.into_iter())
{
let tile = tiles[tile_idx as usize];
eprintln!("updating {} with {:?}", tile_idx, class);

if let Some(old_occupant) = entity::get_component(tile, medium_crop_occupant())
{
if !old_occupant.is_null() {
entity::despawn_recursive(old_occupant);
}
let occupant = &mut occupants[tile_idx as usize];

if !occupant.is_null() {
entity::despawn_recursive(*occupant);
}

if class.is_null() {
Expand All @@ -68,15 +81,15 @@ fn main() {
)
+ 0.5;

let new_occupant = entity::get_all_components(class)
*occupant = entity::get_all_components(class)
.with_default(medium_crop())
.with(position(), occupant_position)
.with(in_chunk(), chunk)
.with(chunk_tile_index(), tile_idx)
.spawn();

entity::add_component(tile, medium_crop_occupant(), new_occupant);
}

entity::set_component(chunk, medium_crop_occupants(), occupants);
});
}
});
Expand All @@ -102,10 +115,12 @@ fn main() {
}
});

despawn_query(medium_crop_occupant()).bind(move |entities| {
for (_, occupant) in entities {
if !occupant.is_null() {
entity::despawn_recursive(occupant);
despawn_query(medium_crop_occupants()).bind(move |entities| {
for (_, occupants) in entities {
for occupant in occupants {
if !occupant.is_null() {
entity::despawn_recursive(occupant);
}
}
}
});
Expand Down
Loading