rpg/world/two_dimensional.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
use rustc_serialize::json;
use std::io::prelude::*;
use std::fs::File;
/// A single field of the world
#[derive(RustcEncodable, RustcDecodable, Clone)]
pub struct Field {
/// The type of the field
pub field_type: FieldType,
/// The height of the field. Used for collision detection
pub height: i32,
/// The id if the contained entity (optional)
pub contained_entity_id: Option<usize>,
}
impl Field {
/// Creates a new instance of `Field`
pub fn new(field_type: FieldType) -> Field {
Field {
field_type: field_type,
height: 0,
contained_entity_id: None,
}
}
/// A builder method for adding an entity to a field
pub fn contained_entity_id(mut self, entity_id: usize) -> Field {
self.contained_entity_id = Some(entity_id);
self
}
/// A builder method for setting the height of a field
pub fn height(mut self, height: i32) -> Field {
self.height = height;
self
}
}
/// The field type. Used to determine the optical properties of the ground
#[derive(RustcEncodable, RustcDecodable, Clone)]
pub enum FieldType {
/// A field consists of dirt
Dirt,
/// A field consists of grass
Grass,
/// A field a hole in the ground
Hole,
/// A field consists of mud
Mud,
/// A field consists of quicksand
Quicksand,
/// A field consists of sand
Sand,
/// A field consists of stone
Stone,
/// A field is a stone wall
StoneWall,
/// A field consists of swamp water
SwampWater,
/// A field consists of water
Water,
/// A field consists of wood
Wood,
/// A field is a wooded fence
WoodenFence,
}
/// A larger section of a campagne containing a starting point and end point. The starting point
/// is where the character *spawns* and the end point is the point he has to reach for the next
/// level to begin.
#[derive(RustcEncodable, RustcDecodable)]
pub struct Level {
/// The name or title of the level
pub name: String,
/// The entry point of the character
pub starting_point: (usize, usize),
/// The point where the level is finished
pub end_point: (usize, usize),
/// The actual size of the level
size: (usize, usize),
/// The actual fields, the level consists of
data: Vec<Vec<Field>>,
}
impl Level {
/// Creates a new instance of `Level`
pub fn new(name: &str, size: (usize, usize)) -> Level {
let (width, height) = size;
Level {
name: name.to_owned(),
starting_point: (0, 0),
end_point: (0, 0),
size: (width, height),
data: vec![vec![Field::new(FieldType::Grass); height]; width],
}
}
/// A builder method for setting the starting point of the level
pub fn starting_point(mut self, starting_point: (usize, usize)) -> Level {
self.starting_point = starting_point;
self
}
/// A builder method for setting the end point of the level
pub fn end_point(mut self, end_point: (usize, usize)) -> Level {
self.end_point = end_point;
self
}
/// Sets the given field at the given position
pub fn set_field(&mut self, field: Field, position: (usize, usize)) {
let (x, y) = position;
let (width, height) = self.size;
assert!(x < width, "x is out of bounds");
assert!(y < height, "y is out of bounds");
self.data[x][y] = field;
}
}
/// A collection of levels. Usually used to create larger adventures
#[derive(RustcEncodable, RustcDecodable)]
pub struct Campagne {
/// The title of the campagne
pub title: String,
levels: Vec<Level>,
}
impl Campagne {
/// Creates a new instance of `Campagne`
pub fn new(title: &str) -> Campagne {
Campagne {
title: title.to_owned(),
levels: Vec::new(),
}
}
/// Adds a level to the campagne
pub fn add_level(&mut self, level: Level) {
self.levels.push(level);
}
/// Saves the campagne to the specified file
pub fn save_to_file(&self, file_name: &str) {
let mut f = match File::create(file_name) {
Err(_) => return,
Ok(file) => file,
};
let campagne = match json::encode(self) {
Err(_) => return,
Ok(campagne) => campagne,
};
match f.write_all(campagne.as_bytes()) {
Err(_) => {}
Ok(_) => {}
};
}
/// Loads the campagen from the specified file
pub fn load_from_file(file_name: &str) -> Result<Campagne, &str> {
let mut f = match File::open(file_name) {
Err(_) => return Err(file_name),
Ok(file) => file,
};
let mut s = String::new();
match f.read_to_string(&mut s) {
Err(_) => return Err(file_name),
Ok(_) => {}
};
match json::decode(s.as_str()) {
Err(_) => return Err(file_name),
Ok(campagne) => Ok(campagne),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn save_and_load() {
let camp = Campagne::new("Adventure Time!");
camp.save_to_file("test.json");
let new_camp = Campagne::load_from_file("test.json").ok().unwrap();
assert_eq!(camp.title, new_camp.title);
}
#[test]
fn build_campagne() {
let mut camp = Campagne::new("Adventure Time!");
let mut level = Level::new("Hunger Game", (10, 10));
let field = Field::new(FieldType::Stone);
level.set_field(field, (0, 0));
camp.add_level(level);
}
#[test]
fn new_level() {
let mut level = Level::new("Hunger Game", (10, 10));
level = level.starting_point((1, 2)).end_point((3, 4));
assert_eq!(level.size.0, 10);
assert_eq!(level.size.1, 10);
}
#[test]
fn new_field() {
let mut field = Field::new(FieldType::WoodenFence);
field = field.contained_entity_id(23).height(2);
}
}