Skip to content

Instantly share code, notes, and snippets.

@cyberj
Created August 8, 2019 15:57
Show Gist options
  • Save cyberj/415a735704818f2d6d8fae4e80f275b1 to your computer and use it in GitHub Desktop.
Save cyberj/415a735704818f2d6d8fae4e80f275b1 to your computer and use it in GitHub Desktop.
use amethyst::{
renderer::{camera::Projection, Camera},
window::ScreenDimensions,
};
use amethyst::core::{Time, Transform};
use amethyst::ecs::{Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
use crate::components::camera_subject::CameraSubject;
#[derive(Debug)]
pub struct CameraSystem;
impl<'s> System<'s> for CameraSystem {
type SystemData = (
ReadStorage<'s, CameraSubject>,
WriteStorage<'s, Transform>,
WriteStorage<'s, Camera>,
ReadExpect<'s, ScreenDimensions>,
);
fn run(&mut self, data: Self::SystemData) {
let (camera_entities, mut transforms, mut cameras, dim) = data;
for (_camera_entity, transform, camera) in
(&camera_entities, &mut transforms, &mut cameras).join()
{
// println!("Screen : {:?}", screen);
// println!("transform: {:?}", transform);
// println!("camera: {:?}", camera);
// let pos = 1. * time.absolute_time().as_secs() as f32;
const SCALE_FACTOR: f32 = 0.5;
let (width, height) = (dim.width() * SCALE_FACTOR, dim.height() * SCALE_FACTOR);
transform.set_translation_xyz(width * 0.5, height * 0.5, 10.0);
camera.set_projection(Projection::orthographic(
-width / 2.0,
width / 2.0,
-height / 2.0,
height / 2.0,
0.1,
2000.0,
));
}
}
}
/////////////////////////////
/// Init :
fn initialise_camera(world: &mut World) {
use amethyst::{renderer::camera::Camera, window::ScreenDimensions};
// Setup camera in a way that our screen covers whole arena and (0, 0) is in the bottom left.
let (width, height) = {
let dim = world.read_resource::<ScreenDimensions>();
(dim.width(), dim.height())
};
let mut transform = Transform::default();
transform.set_translation_xyz(-25., -25., 10.0);
world
.create_entity()
.with(Camera::standard_2d(width, height))
.with(components::camera_subject::CameraSubject {
id: String::from("Mine !"),
})
.with(transform)
.build();
}
///// Component
use amethyst::ecs::prelude::{Component, DenseVecStorage};
#[derive(Debug)]
pub struct CameraSubject {
pub id: String,
}
impl Component for CameraSubject {
type Storage = DenseVecStorage<Self>;
}
impl Default for CameraSubject {
fn default() -> Self {
return CameraSubject {
id: String::from("Default"),
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment