simple_camera/
simple_camera.rs1use lotus_engine::*;
5
6your_game!(
7 WindowConfiguration::default(),
8 setup,
9 update
10);
11
12fn setup(context: &mut Context) {
13 let player: Sprite = Sprite::new("textures/lotus_pink_256x256.png".to_string());
14 let secondary_sprite: Sprite = Sprite::new("textures/lotus_pink_256x256.png".to_string());
15 let shape: Shape = Shape::new(Orientation::Horizontal, GeometryType::Square, Color::by_option(ColorOption::Burgundy));
16 let text: Text = Text::new(
17 &mut context.render_state,
18 Font::new(Fonts::RobotoMono.get_path(), 30.0),
19 Position::new(Vector2::new(0.0, 0.0), Strategy::Pixelated),
20 Color::by_option(ColorOption::Black),
21 "Boiler Plate".to_string()
22 );
23
24 context.commands.spawn(
25 vec![
26 Box::new(player),
27 Box::new(Transform::new(
28 Position::new(Vector2::new(0.0, 0.0), Strategy::Normalized),
29 0.0,
30 Vector2::new(0.25, 0.25)
31 )),
32 Box::new(Velocity::new(Vector2::new(1.0, 1.0)))
33 ]
34 );
35
36 context.commands.spawn(
37 vec![
38 Box::new(secondary_sprite),
39 Box::new(Transform::new(
40 Position::new(Vector2::new(-0.25, 0.0), Strategy::Normalized),
41 0.0,
42 Vector2::new(0.25, 0.25)
43 ))
44 ]
45 );
46
47 context.commands.spawn(
48 vec![
49 Box::new(shape),
50 Box::new(Transform::new(
51 Position::new(Vector2::new(-0.75, 0.0), Strategy::Normalized),
52 0.0,
53 Vector2::new(0.25, 0.25)
54 )),
55 Box::new(Velocity::new(Vector2::new(1.0, 1.0)))
56 ]
57 );
58
59 context.commands.spawn(
60 vec![
61 Box::new(text)
62 ]
63 );
64}
65
66fn update(context: &mut Context) {
67 let keyboard_input: ResourceRef<'_, KeyboardInput> = context.world.get_resource::<KeyboardInput>().unwrap();
68
69 let mut query: Query = Query::new(&context.world).with::<Sprite>().with::<Velocity>();
70 let player_entity: Entity = query.entities_with_components().unwrap().first().unwrap().clone();
71
72 let mut camera2d: ResourceRefMut<'_, Camera2d> = context.world.get_resource_mut::<Camera2d>().unwrap();
73 camera2d.set_target(player_entity);
74
75 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&player_entity).unwrap();
76 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&player_entity).unwrap();
77
78 if keyboard_input.is_key_pressed(KeyboardKey::ArrowRight) {
79 let x: f32 = transform.position.x + velocity.x * context.delta;
80 transform.set_position_x(&context.render_state, x);
81 } else if keyboard_input.is_key_pressed(KeyboardKey::ArrowLeft) {
82 let x: f32 = transform.position.x - velocity.x * context.delta;
83 transform.set_position_x(&context.render_state, x);
84 }
85}