summaryrefslogtreecommitdiff
path: root/src/simpleworld.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2019-09-23 09:18:42 +0200
committertroido <troido@protonmail.com>2019-09-23 09:18:42 +0200
commit62c810b6f8ef7c3feca62637b8361a3386fa43d8 (patch)
tree5b2f8748808bcba23630927f593e64495a147d57 /src/simpleworld.rs
parent864335ea6007dbfebe87ff717aeec8478ca10ec5 (diff)
picking up the project again. Not sure what I changed, but it seemed wise to commit
Diffstat (limited to 'src/simpleworld.rs')
-rw-r--r--src/simpleworld.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/simpleworld.rs b/src/simpleworld.rs
new file mode 100644
index 0000000..1621ee2
--- /dev/null
+++ b/src/simpleworld.rs
@@ -0,0 +1,74 @@
+
+use std::collections::HashMap;
+use std::collections::HashSet;
+
+#[derive(PartialEq, Eq, Hash, Clone, Copy)]
+struct Pos (i32, i32);
+
+
+struct Room {
+ objects :HashMap<Pos, HashSet<GameObject>>,
+ pub area :(Pos, Pos),
+ players :HashMap<String, GameObject>
+}
+
+
+impl Room {
+
+ pub fn new(area :(Pos, Pos)) -> Room {
+ Room {
+ objects: HashMap::new(),
+ area: area,
+ players: HashMap::new()
+ }
+ }
+
+
+ pub fn get_sprites(&self, pos :&Pos) -> Vec<String> {
+ match self.objects.get(pos) {
+ Some(objs) => {objs.iter().map(|o| o.sprite.clone()).collect()}
+ None => {Vec::new()}
+ }
+ }
+
+// let mut sprites :Vec<String> = Vec::new();
+// for maybe_obj in self.objects.get((x, y))
+// sprites.push
+
+
+ pub fn draw(&self) -> (Vec<usize>, Vec<Vec<String>>) {
+ let (minp, maxp) = &self.area;
+ let Pos(xmin, ymin) = *minp;
+ let Pos(xmax, ymax) = *maxp;
+ let width = xmax - xmin;
+ let height = ymax - ymin;
+ let size = width * height;
+ let mut values :Vec<usize>= Vec::with_capacity(size as usize);
+ let mut mapping = Vec::with_capacity(size as usize);
+ for y in ymin..ymax {
+ for x in xmin..xmax {
+ values.push(mapping.len());
+ mapping.push(self.get_sprites(&Pos(x, y)));
+ }
+ }
+ return (values, mapping)
+ }
+}
+
+
+struct GameObject {
+ pos :Pos,
+// name :&str,
+ sprite :String
+}
+
+impl GameObject {
+
+ pub fn new(sprite :&str, pos :Pos) -> GameObject {
+ GameObject {
+ pos: pos,
+ sprite: sprite.to_string()
+ }
+ }
+
+}