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
|
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect};
use ggez::nalgebra::{Point2, Vector2};
use std::f32::consts::PI;
use xml::reader::XmlEvent;
use crate::constants;
use crate::xmlelements::XMLElements;
#[derive(Clone, Debug, PartialEq)]
pub struct Tile {
pub source: Rect,
pub properties: Properties,
}
impl Tile {
pub fn new(source: Rect, properties: Properties) -> Tile {
Tile { source, properties }
}
pub fn draw(&self, spritebatch: &mut SpriteBatch, position: Point2<f32>) {
let draw = match self.properties.visible {
Some(draw) => draw,
None => true,
};
if draw {
spritebatch.add(
DrawParam::default()
.src(self.source)
.rotation(self.properties.rotation)
.offset(Point2::new(0.5, 0.5))
.dest(position)
.scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)),
);
}
}
}
impl Default for Tile {
fn default() -> Tile {
Tile::new(Rect::zero(), Properties::default())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Properties {
pub entity: Option<String>,
pub rotation: f32,
pub keyframe: Option<usize>,
pub delay: Option<usize>,
pub scramble_delay: Option<bool>,
pub spawn: Option<String>,
pub visible: Option<bool>,
}
impl Properties {
pub fn new(properties_elements: Vec<XmlEvent>) -> Properties {
let entity = match XMLElements::get_attribute_value(&properties_elements, "entity") {
Ok(entity) => entity.parse().ok(),
Err(_) => None,
};
let keyframe = match XMLElements::get_attribute_value(&properties_elements, "keyframe") {
Ok(keyframe) => keyframe.parse().ok(),
Err(_) => None,
};
let spawn = XMLElements::get_attribute_value(&properties_elements, "spawn").ok();
let visible = match XMLElements::get_attribute_value(&properties_elements, "visible") {
Ok(visible) => visible.parse().ok(),
Err(_) => None,
};
let delay = match XMLElements::get_attribute_value(&properties_elements, "delay") {
Ok(delay) => delay.parse().ok(),
Err(_) => None,
};
let scramble_delay =
match XMLElements::get_attribute_value(&properties_elements, "scramble_delay") {
Ok(scramble_delay) => scramble_delay.parse().ok(),
Err(_) => None,
};
/*
if scramble_delay {
println!("in");
let mut rng = rand::thread_rng();
let d = delay.unwrap() as f32;
let normal = Normal::new(d, d * 0.50).unwrap();
delay = Some(normal.sample(&mut rng) as usize);
println!("{:?}", delay);
}
*/
Properties {
rotation: 0.0,
entity,
keyframe,
delay,
scramble_delay,
spawn,
visible,
}
}
}
impl Default for Properties {
fn default() -> Properties {
Properties {
rotation: 0.0,
entity: None,
keyframe: None,
delay: None,
scramble_delay: None,
spawn: None,
visible: None,
}
}
}
pub fn convert_angle_to_rad(angle: f32) -> f32 {
angle * (PI / 180.0)
}
pub fn flip(tile: Tile) -> Tile {
let mut t = tile.clone();
t.source.x *= -1.0;
t.source.x -= t.source.w;
t
}
pub fn rotate(tile: Tile, angle: f32) -> Tile {
let mut t = tile.clone();
t.properties.rotation = convert_angle_to_rad(angle);
t
}
|