Created
May 3, 2020 15:18
-
-
Save Bert-Proesmans/dfe1cee5b9ec28d98c8a76ce35d84dd6 to your computer and use it in GitHub Desktop.
Amethyst XZ plane floor test, orthographic camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use amethyst::{ | |
core::transform::TransformBundle, | |
prelude::*, | |
renderer::{ | |
plugins::{RenderFlat2D, RenderFlat3D, RenderToWindow}, | |
types::DefaultBackend, | |
RenderingBundle, | |
}, | |
utils::application_root_dir, | |
}; | |
mod room; | |
fn main() -> amethyst::Result<()> { | |
amethyst::start_logger(Default::default()); | |
let app_root = application_root_dir()?; | |
let assets_dir = app_root.join("assets"); | |
let config_dir = app_root.join("config"); | |
let display_config_path = config_dir.join("display.ron"); | |
let game_data = GameDataBuilder::default() | |
.with_bundle(TransformBundle::new())? | |
.with_bundle( | |
RenderingBundle::<DefaultBackend>::new() | |
.with_plugin( | |
RenderToWindow::from_config_path(display_config_path)? | |
.with_clear([0.34, 0.36, 0.52, 1.0]), | |
) | |
.with_plugin(RenderFlat3D::default()), | |
)?; | |
let mut game = Application::new(assets_dir, room::RoomState, game_data)?; | |
game.run(); | |
Ok(()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
amethyst = "0.15.0" | |
lazy_static = "1.4.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use amethyst::{ | |
assets::AssetLoaderSystemData, | |
core::Transform, | |
prelude::*, | |
renderer::{ | |
camera::Camera, | |
mtl::{Material, MaterialDefaults}, | |
palette::Srgba, | |
rendy::{ | |
mesh::{Normal, Position, Tangent, TexCoord}, | |
texture::palette::load_from_srgba, | |
}, | |
shape::Shape, | |
Mesh, Texture, | |
}, | |
window::ScreenDimensions, | |
}; | |
use lazy_static::lazy_static; | |
pub const TILE_UNIT_SIZE: f32 = 32.0; | |
pub const TILE_WIDTH: f32 = TILE_UNIT_SIZE; | |
pub const TILE_HEIGHT: f32 = TILE_UNIT_SIZE; | |
lazy_static! { | |
pub static ref TILE_COLOR: Srgba = Srgba::new(142.0, 142.0, 94.0, 127.0); | |
} | |
pub(crate) struct RoomState; | |
fn generate_floor(world: &mut World) { | |
let mat_defaults = world.read_resource::<MaterialDefaults>().0.clone(); | |
let (floor_mesh, floor_albedo) = { | |
let mesh = world.exec(|loader: AssetLoaderSystemData<'_, Mesh>| { | |
// WARN; XY-PLANE! | |
let tile_shape_data = Shape::Plane(None) | |
.generate::<(Vec<Position>, Vec<Normal>, Vec<Tangent>, Vec<TexCoord>)>(None) | |
.into(); | |
loader.load_from_data(tile_shape_data, ()) | |
}); | |
let albedo = world.exec(|loader: AssetLoaderSystemData<'_, Texture>| { | |
loader.load_from_data(load_from_srgba(*TILE_COLOR).into(), ()) | |
}); | |
(mesh, albedo) | |
}; | |
// WARN; Right-Handed axis system! | |
let max_depth_z = 10; | |
let max_width_x = 10; | |
for (idx, height) in (0..10).enumerate() { | |
let idx = idx as u32; | |
let tile_x_offset = (idx % max_width_x) as f32; | |
let tile_z_offset = (idx / max_width_x) as f32; | |
let tile_y_offset = 0 as f32; // TODO; Height! | |
let mut transform = Transform::default(); | |
// NOTE; Transforms UNIT 1 into TILE_SIZE | |
transform.set_scale([TILE_WIDTH, TILE_HEIGHT, 0.0].into()); | |
// NOTE; Rotation required to transform XY-Plane into XZ-Plane! | |
transform.append_rotation_x_axis(std::f32::consts::FRAC_PI_2); | |
transform.append_translation_xyz( | |
tile_x_offset * TILE_WIDTH, | |
tile_y_offset, | |
max_depth_z as f32 - tile_z_offset, | |
); | |
let material = world.exec(|mtl_loader: AssetLoaderSystemData<'_, Material>| { | |
mtl_loader.load_from_data( | |
Material { | |
albedo: floor_albedo.clone(), | |
..mat_defaults.clone() | |
}, | |
(), | |
) | |
}); | |
world | |
.create_entity() | |
.with(transform) | |
.with(floor_mesh.clone()) | |
.with(material) | |
.build(); | |
} | |
} | |
fn generate_camera(world: &mut World) { | |
let mut transform = Transform::default(); | |
transform.set_translation_xyz(0.0, 10.0, 0.0); | |
transform.prepend_rotation_x_axis(std::f32::consts::PI); | |
let (width, height) = { | |
let dim = world.read_resource::<ScreenDimensions>(); | |
(dim.width(), dim.height()) | |
}; | |
world | |
.create_entity() | |
.with(Camera::standard_2d(width, height)) | |
.with(transform) | |
.build(); | |
} | |
impl SimpleState for RoomState { | |
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { | |
let StateData { world, .. } = data; | |
generate_floor(world); | |
generate_camera(world); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment