Move setting Atom::loc into get_atom_list

This commit is contained in:
Tad Hardesty 2019-11-17 10:35:26 -08:00
parent 4df38245e7
commit 27323eddfa
4 changed files with 32 additions and 29 deletions

View file

@ -181,7 +181,7 @@ pub fn get_atom_list<'a>(
}
// look up the type
let atom = match Atom::from_prefab(objtree, fab, loc) {
let atom = match Atom::from_prefab(objtree, fab) {
Some(x) => x,
None => {
if let Some(errors) = errors {
@ -210,6 +210,10 @@ pub fn get_atom_list<'a>(
result.push(atom);
}
for atom in result.iter_mut() {
atom.loc = loc;
}
result
}
@ -225,33 +229,15 @@ pub struct Atom<'a> {
}
impl<'a> Atom<'a> {
pub fn from_prefab(objtree: &'a ObjectTree, fab: &'a Prefab, loc: (u32, u32)) -> Option<Self> {
pub fn from_prefab(objtree: &'a ObjectTree, fab: &'a Prefab) -> Option<Self> {
objtree.find(&fab.path).map(|type_| Atom {
type_: type_.get(),
prefab: Some(&fab.vars),
sprite: Sprite::default(),
loc,
loc: (0, 0),
})
}
pub fn from_type(objtree: &'a ObjectTree, path: &str, loc: (u32, u32)) -> Option<Self> {
objtree.find(path).map(|type_| Atom {
type_: type_.get(),
prefab: None,
sprite: Sprite::default(),
loc,
})
}
pub fn from_type_ref(type_: &'a Type, loc: (u32, u32)) -> Self {
Atom {
type_: type_,
prefab: None,
sprite: Sprite::default(),
loc,
}
}
pub fn path(&self) -> &str {
&self.type_.path
}
@ -261,6 +247,23 @@ impl<'a> Atom<'a> {
}
}
impl<'a> From<&'a Type> for Atom<'a> {
fn from(type_: &'a Type) -> Self {
Atom {
type_: type_,
prefab: None,
sprite: Sprite::default(),
loc: (0, 0),
}
}
}
impl<'a> From<TypeRef<'a>> for Atom<'a> {
fn from(type_ref: TypeRef<'a>) -> Self {
Atom::from(type_ref.get())
}
}
// ----------------------------------------------------------------------------
// Vars abstraction

View file

@ -132,7 +132,7 @@ impl RenderPass for HideSpace {
output: &mut Vec<Atom<'a>>,
) -> bool {
if atom.istype("/turf/template_noop/") {
output.push(Atom::from_type(objtree, "/turf/open/space", atom.loc).unwrap());
output.push(Atom::from(objtree.expect("/turf/open/space")));
false
} else {
true

View file

@ -21,8 +21,8 @@ impl RenderPass for Random {
machines.push(child.get());
}
}
if let Some(replacement) = machines.choose(&mut rng) {
output.push(Atom::from_type_ref(replacement, atom.loc));
if let Some(&replacement) = machines.choose(&mut rng) {
output.push(Atom::from(replacement));
return false; // consumed
}
}
@ -34,8 +34,8 @@ impl RenderPass for Random {
machines.push(child.get());
}
}
if let Some(replacement) = machines.choose(&mut rng) {
output.push(Atom::from_type_ref(replacement, atom.loc));
if let Some(&replacement) = machines.choose(&mut rng) {
output.push(Atom::from(replacement));
return false; // consumed
}
}
@ -47,8 +47,8 @@ impl RenderPass for Random {
sheets.push(child.get());
}
}
if let Some(replacement) = sheets.choose(&mut rng) {
output.push(Atom::from_type_ref(replacement, atom.loc));
if let Some(&replacement) = sheets.choose(&mut rng) {
output.push(Atom::from(replacement));
return false; // consumed
}
}

View file

@ -28,7 +28,7 @@ impl RenderPass for Spawners {
},
_ => continue,
};
output.push(Atom::from_type(objtree, reference, atom.loc).unwrap());
output.push(Atom::from(objtree.expect(reference)));
}
false // don't include the original atom
}