-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorcmark.rs
173 lines (141 loc) · 4.35 KB
/
orcmark.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
use rand::rngs::ThreadRng;
use rand::{self, Rng};
use peacock::graphics::{self, DrawImageParams, Image, Rectangle};
use peacock::input::{self, Key};
use peacock::time;
use peacock::window;
use peacock::{ContextBuilder, Result, State, Vector2f};
type Context = peacock::Context<()>;
const WIDTH: u32 = 1920;
const HEIGHT: u32 = 1080;
const INITIAL_ORCS: usize = 100;
const ORC_SCALE: f32 = 2.0;
const ORC_GRUNT_WIDTH: i32 = 11;
const ORC_GRUNT_HEIGHT: i32 = 16;
const ORC_SHAMAN_WIDTH: i32 = 11;
const ORC_SHAMAN_HEIGHT: i32 = 15;
const GRAVITY: f32 = 0.5;
enum OrcKind {
Grunt,
Shaman,
}
struct Orc {
kind: OrcKind,
position: Vector2f,
velocity: Vector2f,
}
impl Orc {
fn new(rng: &mut ThreadRng) -> Self {
let kind = if rng.gen::<bool>() {
OrcKind::Grunt
} else {
OrcKind::Shaman
};
let velocity = Vector2f::new(rng.gen::<f32>() * 5.0, rng.gen::<f32>() * 5.0 - 2.5);
Self {
kind,
position: Vector2f::ZERO,
velocity,
}
}
}
struct OrcMarkExample {
rng: ThreadRng,
sprite_sheet: Image,
orcs: Vec<Orc>,
spawn_timer: i32,
}
impl OrcMarkExample {
fn new(ctx: &mut Context) -> Result<Self> {
let mut rng = rand::thread_rng();
let sprite_sheet = Image::from_file(ctx, "examples/res/0x72_dungeon_ii.png")?;
let mut orcs = Vec::with_capacity(INITIAL_ORCS);
for _ in 0..INITIAL_ORCS {
orcs.push(Orc::new(&mut rng));
}
Ok(Self {
rng,
sprite_sheet,
orcs,
spawn_timer: 0,
})
}
}
impl State for OrcMarkExample {
type Context = ();
fn update(&mut self, ctx: &mut Context) -> Result<()> {
if self.spawn_timer > 0 {
self.spawn_timer -= 1;
}
if input::is_key_down(ctx, Key::Space) && self.spawn_timer == 0 {
for _ in 0..INITIAL_ORCS {
self.orcs.push(Orc::new(&mut self.rng));
}
self.spawn_timer = 10;
}
for orc in &mut self.orcs {
let (orc_width, orc_height) = match orc.kind {
OrcKind::Grunt => (ORC_GRUNT_WIDTH, ORC_GRUNT_HEIGHT),
OrcKind::Shaman => (ORC_SHAMAN_WIDTH, ORC_SHAMAN_HEIGHT),
};
let max_x = WIDTH as f32 - (orc_width as f32 * ORC_SCALE);
let max_y = HEIGHT as f32 - (orc_height as f32 * ORC_SCALE);
orc.position += orc.velocity;
orc.velocity.y += GRAVITY;
if orc.position.x > max_x {
orc.velocity.x *= -1.0;
orc.position.x = max_x;
} else if orc.position.x < 0.0 {
orc.velocity.x *= -1.0;
orc.position.x = 0.0;
}
if orc.position.y > max_y {
orc.velocity.y *= -0.8;
orc.position.y = max_y;
if self.rng.gen::<bool>() {
orc.velocity.y -= 3.0 + (self.rng.gen::<f32>() * 4.0);
}
} else if orc.position.y < 0.0 {
orc.velocity.y = 0.0;
orc.position.y = 0.0;
}
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context, _dt: f64) -> Result<()> {
for orc in &self.orcs {
let clip_rect = match orc.kind {
OrcKind::Grunt => {
Rectangle::<i32>::new(372, 208, ORC_GRUNT_WIDTH, ORC_GRUNT_HEIGHT)
}
OrcKind::Shaman => {
Rectangle::<i32>::new(372, 241, ORC_SHAMAN_WIDTH, ORC_SHAMAN_HEIGHT)
}
};
graphics::draw(
ctx,
&self.sprite_sheet,
&DrawImageParams {
position: orc.position,
clip_rect: Some(clip_rect),
scale: Some(Vector2f::new(ORC_SCALE, ORC_SCALE)),
..Default::default()
},
)?;
}
window::set_title(
ctx,
&format!(
"OrcMark - {} orcs - {:.0} FPS",
self.orcs.len(),
time::get_fps(ctx)
),
);
Ok(())
}
}
fn main() -> Result<()> {
ContextBuilder::new("OrcMark", WIDTH, HEIGHT)
.build_empty()?
.run_with_result(OrcMarkExample::new)
}