Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit d57716f

Browse files
committed
fixed fps tutorial part 3
1 parent 95dbbc4 commit d57716f

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

tutorial3-bots-ai/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ authors = ["Dmitry Stepanov <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
fyrox = {path = "../../fyrox/", version = "0.28"}
8+
fyrox = {path = "../../fyrox/", version = "0.29"}

tutorial3-bots-ai/src/bot.rs

+34-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use fyrox::animation::machine::MachineLayer;
2+
use fyrox::scene::animation::{AnimationPlayer, AnimationPlayerBuilder};
13
use fyrox::{
24
animation::{
35
machine::{Machine, Parameter, PoseNode, State, Transition},
@@ -37,7 +39,7 @@ impl Bot {
3739
.request_model("data/models/zombie.fbx")
3840
.await
3941
.unwrap()
40-
.instantiate_geometry(scene);
42+
.instantiate(scene);
4143

4244
scene.graph[model]
4345
.local_transform_mut()
@@ -130,20 +132,20 @@ impl Bot {
130132
fn create_play_animation_state(
131133
animation_resource: Model,
132134
name: &str,
133-
machine: &mut Machine,
135+
layer: &mut MachineLayer,
134136
scene: &mut Scene,
135137
model: Handle<Node>,
136138
) -> (Handle<Animation>, Handle<State>) {
137139
// Animations retargetting just makes an instance of animation and binds it to
138140
// given model using names of bones.
139141
let animation = *animation_resource
140-
.retarget_animations(model, scene)
142+
.retarget_animations(model, &mut scene.graph)
141143
.get(0)
142144
.unwrap();
143145
// Create new PlayAnimation node and add it to machine.
144-
let node = machine.add_node(PoseNode::make_play_animation(animation));
146+
let node = layer.add_node(PoseNode::make_play_animation(animation));
145147
// Make a state using the node we've made.
146-
let state = machine.add_state(State::new(name, node));
148+
let state = layer.add_state(State::new(name, node));
147149
(animation, state)
148150
}
149151

@@ -155,6 +157,7 @@ pub struct BotAnimationMachineInput {
155157
}
156158

157159
pub struct BotAnimationMachine {
160+
animation_player: Handle<Node>,
158161
machine: Machine,
159162
}
160163

@@ -172,7 +175,13 @@ impl BotAnimationMachine {
172175
model: Handle<Node>,
173176
resource_manager: ResourceManager,
174177
) -> Self {
175-
let mut machine = Machine::new(model);
178+
let animation_player =
179+
AnimationPlayerBuilder::new(BaseBuilder::new()).build(&mut scene.graph);
180+
scene.graph.link_nodes(animation_player, model);
181+
182+
let mut machine = Machine::new();
183+
184+
let root = machine.layers_mut().first_mut().unwrap();
176185

177186
// Load animations in parallel.
178187
let (walk_animation_resource, idle_animation_resource, attack_animation_resource) = fyrox::core::futures::join!(
@@ -185,29 +194,29 @@ impl BotAnimationMachine {
185194
let (_, idle_state) = create_play_animation_state(
186195
idle_animation_resource.unwrap(),
187196
"Idle",
188-
&mut machine,
197+
root,
189198
scene,
190199
model,
191200
);
192201

193202
let (walk_animation, walk_state) = create_play_animation_state(
194203
walk_animation_resource.unwrap(),
195204
"Walk",
196-
&mut machine,
205+
root,
197206
scene,
198207
model,
199208
);
200209

201210
let (attack_animation, attack_state) = create_play_animation_state(
202211
attack_animation_resource.unwrap(),
203212
"Attack",
204-
&mut machine,
213+
root,
205214
scene,
206215
model,
207216
);
208217

209218
// Next, define transitions between states.
210-
machine.add_transition(Transition::new(
219+
root.add_transition(Transition::new(
211220
// A name for debugging.
212221
"Idle->Walk",
213222
// Source state.
@@ -219,35 +228,35 @@ impl BotAnimationMachine {
219228
// A name of transition rule parameter.
220229
Self::IDLE_TO_WALK,
221230
));
222-
machine.add_transition(Transition::new(
231+
root.add_transition(Transition::new(
223232
"Walk->Idle",
224233
walk_state,
225234
idle_state,
226235
0.4,
227236
Self::WALK_TO_IDLE,
228237
));
229-
machine.add_transition(Transition::new(
238+
root.add_transition(Transition::new(
230239
"Walk->Attack",
231240
walk_state,
232241
attack_state,
233242
0.4,
234243
Self::WALK_TO_ATTACK,
235244
));
236-
machine.add_transition(Transition::new(
245+
root.add_transition(Transition::new(
237246
"Idle->Attack",
238247
idle_state,
239248
attack_state,
240249
0.4,
241250
Self::IDLE_TO_ATTACK,
242251
));
243-
machine.add_transition(Transition::new(
252+
root.add_transition(Transition::new(
244253
"Attack->Idle",
245254
attack_state,
246255
idle_state,
247256
0.4,
248257
Self::ATTACK_TO_IDLE,
249258
));
250-
machine.add_transition(Transition::new(
259+
root.add_transition(Transition::new(
251260
"Attack->Walk",
252261
attack_state,
253262
walk_state,
@@ -256,12 +265,19 @@ impl BotAnimationMachine {
256265
));
257266

258267
// Define entry state.
259-
machine.set_entry_state(idle_state);
268+
root.set_entry_state(idle_state);
260269

261-
Self { machine }
270+
Self {
271+
animation_player,
272+
machine,
273+
}
262274
}
263275

264276
pub fn update(&mut self, scene: &mut Scene, dt: f32, input: BotAnimationMachineInput) {
277+
let animation_player = scene.graph[self.animation_player]
278+
.query_component_ref::<AnimationPlayer>()
279+
.unwrap();
280+
265281
self.machine
266282
// Set transition parameters.
267283
.set_parameter(Self::WALK_TO_IDLE, Parameter::Rule(!input.walk))
@@ -271,7 +287,7 @@ impl BotAnimationMachine {
271287
.set_parameter(Self::ATTACK_TO_IDLE, Parameter::Rule(!input.attack))
272288
.set_parameter(Self::ATTACK_TO_WALK, Parameter::Rule(!input.attack))
273289
// Update machine and evaluate final pose.
274-
.evaluate_pose(&scene.animations, dt)
290+
.evaluate_pose(animation_player.animations(), dt)
275291
// Apply the pose to the graph.
276292
.apply(&mut scene.graph);
277293
}

tutorial3-bots-ai/src/main.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
use crate::{bot::Bot, message::Message, weapon::Weapon};
2-
use fyrox::engine::{EngineInitParams, SerializationContext};
3-
use fyrox::scene::pivot::PivotBuilder;
42
use fyrox::{
53
core::{
64
algebra::{Point3, UnitQuaternion, Vector3},
75
color::Color,
86
color_gradient::{ColorGradient, GradientPoint},
97
math::{ray::Ray, vector_to_quat},
10-
parking_lot::Mutex,
118
pool::{Handle, Pool},
129
sstorage::ImmutableString,
1310
},
14-
engine::{resource_manager::ResourceManager, Engine},
11+
engine::{resource_manager::ResourceManager, Engine, EngineInitParams, SerializationContext},
1512
event::{DeviceEvent, ElementState, Event, MouseButton, VirtualKeyCode, WindowEvent},
1613
event_loop::{ControlFlow, EventLoop},
17-
material::{Material, PropertyValue},
14+
material::{Material, PropertyValue, SharedMaterial},
1815
resource::texture::TextureWrapMode,
1916
scene::{
2017
base::BaseBuilder,
2118
camera::{CameraBuilder, SkyBox, SkyBoxBuilder},
2219
collider::{ColliderBuilder, ColliderShape},
2320
graph::{physics::RayCastOptions, Graph},
2421
mesh::{
25-
surface::{SurfaceBuilder, SurfaceData},
22+
surface::{SurfaceBuilder, SurfaceData, SurfaceSharedData},
2623
MeshBuilder, RenderPath,
2724
},
2825
node::Node,
2926
particle_system::{
3027
emitter::base::BaseEmitterBuilder, emitter::sphere::SphereEmitterBuilder,
3128
ParticleSystemBuilder,
3229
},
30+
pivot::PivotBuilder,
3331
rigidbody::RigidBodyBuilder,
3432
transform::TransformBuilder,
3533
Scene,
@@ -338,14 +336,14 @@ fn create_shot_trail(
338336
.build();
339337

340338
// Create unit cylinder with caps that faces toward Z axis.
341-
let shape = Arc::new(Mutex::new(SurfaceData::make_cylinder(
339+
let shape = SurfaceSharedData::new(SurfaceData::make_cylinder(
342340
6, // Count of sides
343341
1.0, // Radius
344342
1.0, // Height
345343
false, // No caps are needed.
346344
// Rotate vertical cylinder around X axis to make it face towards Z axis
347345
&UnitQuaternion::from_axis_angle(&Vector3::x_axis(), 90.0f32.to_radians()).to_homogeneous(),
348-
)));
346+
));
349347

350348
// Create an instance of standard material for the shot trail.
351349
let mut material = Material::standard();
@@ -367,7 +365,7 @@ fn create_shot_trail(
367365
.with_lifetime(0.25),
368366
)
369367
.with_surfaces(vec![SurfaceBuilder::new(shape)
370-
.with_material(Arc::new(Mutex::new(material)))
368+
.with_material(SharedMaterial::new(material))
371369
.build()])
372370
// Make sure to set Forward render path, otherwise the object won't be
373371
// transparent.
@@ -397,7 +395,7 @@ impl Game {
397395
.request_model("data/models/scene.rgs")
398396
.await
399397
.unwrap()
400-
.instantiate_geometry(&mut scene);
398+
.instantiate(&mut scene);
401399

402400
// Create player first.
403401
let mut player =
@@ -550,6 +548,7 @@ fn main() {
550548
serialization_context,
551549
events_loop: &event_loop,
552550
vsync: false,
551+
headless: false,
553552
})
554553
.unwrap();
555554

@@ -579,7 +578,7 @@ fn main() {
579578
game.update(&mut engine, TIMESTEP);
580579

581580
// Update engine each frame.
582-
engine.update(TIMESTEP, control_flow, &mut lag);
581+
engine.update(TIMESTEP, control_flow, &mut lag, Default::default());
583582
}
584583

585584
// Rendering must be explicitly requested and handled after RedrawRequested event is received.

tutorial3-bots-ai/src/weapon.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl Weapon {
1919
.request_model("data/models/m4.FBX")
2020
.await
2121
.unwrap()
22-
.instantiate_geometry(scene);
22+
.instantiate(scene);
2323

2424
let shot_point = scene.graph.find_by_name(model, "Weapon:ShotPoint");
2525

0 commit comments

Comments
 (0)