Skip to main content

physics_playground/
physics_playground.rs

1use nightshade_api::prelude::*;
2
3fn main() {
4    let mut app = open();
5    spawn_floor(&mut app.world, 20.0);
6
7    for level in 0..6 {
8        for column in 0..(6 - level) {
9            spawn_object(
10                &mut app.world,
11                Object {
12                    position: vec3(
13                        column as f32 + level as f32 * 0.5 - 3.0,
14                        level as f32 + 0.5,
15                        0.0,
16                    ),
17                    color: GREEN,
18                    body: Body::Dynamic { mass: 1.0 },
19                    ..Object::default()
20                },
21            );
22        }
23    }
24
25    let counter = spawn_text(&mut app.world, "shots: 0", ScreenAnchor::TopLeft);
26    spawn_text(
27        &mut app.world,
28        "space fires a ball, click pops a block upward",
29        ScreenAnchor::BottomLeft,
30    );
31
32    let mut shots = 0;
33    while frame(&mut app) {
34        if key_pressed(&app.world, KeyCode::Space) {
35            let muzzle = camera_position(&app.world);
36            let ball = spawn_object(
37                &mut app.world,
38                Object {
39                    shape: Shape::Sphere,
40                    position: muzzle,
41                    scale: vec3(0.3, 0.3, 0.3),
42                    color: ORANGE,
43                    body: Body::Dynamic { mass: 3.0 },
44                },
45            );
46            let velocity = camera_forward(&app.world) * 25.0;
47            set_velocity(&mut app.world, ball, velocity);
48            shots += 1;
49            set_text(&mut app.world, counter, &format!("shots: {shots}"));
50        }
51        if let Some(target) = clicked_entity(&app.world) {
52            push(&mut app.world, target, vec3(0.0, 6.0, 0.0));
53            let pop_position = position(&app.world, target);
54            emit_burst(&mut app.world, pop_position, ORANGE, 60);
55            shake_camera(&mut app.world, 0.05, 0.2);
56        }
57    }
58}