mirror of
https://github.com/neogeek23/rust_pong.git
synced 2026-02-04 02:48:17 +00:00
added a texture, pong struct & traits got it basically running with nothing in it
This commit is contained in:
parent
5a8b2fcbf1
commit
7e94d160d2
@ -1,6 +1,6 @@
|
||||
(
|
||||
title: "Pong!",
|
||||
dimensions: Some((500, 500)),
|
||||
dimensions: Some((875, 500)),
|
||||
max_dimensions: None,
|
||||
min_dimensions: None,
|
||||
fullscreen: false,
|
||||
|
||||
12
src/main.rs
12
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)?;
|
||||
|
||||
|
||||
111
src/pong.rs
Normal file
111
src/pong.rs
Normal 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,
|
||||
)
|
||||
};
|
||||
}
|
||||
BIN
texture/pong_spritesheet.png
Normal file
BIN
texture/pong_spritesheet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 129 B |
18
texture/pong_spritesheet.ron
Normal file
18
texture/pong_spritesheet.ron
Normal 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,
|
||||
),
|
||||
],
|
||||
)
|
||||
Loading…
x
Reference in New Issue
Block a user