Skip to main content

gd_levels/
objects.rs

1use std::collections::HashMap;
2use crate::property_ids::{ObjectCommonProperties, PropertyID};
3use crate::property_types::PropertyValueType;
4
5pub struct Object {
6    properties: HashMap<i32, String>
7}
8
9impl Object {
10    pub fn new(object_id: i32, x: i32, y: i32) -> Self {
11        let mut obj = Self { properties: HashMap::new() };
12
13        obj.set_property(ObjectCommonProperties::Id, object_id);
14        obj.set_property(ObjectCommonProperties::X, x);
15        obj.set_property(ObjectCommonProperties::Y, y);
16
17        obj
18    }
19
20    pub fn to_object_string(&self) -> String {
21        let mut obj_string = String::new();
22
23        for prop in self.properties.iter() {
24            if !obj_string.is_empty() {
25                obj_string.push_str(",");
26            }
27
28            obj_string.push_str(prop.0.to_string().as_str());
29            obj_string.push_str(",");
30            obj_string.push_str(prop.1);
31
32        }
33
34        obj_string
35    }
36
37    pub fn set_property(&mut self, id: impl PropertyID, value: impl PropertyValueType) {
38        self.properties.insert(id.to_property_id(), value.to_object_string());
39    }
40
41    // Removes a property by id, returns the value or none
42    pub fn remove_property(&mut self, id: impl PropertyID) -> Option<String> {
43        self.properties.remove(&id.to_property_id())
44    }
45}