diff --git a/resources/display_config.ron b/resources/display_config.ron index 7999694..3aa60f1 100644 --- a/resources/display_config.ron +++ b/resources/display_config.ron @@ -1,6 +1,6 @@ ( title: "Pong!", - dimensions: Some((500, 500)), + dimensions: Some((875, 500)), max_dimensions: None, min_dimensions: None, fullscreen: false, diff --git a/src/main.rs b/src/main.rs index 727324c..91772f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ extern crate amethyst; +mod pong; + use amethyst::prelude::*; use amethyst::renderer::{DisplayConfig, DrawFlat2D, Event, Pipeline, RenderBundle, Stage, VirtualKeyCode}; use amethyst::utils::application_root_dir; - -pub struct Pong; - -impl SimpleState for Pong { -} +use amethyst::core::transform::TransformBundle; +use crate::pong::Pong; fn main() -> amethyst::Result<()> { amethyst::start_logger(Default::default()); @@ -26,7 +25,8 @@ fn main() -> amethyst::Result<()> { .with_bundle( RenderBundle::new(pipe, Some(config)) .with_sprite_sheet_processor() - )?; + )? + .with_bundle(TransformBundle::new())?; let mut game = Application::new("./", Pong, game_data)?; diff --git a/src/pong.rs b/src/pong.rs new file mode 100644 index 0000000..5627e7a --- /dev/null +++ b/src/pong.rs @@ -0,0 +1,111 @@ + +use amethyst::assets::{AssetStorage, Loader}; +use amethyst::core::transform::Transform; +use amethyst::ecs::prelude::{Component, DenseVecStorage}; +use amethyst::prelude::*; +use amethyst::renderer::{ + Camera, PngFormat, Projection, SpriteRender, SpriteSheet, + SpriteSheetFormat, SpriteSheetHandle, Texture, TextureMetadata, +}; + +pub const ARENA_HEIGHT: f32 = 100.0; +pub const ARENA_WIDTH: f32 = 100.0; + +const PADDLE_HEIGHT: f32 = 16.0; +const PADDLE_WIDTH: f32 = 4.0; + +pub struct Pong; + +impl SimpleState for Pong { + fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { + let world = data.world; + + world.register::(); + initialise_paddles(world); + initialise_camera(world); + } + +} + +#[derive(PartialEq, Eq)] +pub enum Side { + Left, + Right, +} + +pub struct Paddle { + pub side: Side, + pub width: f32, + pub height: f32, +} + +impl Paddle { + fn new(side: Side) -> Paddle { + Paddle { + side, + width: 1.0, + height: 1.0, + } + } +} + +impl Component for Paddle { + type Storage = DenseVecStorage; +} + +fn initialise_camera(world: &mut World) { + let mut transform = Transform::default(); + transform.set_z(1.0); + world + .create_entity() + .with(Camera::from(Projection::orthographic( + 0.0, + ARENA_WIDTH, + 0.0, + ARENA_HEIGHT, + ))) + .with(transform) + .build(); +} + +/// Initialises one paddle on the left, and one paddle on the right. +fn initialise_paddles(world: &mut World) { + let mut left_transform = Transform::default(); + let mut right_transform = Transform::default(); + + // Correctly position the paddles. + let y = ARENA_HEIGHT / 2.0; + left_transform.set_xyz(PADDLE_WIDTH * 0.5, y, 0.0); + right_transform.set_xyz(ARENA_WIDTH - PADDLE_WIDTH * 0.5, y, 0.0); + + // Create a left plank entity. + world + .create_entity() + .with(Paddle::new(Side::Left)) + .with(left_transform) + .build(); + + // Create right plank entity. + world + .create_entity() + .with(Paddle::new(Side::Right)) + .with(right_transform) + .build(); +} + +fn load_sprite_sheet(world: &mut World) -> SpriteSheetHandle { + // Load the sprite sheet necessary to render the graphics. + // The texture is the pixel data + // `texture_handle` is a cloneable reference to the texture + let texture_handle = { + let loader = world.read_resource::(); + let texture_storage = world.read_resource::>(); + loader.load( + "texture/pong_spritesheet.png", + PngFormat, + TextureMetadata::srgb_scale(), + (), + &texture_storage, + ) + }; +} diff --git a/texture/pong_spritesheet.png b/texture/pong_spritesheet.png new file mode 100644 index 0000000..d68beda Binary files /dev/null and b/texture/pong_spritesheet.png differ diff --git a/texture/pong_spritesheet.ron b/texture/pong_spritesheet.ron new file mode 100644 index 0000000..53fd7e8 --- /dev/null +++ b/texture/pong_spritesheet.ron @@ -0,0 +1,18 @@ +( + spritesheet_width: 8, + spritesheet_height: 16, + sprites: [ + ( + x: 0, + y: 0, + width: 4, + height: 16, + ), + ( + x: 4, + y: 0, + width: 4, + height: 4, + ), + ], +) \ No newline at end of file