Add a world representation

Description

Now that we can render some blocks in different locations, lets try to build a small world.

This change adds a new World struct that provides an iterator for the blocks in the world. Right now it just produces a flat world that's 20 blocks width in the X direction and 20 blocks deep in the Z direction.

The X and Z axes start at position 0 and extend in the positive direction. By default our view of the world is from the origin, so it should look like we're at the corner. We also set the Y value of the single layer of blocks to -2 so it appears that we're standing on top of the world.

Screenshot

Commands

git clone git@github.com:atsheehan/iridium
cd iridium
git checkout 7ddf81affbbda8065eccf2466378f158acca25bf
cargo run --release

Code Changes

Modified src/main.rsGitHub

@@ -1,13 +1,14 @@
11 mod math;
22 mod render;
3+ mod world;
34
4- use math::Vec3;
55 use render::Renderer;
66 use winit::{
77 event::{ElementState, Event, KeyEvent, WindowEvent},
88 event_loop::EventLoop,
99 keyboard::{KeyCode, PhysicalKey},
1010 };
11+ use world::World;
1112
1213 fn main() {
1314 let options = get_options();
@@ -15,6 +16,8 @@
1516 let event_loop = EventLoop::new().unwrap();
1617 let mut renderer = Renderer::new(&event_loop, options.windowed);
1718
19+ let world = World::new(20, 20);
20+
1821 event_loop
1922 .run(move |event, window_target| match event {
2023 Event::WindowEvent {
@@ -50,10 +53,11 @@
5053 } if window_id == renderer.window_id() => {
5154 renderer.set_viewport();
5255 renderer.clear();
53- renderer.draw_cube(&Vec3(2.0, 3.0, 6.0));
54- renderer.draw_cube(&Vec3(-4.0, 0.0, 6.0));
55- renderer.draw_cube(&Vec3(5.0, 10.0, 30.0));
56- renderer.draw_cube(&Vec3(20.0, -23.0, 30.0));
56+
57+ for position in world.block_positions() {
58+ renderer.draw_cube(&position);
59+ }
60+
5761 renderer.present();
5862 }
5963 _ => (),
@@ -1,13 +1,14 @@
1 mod math;
2 mod render;
 
3
4- use math::Vec3;
5 use render::Renderer;
6 use winit::{
7 event::{ElementState, Event, KeyEvent, WindowEvent},
8 event_loop::EventLoop,
9 keyboard::{KeyCode, PhysicalKey},
10 };
 
11
12 fn main() {
13 let options = get_options();
@@ -15,6 +16,8 @@
15 let event_loop = EventLoop::new().unwrap();
16 let mut renderer = Renderer::new(&event_loop, options.windowed);
17
 
 
18 event_loop
19 .run(move |event, window_target| match event {
20 Event::WindowEvent {
@@ -50,10 +53,11 @@
50 } if window_id == renderer.window_id() => {
51 renderer.set_viewport();
52 renderer.clear();
53- renderer.draw_cube(&Vec3(2.0, 3.0, 6.0));
54- renderer.draw_cube(&Vec3(-4.0, 0.0, 6.0));
55- renderer.draw_cube(&Vec3(5.0, 10.0, 30.0));
56- renderer.draw_cube(&Vec3(20.0, -23.0, 30.0));
 
57 renderer.present();
58 }
59 _ => (),
@@ -1,13 +1,14 @@
1 mod math;
2 mod render;
3+ mod world;
4
 
5 use render::Renderer;
6 use winit::{
7 event::{ElementState, Event, KeyEvent, WindowEvent},
8 event_loop::EventLoop,
9 keyboard::{KeyCode, PhysicalKey},
10 };
11+ use world::World;
12
13 fn main() {
14 let options = get_options();
@@ -15,6 +16,8 @@
16 let event_loop = EventLoop::new().unwrap();
17 let mut renderer = Renderer::new(&event_loop, options.windowed);
18
19+ let world = World::new(20, 20);
20+
21 event_loop
22 .run(move |event, window_target| match event {
23 Event::WindowEvent {
@@ -50,10 +53,11 @@
53 } if window_id == renderer.window_id() => {
54 renderer.set_viewport();
55 renderer.clear();
56+
57+ for position in world.block_positions() {
58+ renderer.draw_cube(&position);
59+ }
60+
61 renderer.present();
62 }
63 _ => (),

Added src/world.rsGitHub

1+ use crate::math::Vec3;
2+
3+ pub(crate) struct World {
4+ x_width: u32,
5+ z_depth: u32,
6+ }
7+
8+ impl World {
9+ pub(crate) fn new(x_width: u32, z_depth: u32) -> Self {
10+ Self { x_width, z_depth }
11+ }
12+
13+ pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> {
14+ let x_start = 0;
15+ let x_end = self.x_width;
16+ let z_start = 0;
17+ let z_end = self.z_depth;
18+
19+ (x_start..x_end)
20+ .flat_map(move |x| (z_start..z_end).map(move |z| Vec3(x as f32, -2.0, z as f32)))
21+ }
22+ }
1+ use crate::math::Vec3;
2+
3+ pub(crate) struct World {
4+ x_width: u32,
5+ z_depth: u32,
6+ }
7+
8+ impl World {
9+ pub(crate) fn new(x_width: u32, z_depth: u32) -> Self {
10+ Self { x_width, z_depth }
11+ }
12+
13+ pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> {
14+ let x_start = 0;
15+ let x_end = self.x_width;
16+ let z_start = 0;
17+ let z_end = self.z_depth;
18+
19+ (x_start..x_end)
20+ .flat_map(move |x| (z_start..z_end).map(move |z| Vec3(x as f32, -2.0, z as f32)))
21+ }
22+ }