added a texture, pong struct & traits got it basically running with nothing in it

This commit is contained in:
brad 2019-04-22 00:21:48 -05:00
parent 5a8b2fcbf1
commit 7e94d160d2
5 changed files with 136 additions and 7 deletions

View File

@ -1,6 +1,6 @@
( (
title: "Pong!", title: "Pong!",
dimensions: Some((500, 500)), dimensions: Some((875, 500)),
max_dimensions: None, max_dimensions: None,
min_dimensions: None, min_dimensions: None,
fullscreen: false, fullscreen: false,

View File

@ -1,14 +1,13 @@
extern crate amethyst; extern crate amethyst;
mod pong;
use amethyst::prelude::*; use amethyst::prelude::*;
use amethyst::renderer::{DisplayConfig, DrawFlat2D, Event, Pipeline, use amethyst::renderer::{DisplayConfig, DrawFlat2D, Event, Pipeline,
RenderBundle, Stage, VirtualKeyCode}; RenderBundle, Stage, VirtualKeyCode};
use amethyst::utils::application_root_dir; use amethyst::utils::application_root_dir;
use amethyst::core::transform::TransformBundle;
pub struct Pong; use crate::pong::Pong;
impl SimpleState for Pong {
}
fn main() -> amethyst::Result<()> { fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default()); amethyst::start_logger(Default::default());
@ -26,7 +25,8 @@ fn main() -> amethyst::Result<()> {
.with_bundle( .with_bundle(
RenderBundle::new(pipe, Some(config)) RenderBundle::new(pipe, Some(config))
.with_sprite_sheet_processor() .with_sprite_sheet_processor()
)?; )?
.with_bundle(TransformBundle::new())?;
let mut game = Application::new("./", Pong, game_data)?; let mut game = Application::new("./", Pong, game_data)?;

111
src/pong.rs Normal file
View File

@ -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::<Paddle>();
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<Self>;
}
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::<Loader>();
let texture_storage = world.read_resource::<AssetStorage<Texture>>();
loader.load(
"texture/pong_spritesheet.png",
PngFormat,
TextureMetadata::srgb_scale(),
(),
&texture_storage,
)
};
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 B

View File

@ -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,
),
],
)