Skip to content

Commit

Permalink
spawn agent
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMascolo committed May 15, 2024
1 parent b8a69b6 commit d4f0d54
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 133 deletions.
32 changes: 16 additions & 16 deletions gis/maps/map.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"geometry": {
"coordinates": [
[
0.2717266103028919,
23.719256114054062
5.0,
25.0
],
[
28.912889334367378,
24.005122774762825
25.0,
25.0
]
],
"type": "LineString"
Expand All @@ -24,12 +24,12 @@
"geometry": {
"coordinates": [
[
28.33285787739456,
23.989100126953318
25.0,
25.0
],
[
29.23641715476103,
0.1329339011088848
25.0,
5.0
]
],
"type": "LineString"
Expand All @@ -41,12 +41,12 @@
"geometry": {
"coordinates": [
[
29.10866173200887,
0.36682206957736696
25.0,
5.0
],
[
3.3976994887509875,
0.3714554190081145
5.0,
5.0
]
],
"type": "LineString"
Expand All @@ -58,12 +58,12 @@
"geometry": {
"coordinates": [
[
3.67130675551266,
0.3712397532143825
5.0,
5.0
],
[
1.38106762594918564,
23.759071404593442
5.0,
25.0
]
],
"type": "LineString"
Expand Down
2 changes: 1 addition & 1 deletion gis/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
// Initialize the simulation and its visualization here.

let num_agents = 0;
let dim: (f32, f32) = (50., 50.);
let dim: (f32, f32) = (30., 30.);

let state = Map::new(dim, num_agents);
Visualization::default()
Expand Down
39 changes: 16 additions & 23 deletions gis/src/model/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Map {
pub gis_field: DenseNumberGrid2D<i32>,
pub dim: (f32, f32),
pub num_agents: u32,
pub people: Vec<Person>,
}

impl Map {
Expand All @@ -24,14 +25,14 @@ impl Map {
gis_field: DenseNumberGrid2D::new(dim.0 as i32, dim.1 as i32),
dim,
num_agents,
people: Vec::new(),
}
}
}

impl State for Map {
fn update(&mut self, _step: u64) {
self.field.lazy_update();
println!("{:?}", self.gis_field.locs.is_empty());
}

fn reset(&mut self) {
Expand Down Expand Up @@ -63,11 +64,22 @@ impl State for Map {
self.step += 1
}

fn set_gis(&mut self, vec: Vec<i32>) {
let mut done = true;
fn set_gis(&mut self, vec: Vec<i32>, schedule: &mut Schedule) {
let last_d = Real2D { x: 5., y: 5. };
let loc = Real2D { x: 5., y: 5. };
let agent = Person {
id: 0 as u32,
loc,
last_d,
dir_x: 1.0,
dir_y: 1.0,
};
self.people.push(agent.clone());
self.field.set_object_location(agent, loc);
schedule.schedule_repeating(Box::new(agent), 0., 0);

for (index, i) in vec.iter().enumerate() {
let x = index as f32 / 30 as f32;
let x = (index as f32 / 30 as f32).floor();
let y = index as f32 % 30 as f32;
self.gis_field.set_value_location(
*i,
Expand All @@ -76,25 +88,6 @@ impl State for Map {
y: y as i32,
},
);

if *i == 1 {
println!("{:?} {:?}", x, y);
}

if *i == 1 && done {
let last_d = Real2D { x, y };
let loc = Real2D { x, y };
let agent = Person {
id: 0 as u32,
loc,
last_d,
dir_x: 1.0,
dir_y: 1.0,
};
// Put the agent in your state
self.field.set_object_location(agent, loc);
done = false;
}
}
}
}
95 changes: 13 additions & 82 deletions gis/src/model/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,95 +18,26 @@ pub struct Person {
pub dir_y: f32,
}

impl Person {
#[allow(dead_code)]
pub fn as_agent(self) -> Box<dyn Agent> {
Box::new(self)
}
}

impl Agent for Person {
/// Put the code that should happen for each step, for each agent here.
fn step(&mut self, state: &mut dyn State) {
let state = state.as_any().downcast_ref::<Map>().unwrap();
let gis_field = &state.gis_field;
let field = &state.field;
let last_d = self.last_d;
/* let mut rng = rand::thread_rng();
let generated = rng.gen_range(0..3);
let next_x = last_d.x as i32 + 1;
let next_y = last_d.y as i32 + 1;
let prev_x = last_d.x as i32 - 1;
let prev_y = last_d.y as i32 - 1;

match generated {
0 => {
//in this case we do a simple check for the next cell in x-axis
if gis_field
.get_value(&Int2D {
x: next_x,
y: last_d.y,
})
.eq(&Some(0))
{
field.set_object_location(
curr_agent,
Real2D {
x: next_x as f32,
y: last_d.y as f32,
},
);
}
}
1 => {
//in this case we do a simple check for the next cell in y-axis
if gis_field
.get_value(&Int2D {
x: next_y,
y: last_d.x,
})
.eq(&Some(0))
{
field.set_object_location(
curr_agent,
Real2D {
x: next_y as f32,
y: last_d.x as f32,
},
);
}
}
2 => {
//in this case we do a simple check for the prev cell in x-axis
if gis_field
.get_value(&Int2D {
x: prev_x,
y: last_d.y,
})
.eq(&Some(0))
{
field.set_object_location(
curr_agent,
Real2D {
x: prev_x as f32,
y: last_d.y as f32,
},
);
}
}
3 => {
//in this case we do a simple check for the prev cell in y-axis
if gis_field
.get_value(&Int2D {
x: prev_y,
y: last_d.x,
})
.eq(&Some(0))
{
field.set_object_location(
curr_agent,
Real2D {
x: prev_y as f32,
y: last_d.x as f32,
},
);
}
}
_ => todo!(),
} */
//check dei vicini e movimento in base alla priorità

/* self.loc.x += 1.;
self.loc.y += 1.;
state.field.set_object_location(*self, self.loc); */
}

/// Put the code that decides if an agent should be removed or not
Expand Down
40 changes: 33 additions & 7 deletions gis/src/visualization/map_vis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use krabmaga::bevy::ecs as bevy_ecs;
use krabmaga::bevy::ecs::system::Resource;
use krabmaga::bevy::prelude::Commands;
use krabmaga::engine::agent::Agent;
use krabmaga::engine::location::Int2D;
use krabmaga::engine::location::{Int2D, Real2D};
use krabmaga::engine::schedule::Schedule;
use krabmaga::engine::state::State;
use krabmaga::visualization::agent_render::AgentRender;
use krabmaga::visualization::agent_render::{AgentRender, SpriteType};
use krabmaga::visualization::asset_handle_factory::AssetHandleFactoryResource;
use krabmaga::visualization::simulation_descriptor::SimulationDescriptor;
use krabmaga::visualization::visualization_state::VisualizationState;
Expand Down Expand Up @@ -39,22 +39,48 @@ impl VisualizationState<Map> for MapVis {
}))
}

fn before_render(
&mut self,
state: &mut Map,
_schedule: &Schedule,
commands: &mut Commands,
asset_factory: &mut AssetHandleFactoryResource,
) {
// let new_sheep = state.new_sheep.lock().unwrap();
// for sheep in &*new_sheep {
for person in &state.people {
//let boxed_agent = &(*sheep).as_agent();
let boxed_agent = &person.as_agent();
let boxed_state = Box::new(state.as_state());
let person_vis = self.get_agent_render(boxed_agent, state);
let SpriteType::Emoji(emoji_code) =
person_vis.unwrap().sprite(boxed_agent, &boxed_state);
let sprite_render = asset_factory.get_emoji_loader(emoji_code);
self.setup_agent_graphics(
boxed_agent,
self.get_agent_render(boxed_agent, state).unwrap(),
sprite_render,
commands,
&boxed_state,
);
}
}

fn get_agent(
&self,
agent_render: &Box<dyn AgentRender>,
state: &Box<&dyn State>,
) -> Option<Box<dyn Agent>> {
let state = state.as_any().downcast_ref::<Map>().unwrap();
/* match state.field(&Person {
match state.field.get(&Person {
id: agent_render.get_id(),
loc: Int2D { x: 0, y: 0 },
last_d: Int2D { x: 0, y: 0 },
loc: Real2D { x: 0., y: 0. },
last_d: Real2D { x: 0., y: 0. },
dir_x: 0.,
dir_y: 0.,
}) {
Some(matching_agent) => Some(Box::new(*matching_agent)),
None => None,
} */
None
}
}
}
7 changes: 3 additions & 4 deletions gis/src/visualization/person_vis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ impl AgentRender for PersonVis {
fn location(&self, agent: &Box<dyn Agent>, state: &Box<&dyn State>) -> (f32, f32, f32) {
let state = state.as_any().downcast_ref::<Map>().unwrap();
let agent = agent.downcast_ref::<Person>().unwrap();
//let loc = state.field.get_location(*agent);
/* match loc {
let loc = state.field.get_location(*agent);
match loc {
Some(loc) => (loc.x, loc.y, 0.),
None => (agent.loc.x, agent.loc.y, 0.),
} */
(0., 0., 0.)
}
}

/// Specify how much the texture should be scaled by. A common scale is (0.1, 0.1).
Expand Down

0 comments on commit d4f0d54

Please sign in to comment.