Skip to content
Permalink
Browse files

storages refactor

  • Loading branch information...
agmcleod committed Jan 3, 2019
1 parent 611bf48 commit 8cd393d7b55fc419987d3ce4bbbbdbf6dc3e73c0
@@ -2,9 +2,8 @@ use components::{Button, Color, EffectedByPollutionTiles, GathererType, Node, Re
ResearchedBuffs, Sprite, Text, TileType, Transform};
use entities::{create_colored_rect, create_text, tech_tree::Buff};
use renderer;
use specs::{Entities, Entity, WriteStorage};
use specs::{Entities, Entity, LazyUpdate, Read, WriteStorage};
use std::cmp;
use storage_types::TextStorage;

const SPACING_F: f32 = 10.0;
const SPACING: u16 = SPACING_F as u16;
@@ -20,57 +19,36 @@ pub fn create<'a, 'b: 'a>(
y: f32,
selected_tile_type: &TileType,
entities: &'a Entities,
button_storage: &mut WriteStorage<'b, Button>,
color_storage: &mut WriteStorage<'b, Color>,
lazy: &'a Read<LazyUpdate>,
node_storage: &mut WriteStorage<'b, Node>,
rect_storage: &mut WriteStorage<'b, Rect>,
sprite_storage: &mut WriteStorage<'b, Sprite>,
text_storage: &mut WriteStorage<'b, Text>,
transform_storage: &mut WriteStorage<'b, Transform>,
effected_by_pollution_tiles_storage: &mut WriteStorage<'b, EffectedByPollutionTiles>,
researched_buffs: &ResearchedBuffs,
) -> Entity {
) -> (Entity, f32, f32) {
let mut new_entities = Vec::new();

if *selected_tile_type == TileType::Open {
let coal_entity = entities.create();
transform_storage
.insert(
coal_entity,
Transform::visible(SPACING_F, SPACING_F, 1.0, SIZE, SIZE, 0.0, 1.0, 1.0),
)
.unwrap();
button_storage
.insert(
coal_entity,
let coal_entity = lazy.create_entity(entities)
.with(Transform::visible(SPACING_F, SPACING_F, 1.0, SIZE, SIZE, 0.0, 1.0, 1.0))
.with(
Button::new(
"build_coal".to_string(),
[
"mine_button_1.png".to_string(),
"mine_button_2.png".to_string(),
],
),
)
)
.unwrap();
sprite_storage
.insert(
coal_entity,
.with(
Sprite {
frame_name: "mine_button_1.png".to_string(),
},
}
)
.unwrap();
effected_by_pollution_tiles_storage
.insert(coal_entity, EffectedByPollutionTiles::new())
.unwrap();
.with(EffectedByPollutionTiles::new())
.build();

new_entities.push(coal_entity);
let text = create_text::create(
&mut TextStorage {
entities,
color_storage,
text_storage,
transform_storage,
},
entities,
lazy,
format!("${}", GathererType::Coal.get_build_cost()),
16.0,
29.0,
@@ -85,10 +63,8 @@ pub fn create<'a, 'b: 'a>(
}

if researched_buffs.0.contains_key(&Buff::Oil) && *selected_tile_type == TileType::Open {
let oil_entity = entities.create();
transform_storage
.insert(
oil_entity,
let oil_entity = lazy.create_entity(entities)
.with(
Transform::visible(
SPACING_F,
CELL_HEIGHT as f32,
@@ -98,41 +74,38 @@ pub fn create<'a, 'b: 'a>(
0.0,
1.0,
1.0,
),
)
)
.with(
Button::new(
"build_oil".to_string(),
[
"refinery_button_1.png".to_string(),
"refinery_button_2.png".to_string(),
],
)
)
.unwrap();
button_storage
.insert(
oil_entity,
.with(
Button::new(
"build_oil".to_string(),
[
"refinery_button_1.png".to_string(),
"refinery_button_2.png".to_string(),
],
),
)
)
.unwrap();
sprite_storage
.insert(
oil_entity,
.with(
Sprite {
frame_name: "refinery_button_1.png".to_string(),
},
}
)
.unwrap();
effected_by_pollution_tiles_storage
.insert(oil_entity, EffectedByPollutionTiles::new())
.unwrap();
.with(EffectedByPollutionTiles::new())
.build();

new_entities.push(oil_entity);
let text = create_text::create(
&mut TextStorage {
entities,
color_storage,
text_storage,
transform_storage,
},
entities,
lazy,
format!("${}", GathererType::Oil.get_build_cost()),
16.0,
29.0,
@@ -147,37 +120,24 @@ pub fn create<'a, 'b: 'a>(
}

if researched_buffs.0.contains_key(&Buff::Solar) && *selected_tile_type == TileType::Open {
let solar_entity = entities.create();
transform_storage
.insert(
solar_entity,
Transform::visible(CELL_WIDTH as f32, SPACING_F, 0.0, SIZE, SIZE, 0.0, 1.0, 1.0),
)
.unwrap();
button_storage
.insert(
solar_entity,
let solar_entity = lazy.create_entity(&entities)
.with(Transform::visible(CELL_WIDTH as f32, SPACING_F, 0.0, SIZE, SIZE, 0.0, 1.0, 1.0))
.with(
Button::new(
"build_solar".to_string(),
[
"plant_button_1.png".to_string(),
"plant_button_2.png".to_string(),
],
),
)
)
.unwrap();
sprite_storage
.insert(
solar_entity,
.with(
Sprite {
frame_name: "plant_button_2.png".to_string(),
},
}
)
.unwrap();
// technically not needed for solar, but using it here so we can group these buttons
effected_by_pollution_tiles_storage
.insert(solar_entity, EffectedByPollutionTiles::new())
.unwrap();
.with(EffectedByPollutionTiles::new())
.build();

new_entities.push(solar_entity);
let mut cost = GathererType::Solar.get_build_cost();
@@ -188,12 +148,8 @@ pub fn create<'a, 'b: 'a>(
cost -= cost * 20 / 100;
}
let text = create_text::create(
&mut TextStorage {
entities,
color_storage,
text_storage,
transform_storage,
},
entities,
lazy,
format!("${}", cost),
16.0,
102.0,
@@ -208,45 +164,31 @@ pub fn create<'a, 'b: 'a>(
}

if researched_buffs.0.contains_key(&Buff::Hydro) && *selected_tile_type == TileType::River {
let hydro_entity = entities.create();
transform_storage
.insert(
hydro_entity,
Transform::visible(SPACING_F, SPACING_F, 0.0, SIZE, SIZE, 0.0, 1.0, 1.0),
let hydro_entity = lazy.create_entity(&entities)
.with(
Transform::visible(SPACING_F, SPACING_F, 0.0, SIZE, SIZE, 0.0, 1.0, 1.0)
)
.unwrap();
button_storage
.insert(
hydro_entity,
.with(
Button::new(
"build_hydro".to_string(),
[
"hydro_button.png".to_string(),
"hydro_button_2.png".to_string(),
],
),
)
)
.unwrap();
sprite_storage
.insert(
hydro_entity,
.with(
Sprite {
frame_name: "hydro_button.png".to_string(),
},
}
)
.unwrap();
effected_by_pollution_tiles_storage
.insert(hydro_entity, EffectedByPollutionTiles::new())
.unwrap();
.with(EffectedByPollutionTiles::new())
.build();

new_entities.push(hydro_entity);
let text = create_text::create(
&mut TextStorage {
entities,
color_storage,
text_storage,
transform_storage,
},
entities,
lazy,
format!("${}", GathererType::Hydro.get_build_cost()),
16.0,
29.0,
@@ -265,29 +207,27 @@ pub fn create<'a, 'b: 'a>(
let x = cmp::max(
0,
cmp::min(x as i32, dim[0] as i32 - CONTAINER_WIDTH as i32),
);
) as f32;

let y = cmp::max(
0,
cmp::min(y as i32, dim[1] as i32 - CONTAINER_HEIGHT as i32),
);
) as f32;

let container_entity = create_colored_rect::create(
x as f32,
y as f32,
x,
y,
4.0,
CONTAINER_WIDTH,
CONTAINER_HEIGHT,
[0.0, 0.0, 0.0, 0.8],
entities,
transform_storage,
color_storage,
rect_storage,
lazy,
);

let mut node = Node::new();
node.add_many(new_entities);
node_storage.insert(container_entity, node).unwrap();

container_entity
(container_entity, x, y)
}
@@ -1,5 +1,5 @@
use components::{Color, Rect, Transform};
use specs::{Entities, Entity, WriteStorage};
use specs::{Entities, LazyUpdate, Entity, Read};

pub fn create(
x: f32,
@@ -9,17 +9,11 @@ pub fn create(
h: u16,
color: [f32; 4],
entities: &Entities,
transform_storage: &mut WriteStorage<Transform>,
color_storage: &mut WriteStorage<Color>,
rect_storage: &mut WriteStorage<Rect>,
lazy: &Read<LazyUpdate>,
) -> Entity {
let entity = entities.create();

transform_storage
.insert(entity, Transform::visible(x, y, z, w, h, 0.0, 1.0, 1.0))
.unwrap();
color_storage.insert(entity, Color(color)).unwrap();
rect_storage.insert(entity, Rect {}).unwrap();

entity
lazy.create_entity(entities)
.with(Transform::visible(x, y, z, w, h, 0.0, 1.0, 1.0))
.with(Color(color))
.with(Rect{})
.build()
}
@@ -1,10 +1,10 @@
use components::{Color, Text, Transform};
use gfx_glyph::HorizontalAlign;
use specs::Entity;
use storage_types::TextStorage;
use specs::{Entity, Entities, LazyUpdate, Read};

pub fn create(
storages: &mut TextStorage,
entities: &Entities,
lazy: &Read<LazyUpdate>,
text: String,
size: f32,
x: f32,
@@ -20,16 +20,11 @@ pub fn create(
text = text.align(align);
}

let entity = storages.entities.create();
storages
.transform_storage
.insert(
entity.clone(),
Transform::visible(x, y, z, w, h, 0.0, 1.0, 1.0),
lazy.create_entity(entities)
.with(
Transform::visible(x, y, z, w, h, 0.0, 1.0, 1.0)
)
.unwrap();
storages.text_storage.insert(entity.clone(), text).unwrap();
storages.color_storage.insert(entity, color).unwrap();

entity
.with(text)
.with(color)
.build()
}
@@ -59,6 +59,7 @@ impl StateManager {
self.cleanup_state(world);
if let Some(current_state) = self.states.get_mut(&self.current_state) {
current_state.setup(world);
world.maintain();
}
}

@@ -69,6 +70,8 @@ impl StateManager {
.get_mut(&self.current_state)
.unwrap()
.setup(world);

world.maintain();
}

pub fn get_ui_to_render(&mut self) -> Option<&mut Ui> {

0 comments on commit 8cd393d

Please sign in to comment.
You can’t perform that action at this time.