-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
183 lines (159 loc) · 4.89 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::f32::consts::PI;
use bevy::prelude::*;
use bevy_mod_picking::{
DebugCursorPickingPlugin,
DebugEventsPickingPlugin,
DefaultPickingPlugins,
PickableBundle,
PickingCameraBundle,
PickingEvent,
};
#[derive(Component)]
pub struct PickableGltf;
fn set_pickible_recursive(
commands: &mut Commands,
entity: &Entity,
mesh_query: &Query<(Entity, &Parent), With<Handle<Mesh>>>,
children_query: &Query<&Children>,
) {
for (mesh_entity, mesh_parent) in mesh_query.iter(){
if mesh_parent.get() == *entity {
commands.entity(mesh_entity).insert(PickableBundle::default());
}
}
if let Ok(children) = children_query.get(*entity) {
for child in children.iter() {
set_pickible_recursive(commands, child, mesh_query, children_query);
}
}
}
pub fn make_gltf_scene_pickable(
mut commands: Commands,
mut unpickable_query: Query<(Entity, &Name, &Children), With<PickableGltf>>,
mesh_query: Query<(Entity, &Parent), With<Handle<Mesh>>>,
children_query: Query<&Children>
){
for (entity, name, _children) in unpickable_query.iter_mut(){
println!("[MODELS] Setting Pickable on {name}");
set_pickible_recursive(&mut commands, &entity, &mesh_query, &children_query);
commands.entity(entity).remove::<PickableGltf>();
}
}
#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)]
enum GameState {
#[default]
Playing,
GameOver,
}
fn main() {
App::new()
.init_resource::<Game>()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPickingPlugins)
.add_plugin(DebugCursorPickingPlugin)
.add_plugin(DebugEventsPickingPlugin)
.add_state::<GameState>()
.add_systems((
setup_cameras.on_startup(),
setup.in_schedule(OnEnter(GameState::Playing)),
))
.add_system(bevy::window::close_on_esc)
// TODO: investigate if this can be a startup system
.add_system(make_gltf_scene_pickable)
.add_system(print_events)
.run();
}
#[derive(Default)]
struct Board {
tiles: [Option<Piece>; BOARD_SIZE_I * BOARD_SIZE_J],
}
#[derive(Debug)]
struct Piece {
entity: Entity,
}
#[derive(Resource, Default)]
struct Game {
board: Board,
}
const NUM_PIECES: u8 = 10;
// I is horizontal, J is vertical
const BOARD_ORIGIN_I: f32 = -1.72;
const BOARD_ORIGIN_J: f32 = 0.13;
const BOARD_HEIGHT: f32 = 1.45;
const BOARD_SLOT_FACTOR: f32 = 0.2;
const BOARD_SIZE_I: usize = 10;
const BOARD_SIZE_J: usize = 3;
const PIECE_1_SCALE: f32 = 0.09;
const RESET_FOCUS: [f32; 3] = [
-0.95,
0.8,
-0.5,
];
fn print_events(mut events: EventReader<PickingEvent>, game: Res<Game>) {
for event in events.iter() {
if let PickingEvent::Clicked(e) = event {
info!("A click event happened: {:?}", e);
for tile in &game.board.tiles {
if tile.is_some() {
let piece = tile.as_ref().unwrap();
println!("1: {:#?}, 2: {:#?}", piece.entity, *e);
if piece.entity == *e {
println!("{:#?}", piece);
}
}
}
}
}
}
fn setup_cameras(mut commands: Commands) {
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(
-0.8,
2.5,
1.5,
)
.looking_at(Vec3::from(RESET_FOCUS), Vec3::Y),
..default()
},
PickingCameraBundle::default(),
));
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMut<Game>) {
let piece_1_scene = asset_server.load("piece.glb#Scene0");
for i in 0..NUM_PIECES as usize {
let scene = piece_1_scene.clone();
let scale = Vec3::splat(PIECE_1_SCALE);
let piece_i = BOARD_ORIGIN_I + i as f32 * BOARD_SLOT_FACTOR;
let piece_j = BOARD_ORIGIN_J + 0 as f32 * BOARD_SLOT_FACTOR;
game.board.tiles[i] = Some(Piece {
entity: commands
.spawn(SceneBundle {
transform: Transform {
translation: Vec3::new(
piece_i,
BOARD_HEIGHT,
piece_j,
),
rotation: Quat::from_rotation_y(-PI / 2.),
scale,
},
scene,
..default()
})
.insert(Name::new(format!("Piece {}", i)))
.insert(PickableGltf)
.id(),
})
}
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 10.0, 4.0),
point_light: PointLight {
intensity: 3500.0,
shadows_enabled: true,
range: 30.0,
..default()
},
..default()
});
}