pub struct Transform {
pub position: Position,
pub rotation: f32,
pub scale: Vector2<f32>,
/* private fields */
}Expand description
Struct to represent the transform matrix of every object rendered.
Fields§
§position: Position§rotation: f32§scale: Vector2<f32>Implementations§
Source§impl Transform
impl Transform
Sourcepub fn new(position: Position, rotation: f32, scale: Vector2<f32>) -> Self
pub fn new(position: Position, rotation: f32, scale: Vector2<f32>) -> Self
Create a new transform with parameters.
Examples found in repository?
14fn setup(context: &mut Context) {
15 let table: Shape = Shape::new(Orientation::Horizontal, GeometryType::Rectangle, Color::by_option(ColorOption::Black));
16 let object: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(Circle::default()), Color::by_option(ColorOption::Blue));
17
18 context.commands.spawn(vec![
19 Box::new(table),
20 Box::new(Transform::new(
21 Position::new(Vector2::new(0.0, -0.70), Strategy::Normalized),
22 0.0,
23 Vector2::new(0.90, 0.10)
24 )),
25 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
26 ]);
27
28 context.commands.spawn(vec![
29 Box::new(object),
30 Box::new(Transform::new(
31 Position::new(Vector2::new(400.0, 100.0), Strategy::Pixelated),
32 0.0,
33 Vector2::new(0.50, 0.50)
34 )),
35 Box::new(Collision::new(Collider::new_simple(GeometryType::Square))),
36 Box::new(Gravity::new(0.0)),
37 Box::new(Velocity::new(Vector2::new(0.2, 0.2))),
38 Box::new(RigidBody::new(BodyType::Dynamic, 1.0, 0.9, 1.0))
39 ]);
40}More examples
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}20fn setup(context: &mut Context) {
21 let circle: Circle = Circle::new(64, 0.5);
22 let red_object: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(circle.clone()), Color::by_option(ColorOption::Red));
23 let blue_object: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(circle.clone()), Color::by_option(ColorOption::Blue));
24
25 context.commands.spawn(
26 vec![
27 Box::new(red_object),
28 Box::new(Object()),
29 Box::new(Transform::new(
30 Position::new(Vector2::new(0.0, 0.0), Strategy::Normalized),
31 0.0,
32 Vector2::new(0.30, 0.30)
33 )),
34 Box::new(Velocity::new(Vector2::new(0.45, 0.45))),
35 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
36 ]
37 );
38
39 context.commands.spawn(
40 vec![
41 Box::new(blue_object),
42 Box::new(Object()),
43 Box::new(Transform::new(
44 Position::new(Vector2::new(-0.45, 0.0), Strategy::Normalized),
45 0.0,
46 Vector2::new(0.30, 0.30)
47 )),
48 Box::new(Velocity::new(Vector2::new(0.45, 0.45))),
49 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
50 ]
51 );
52
53 spawn_border(context, Orientation::Horizontal, Vector2::new(0.0, -1.), Vector2::new(context.window_configuration.width as f32, 0.01));
54 spawn_border(context, Orientation::Horizontal, Vector2::new(0.0, 1.), Vector2::new(context.window_configuration.width as f32, 0.01));
55 spawn_border(context, Orientation::Vertical, Vector2::new(1.35, 0.), Vector2::new(0.01, context.window_configuration.height as f32));
56 spawn_border(context, Orientation::Vertical, Vector2::new(-1.35, 0.), Vector2::new(0.01, context.window_configuration.height as f32));
57}
58
59fn update(context: &mut Context) {
60 let mut query: Query = Query::new(&context.world).with::<Object>();
61 let entities: Vec<Entity> = query.entities_with_components().unwrap();
62
63 check_border_collision(context, &entities);
64 check_object_collision(context, &entities);
65 move_objects(context, &entities);
66}
67
68fn spawn_border(context: &mut Context, orientation: Orientation, position: Vector2<f32>, scale: Vector2<f32>) {
69 let border: Shape = Shape::new(orientation, GeometryType::Rectangle, Color::by_option(ColorOption::White));
70
71 context.commands.spawn(
72 vec![
73 Box::new(border),
74 Box::new(Border()),
75 Box::new(Transform::new(
76 Position::new(position, Strategy::Normalized),
77 0.0,
78 scale
79 )),
80 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
81 ]
82 );
83}26fn setup(context: &mut Context) {
27 context.game_loop_listener.fps_cap(120);
28
29 let my_square: Shape = Shape::new(Orientation::Horizontal, GeometryType::Square, Color::by_option(ColorOption::Blue));
30 let my_rectangle: Shape = Shape::new(Orientation::Horizontal, GeometryType::Rectangle, Color::by_option(ColorOption::Green));
31 let my_triangle: Shape = Shape::new(Orientation::Horizontal, GeometryType::Triangle, Color::by_option(ColorOption::Red));
32 let my_circle: Shape = Shape::new(
33 Orientation::Horizontal,
34 GeometryType::Circle(Circle::new(64, 0.5)),
35 Color::by_option(ColorOption::Black)
36 );
37
38 context.commands.spawn(
39 vec![
40 Box::new(my_square),
41 Box::new(Transform::new(
42 Position::new(Vector2::new(-0.60, -0.25), Strategy::Normalized),
43 0.0,
44 Vector2::new(0.10, 0.10)
45 )),
46 Box::new(MySquare())
47 ]
48 );
49 context.commands.spawn(
50 vec![
51 Box::new(my_rectangle),
52 Box::new(Transform::new(
53 Position::new(Vector2::new(-0.35, 0.20), Strategy::Normalized),
54 0.0,
55 Vector2::new(0.50, 0.50)
56 )),
57 Box::new(MyRectangle())
58 ]
59 );
60 context.commands.spawn(
61 vec![
62 Box::new(my_triangle),
63 Box::new(Transform::new(
64 Position::new(Vector2::new(0.50, 0.50), Strategy::Normalized),
65 0.0,
66 Vector2::new(0.25, 0.25)
67 )),
68 Box::new(MyTriangle())
69 ]
70 );
71 context.commands.spawn(
72 vec![
73 Box::new(my_circle),
74 Box::new(Transform::new(
75 Position::new(Vector2::new(0.80, 0.50), Strategy::Normalized),
76 0.0,
77 Vector2::new(0.25, 0.25)
78 )),
79 Box::new(MyCircle())
80 ]
81 );
82}16fn setup(context: &mut Context) {
17 let circle: Circle = Circle::new(64, 0.5);
18 let mut thread_rng: ThreadRng = rng();
19
20 let left_border_perimeter: f64 = -1.3;
21 let right_border_perimiter: f64 = 1.3;
22 let bottom_border_perimeter: f64 = -0.9;
23 let top_border_perimeter: f64 = 0.9;
24
25 // Update the number of spawns for performance benchmarking.
26 for _ in 1..=10 {
27 let randomic_position_x: f32 = thread_rng.random_range(left_border_perimeter..right_border_perimiter) as f32;
28 let randomic_position_y: f32 = thread_rng.random_range(bottom_border_perimeter..top_border_perimeter) as f32;
29
30 let randomic_velocity_x: f32 = thread_rng.random_range(-0.5..0.5) as f32;
31 let randomic_velocity_y: f32 = thread_rng.random_range(-0.5..0.5) as f32;
32
33 context.commands.spawn(
34 vec![
35 Box::new(Shape::new(Orientation::Horizontal, GeometryType::Circle(circle.clone()), Color::by_option(ColorOption::Blue))),
36 Box::new(Object()),
37 Box::new(Transform::new(
38 Position::new(Vector2::new(randomic_position_x, randomic_position_y), Strategy::Normalized),
39 0.0,
40 Vector2::new(0.15, 0.15)
41 )),
42 Box::new(Velocity::new(Vector2::new(randomic_velocity_x, randomic_velocity_y))),
43 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
44 ]
45 );
46 }
47
48 spawn_border(context, Orientation::Horizontal, Vector2::new(0.0, -1.), Vector2::new(context.window_configuration.width as f32, 0.01));
49 spawn_border(context, Orientation::Horizontal, Vector2::new(0.0, 1.), Vector2::new(context.window_configuration.width as f32, 0.01));
50 spawn_border(context, Orientation::Vertical, Vector2::new(1.35, 0.), Vector2::new(0.01, context.window_configuration.height as f32));
51 spawn_border(context, Orientation::Vertical, Vector2::new(-1.35, 0.), Vector2::new(0.01, context.window_configuration.height as f32));
52}
53
54fn update(context: &mut Context) {
55 context.commands.show_fps(context.game_loop_listener.current_fps, Color::by_option(ColorOption::Black));
56
57 let mut query: Query = Query::new(&context.world).with::<Object>();
58 let entities: Vec<Entity> = query.entities_with_components().unwrap();
59
60 check_border_collision(context, &entities);
61 move_objects(context, &entities);
62}
63
64fn spawn_border(context: &mut Context, orientation: Orientation, position: Vector2<f32>, scale: Vector2<f32>) {
65 let border: Shape = Shape::new(orientation, GeometryType::Rectangle, Color::by_option(ColorOption::White));
66
67 context.commands.spawn(
68 vec![
69 Box::new(border),
70 Box::new(Border()),
71 Box::new(Transform::new(
72 Position::new(position, Strategy::Normalized),
73 0.0,
74 scale
75 )),
76 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
77 ]
78 );
79}67fn setup(context: &mut Context) {
68 let player: Shape = Shape::new(Orientation::Horizontal, GeometryType::Rectangle, Color::by_option(ColorOption::Purple));
69 let little_ball: Shape = Shape::new(
70 Orientation::Horizontal,
71 GeometryType::Circle(Circle::new(64, 0.2)),
72 Color::by_option(ColorOption::Black)
73 );
74 let start_text: Text = Text::new(
75 &mut context.render_state,
76 Font::new(Fonts::RobotoMonoItalic.get_path(), 40.0),
77 Position::new(Vector2::new(298.0, 380.0), Strategy::Pixelated),
78 Color::by_option(ColorOption::Black),
79 "> enter <".to_string()
80 );
81
82 let mut thread_rng: ThreadRng = rand::rng();
83 let random_direction: bool = thread_rng.random_bool(1.0 / 3.0);
84
85 let velocity_x: f32 = if random_direction {
86 1.2
87 } else {
88 -1.2
89 };
90
91 context.commands.add_resources(vec![
92 Box::new(LittleBallRespawnTimer::new()),
93 Box::new(NextState::default())
94 ]);
95 context.commands.spawn(vec![Box::new(start_text)]);
96
97 context.commands.spawn(
98 vec![
99 Box::new(player),
100 Box::new(Player()),
101 Box::new(Transform::new(
102 Position::new(Vector2::new(0.0, -0.85), Strategy::Normalized),
103 0.0,
104 Vector2::new(0.15, 0.10)
105 )),
106 Box::new(Velocity::new(Vector2::new(2.0, 2.0))),
107 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
108 ]
109 );
110
111 context.commands.spawn(
112 vec![
113 Box::new(little_ball),
114 Box::new(LittleBall()),
115 Box::new(Transform::new(
116 Position::new(Vector2::new(0.0, -0.5), Strategy::Normalized),
117 0.0,
118 Vector2::new(0.10, 0.10)
119 )),
120 Box::new(Velocity::new(Vector2::new(velocity_x, -0.5))),
121 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
122 ]
123 );
124
125 spawn_border(context, Vector2::new(1.05, 0.0));
126 spawn_border(context, Vector2::new(-1.05, 0.0));
127 spawn_targets(context);
128}
129
130fn update(context: &mut Context) {
131 let keyboard_input: KeyboardInput = context.world.get_resource_cloned::<KeyboardInput>().unwrap();
132 let mouse_input: MouseInput = context.world.get_resource_cloned::<MouseInput>().unwrap();
133 let is_hover: bool = mouse_input.mouse_position.x >= 298.0 && (mouse_input.mouse_position.y > 380.0 && mouse_input.mouse_position.y < 416.0);
134
135 if
136 keyboard_input.is_key_released(KeyboardKey::Enter) ||
137 (mouse_input.is_mouse_button_released(MouseButton::Left) && is_hover)
138 {
139 let mut next_state: ResourceRefMut<'_, NextState> = context.world.get_resource_mut::<NextState>().unwrap();
140 next_state.0 = GameState::Running;
141
142 let mut query: Query = Query::new(&context.world).with::<Text>();
143 if let Some(entity) = query.entities_with_components().unwrap().first() {
144 context.commands.despawn(entity.clone());
145 }
146 }
147
148 if context.world.get_resource::<NextState>().unwrap().0 == GameState::Running {
149 let mut player_query: Query = Query::new(&context.world).with::<Player>();
150 let player_entity: Entity = player_query.entities_with_components().unwrap().first().unwrap().clone();
151
152 let mut little_ball_query: Query = Query::new(&context.world).with::<LittleBall>();
153 let little_ball_entity: Entity = little_ball_query.entities_with_components().unwrap().first().unwrap().clone();
154
155 let mut thread_rng: ThreadRng = rand::rng();
156 let random_factor: f32 = thread_rng.random_range(-0.5..0.5);
157
158 move_player(context, keyboard_input, player_entity);
159 move_little_ball(context, little_ball_entity);
160 check_player_little_ball_collision(context, player_entity, little_ball_entity, random_factor);
161 check_little_ball_borders_collision(context, little_ball_entity, random_factor);
162 check_litte_ball_targets_collision(context, little_ball_entity, random_factor);
163 respawn_little_ball_after_outbounds(context, little_ball_entity);
164 }
165}
166
167fn spawn_border(context: &mut Context, position: Vector2<f32>) {
168 let border: Shape = Shape::new(Orientation::Vertical, GeometryType::Rectangle, Color::by_option(ColorOption::Cyan));
169
170 context.commands.spawn(
171 vec![
172 Box::new(border),
173 Box::new(Border()),
174 Box::new(Transform::new(
175 Position::new(position, Strategy::Normalized),
176 0.0,
177 Vector2::new(0.01, context.window_configuration.height as f32)
178 )),
179 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
180 ]
181 );
182}
183
184fn spawn_targets(context: &mut Context) {
185 let width: f32 = 0.15;
186 let height: f32 = 0.10;
187
188 let rows: i32 = 8;
189 let columns: i32 = 10;
190 let spacing_x: f32 = 0.09;
191 let spacing_y: f32 = 0.02;
192
193 let start_x: f32 = -(columns as f32 * (width + spacing_x)) / 2.0;
194 let start_y: f32 = 1.0 - 0.1;
195
196 for row in 0..rows {
197 for column in 0..columns {
198 let x: f32 = start_x + column as f32 * (width + spacing_x);
199 let y: f32 = start_y - row as f32 * (height + spacing_y);
200
201 let mut color: Color = Color::by_option(ColorOption::Red);
202
203 if row == 2 || row == 3 {
204 color = Color::by_option(ColorOption::Orange);
205 } else if row == 4 || row == 5 {
206 color = Color::by_option(ColorOption::Green);
207 } else if row == 6 || row == 7 {
208 color = Color::by_option(ColorOption::Yellow);
209 }
210
211 context.commands.spawn(
212 vec![
213 Box::new(Shape::new(Orientation::Horizontal, GeometryType::Rectangle, color)),
214 Box::new(Target()),
215 Box::new(Transform::new(
216 Position::new(Vector2::new(x, y), Strategy::Normalized),
217 0.0,
218 Vector2::new(width, height)
219 )),
220 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle))),
221 ]
222 );
223 }
224 }
225}Sourcepub fn new_simple(position: Position) -> Self
pub fn new_simple(position: Position) -> Self
Create a new transform with only the position as an argument.
Sourcepub fn to_matrix(&self) -> Matrix4<f32>
pub fn to_matrix(&self) -> Matrix4<f32>
Returns the current transform struct as a matrix of f32s.
Sourcepub fn write_update_to_buffer(&self, render_state: &RenderState)
pub fn write_update_to_buffer(&self, render_state: &RenderState)
Write the transform matrix updates to its related buffer on rendering surface.
Sourcepub fn set_position(
&mut self,
render_state: &RenderState,
position: Vector2<f32>,
)
pub fn set_position( &mut self, render_state: &RenderState, position: Vector2<f32>, )
Set the current position and sends it to the buffer.
Examples found in repository?
More examples
240fn move_little_ball(context: &mut Context, little_ball_entity: Entity) {
241 let mut little_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
242 let little_ball_velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&little_ball_entity).unwrap();
243
244 let new_position: Vector2<f32> = little_ball_transform.position.to_vec() + little_ball_velocity.to_vec() * context.delta;
245 little_ball_transform.set_position(&context.render_state, new_position);
246}
247
248fn check_player_little_ball_collision(context: &mut Context, player_entity: Entity, little_ball_entity: Entity, random_factor: f32) {
249 let mut little_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
250 let mut little_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&little_ball_entity).unwrap();
251 let little_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&little_ball_entity).unwrap();
252
253 let player_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&player_entity).unwrap();
254
255 if Collision::check(CollisionAlgorithm::Aabb, &player_collision, &little_ball_collision) {
256 let velocity_magnitude: f32 = little_ball_velocity.to_vec().magnitude();
257 let collision_point: f32 = (
258 (little_ball_collision.collider.position.x - player_collision.collider.position.x) /
259 (player_collision.collider.scale.x * 0.5)
260 ).clamp(-1.0, 1.0);
261
262 let mut new_direction: Vector2<f32> = Vector2::new(
263 collision_point * 1.5,
264 1.0 - collision_point.abs() * 0.3
265 ).normalize();
266
267 new_direction.x += random_factor * 0.15;
268 new_direction = new_direction.normalize();
269
270 little_ball_velocity.x = new_direction.x * velocity_magnitude;
271 little_ball_velocity.y = new_direction.y * velocity_magnitude;
272 little_ball_transform.position.y += 0.03;
273 }
274}
275
276fn check_little_ball_borders_collision(context: &mut Context, little_ball_entity: Entity, random_factor: f32) {
277 let mut border_query: Query = Query::new(&context.world).with::<Border>();
278 let borders_entities: Vec<Entity> = border_query.entities_with_components().unwrap();
279
280 let mut little_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
281 let mut little_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&little_ball_entity).unwrap();
282 let little_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&little_ball_entity).unwrap();
283
284 for border in &borders_entities {
285 let border_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(border).unwrap();
286
287 if Collision::check(CollisionAlgorithm::Aabb, &little_ball_collision, &border_collision) {
288 let velocity_magnitude: f32 = little_ball_velocity.to_vec().magnitude();
289 let collision_normal: Vector2<f32> = if border_collision.collider.position.x > 0.0 {
290 Vector2::new(-1.0, 0.0)
291 } else {
292 Vector2::new(1.0, 0.0)
293 };
294
295 let new_direction: Vector2<f32> = (
296 little_ball_velocity.to_vec().normalize() - 2.0 *
297 little_ball_velocity.to_vec().normalize().dot(collision_normal) * collision_normal
298 ).normalize();
299
300 let randomized_direction: Vector2<f32> = Vector2::new(
301 new_direction.x + random_factor * 0.3,
302 new_direction.y
303 ).normalize();
304
305 little_ball_velocity.x = randomized_direction.x * velocity_magnitude;
306 little_ball_velocity.y = randomized_direction.y * velocity_magnitude;
307
308 let collision_offset: Vector2<f32> = collision_normal * 0.02;
309 little_ball_transform.position.x += collision_offset.x;
310 }
311 }
312}
313
314fn check_litte_ball_targets_collision(context: &mut Context, little_ball_entity: Entity, random_factor: f32) {
315 let mut targets_query: Query = Query::new(&context.world).with::<Target>();
316 let targets_entities: Vec<Entity> = targets_query.entities_with_components().unwrap();
317
318 let mut little_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
319 let mut little_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&little_ball_entity).unwrap();
320 let little_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&little_ball_entity).unwrap();
321
322 for target in &targets_entities {
323 let target_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(target).unwrap();
324
325 if Collision::check(CollisionAlgorithm::Aabb, &little_ball_collision, &target_collision) {
326 let velocity_magnitude: f32 = little_ball_velocity.to_vec().magnitude();
327 let impact_vector: Vector2<f32> = (target_collision.collider.position - little_ball_collision.collider.position).normalize();
328
329 let mut new_direction: Vector2<f32> = Vector2::new(
330 -impact_vector.x * 0.8 + random_factor * 0.2,
331 -impact_vector.y * 0.8 + random_factor * 0.2
332 ).normalize();
333
334 new_direction.y = new_direction.y.signum() * new_direction.y.abs().max(0.3);
335
336 little_ball_velocity.x = new_direction.x * velocity_magnitude;
337 little_ball_velocity.y = new_direction.y * velocity_magnitude;
338
339 little_ball_transform.position.x -= impact_vector.x * 0.05;
340 little_ball_transform.position.y -= impact_vector.y * 0.05;
341 context.commands.despawn(target.clone());
342 }
343 }
344}
345
346fn respawn_little_ball_after_outbounds(context: &mut Context, little_ball_entity: Entity) {
347 let mut litte_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
348 let position_default: Vector2<f32> = Vector2::new(0.0, -0.25);
349
350 if litte_ball_transform.position.y < -1.0 {
351 let mut little_ball_respawn_timer: ResourceRefMut<'_, LittleBallRespawnTimer> = context.world.get_resource_mut::<LittleBallRespawnTimer>().unwrap();
352 little_ball_respawn_timer.0.tick(context.delta);
353
354 if little_ball_respawn_timer.0.is_finished() {
355 litte_ball_transform.set_position(&context.render_state, position_default);
356 }
357 }
358}172fn move_gray_racket(context: &mut Context, keyboard_input: KeyboardInput) {
173 let mut query: Query = Query::new(&context.world).with::<GrayRacket>();
174 let entities: Vec<Entity> = query.entities_with_components().unwrap();
175 let gray_racket_entity: &Entity = entities.first().unwrap();
176
177 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(gray_racket_entity).unwrap();
178 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(gray_racket_entity).unwrap();
179
180 if keyboard_input.is_key_pressed(KeyboardKey::KeyW) {
181 transform.position.y += velocity.y * context.delta;
182 let new_position: Vector2<f32> = Vector2::new(transform.position.x, transform.position.y);
183 transform.set_position(&context.render_state, new_position);
184 } else if keyboard_input.is_key_pressed(KeyboardKey::KeyS) {
185 transform.position.y -= velocity.y * context.delta;
186 let new_position: Vector2<f32> = Vector2::new(transform.position.x, transform.position.y);
187 transform.set_position(&context.render_state, new_position);
188 }
189}
190
191fn move_pink_racket(context: &mut Context, keyboard_input: KeyboardInput) {
192 let mut query: Query = Query::new(&context.world).with::<PinkRacket>();
193 let entities: Vec<Entity> = query.entities_with_components().unwrap();
194 let pink_racket_entity: &Entity = entities.first().unwrap();
195
196 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(pink_racket_entity).unwrap();
197 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(pink_racket_entity).unwrap();
198
199 if keyboard_input.is_key_pressed(KeyboardKey::ArrowUp) {
200 transform.position.y += velocity.y * context.delta;
201 let new_position: Vector2<f32> = Vector2::new(transform.position.x, transform.position.y);
202 transform.set_position(&context.render_state, new_position);
203 } else if keyboard_input.is_key_pressed(KeyboardKey::ArrowDown) {
204 transform.position.y -= velocity.y * context.delta;
205 let new_position: Vector2<f32> = Vector2::new(transform.position.x, transform.position.y);
206 transform.set_position(&context.render_state, new_position);
207 }
208}
209
210fn move_pong_ball(context: &mut Context, pong_ball: &Entity) {
211 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&pong_ball).unwrap();
212 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&pong_ball).unwrap();
213
214 let new_position: Vector2<f32> = transform.position.to_vec() + velocity.to_vec() * context.delta;
215 transform.set_position(&context.render_state, new_position);
216}
217
218fn check_rackets_ball_collision(context: &mut Context, pong_ball: &Entity, random_factor: f32) {
219 let mut racket_query: Query = Query::new(&context.world).with::<Racket>();
220 let rackets: Vec<Entity> = racket_query.entities_with_components().unwrap();
221 let mut game_audio: ResourceRefMut<'_, GameAudio> = context.world.get_resource_mut::<GameAudio>().unwrap();
222
223 for racket in &rackets {
224 let racket_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(racket).unwrap();
225 let racket_transform: ComponentRef<'_, Transform> = context.world.get_entity_component::<Transform>(racket).unwrap();
226
227 let pong_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&pong_ball).unwrap();
228 let mut pong_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&pong_ball).unwrap();
229 let mut pong_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&pong_ball).unwrap();
230
231 if Collision::check(CollisionAlgorithm::Aabb, &racket_collision, &pong_ball_collision) {
232 game_audio.0.play_static_sound("racket_hit".to_string()).ok();
233
234 let relative_collision_point: f32 = pong_ball_transform.position.y - racket_transform.position.y;
235 let rebound_angle: f32 = relative_collision_point * 1.0 + random_factor;
236
237 let pong_ball_new_velocity: Vector2<f32>;
238
239 if racket_transform.position.x > 0.0 {
240 pong_ball_new_velocity = Vector2::new(-1.0, rebound_angle).normalize() * pong_ball_velocity.to_vec().magnitude();
241 pong_ball_velocity.x = pong_ball_new_velocity.x; pong_ball_velocity.y = pong_ball_new_velocity.y;
242 pong_ball_transform.position.x -= 0.1;
243 } else if racket_transform.position.x < 0.0 {
244 pong_ball_new_velocity = Vector2::new(1.0, rebound_angle).normalize() * pong_ball_velocity.to_vec().magnitude();
245 pong_ball_velocity.x = pong_ball_new_velocity.x; pong_ball_velocity.y = pong_ball_new_velocity.y;
246 pong_ball_transform.position.x += 0.1;
247 }
248 let new_position: Vector2<f32> = Vector2::new(pong_ball_transform.position.x, pong_ball_transform.position.y);
249 pong_ball_transform.set_position(&context.render_state, new_position);
250 }
251 }
252}
253
254fn check_borders_ball_collision(context: &mut Context, pong_ball: &Entity, random_factor: f32) {
255 let mut border_query: Query = Query::new(&context.world).with::<Border>();
256 let borders: Vec<Entity> = border_query.entities_with_components().unwrap();
257
258 for border in &borders {
259 let border_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(border).unwrap();
260 let border_transform: ComponentRef<'_, Transform> = context.world.get_entity_component::<Transform>(border).unwrap();
261
262 let pong_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&pong_ball).unwrap();
263 let mut pong_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&pong_ball).unwrap();
264 let mut pong_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&pong_ball).unwrap();
265
266 let pong_ball_new_velocity: Vector2<f32>;
267
268 if Collision::check(CollisionAlgorithm::Aabb, &border_collision, &pong_ball_collision) {
269 if border_transform.position.y > 0.0 {
270 pong_ball_new_velocity = Vector2::new(pong_ball_velocity.x.signum(), -1.0 + random_factor).normalize() * pong_ball_velocity.to_vec().magnitude();
271 pong_ball_velocity.x = pong_ball_new_velocity.x; pong_ball_velocity.y = pong_ball_new_velocity.y;
272 pong_ball_transform.position.y -= 0.1;
273 } else if border_transform.position.y < 0.0 {
274 pong_ball_new_velocity = Vector2::new(pong_ball_velocity.x.signum(), 1.0 + random_factor).normalize() * pong_ball_velocity.to_vec().magnitude();
275 pong_ball_velocity.x = pong_ball_new_velocity.x; pong_ball_velocity.y = pong_ball_new_velocity.y;
276 pong_ball_transform.position.y += 0.1;
277 }
278 let new_position: Vector2<f32> = Vector2::new(pong_ball_transform.position.x, pong_ball_transform.position.y);
279 pong_ball_transform.set_position(&context.render_state, new_position);
280 }
281 }
282}
283
284fn respawn_pong_ball_after_outbounds(context: &mut Context, pong_ball: &Entity) {
285 let mut pong_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(pong_ball).unwrap();
286 let position_default: Vector2<f32> = Vector2::new(0.0, 0.0);
287
288 if pong_ball_transform.position.x > 2.0 || pong_ball_transform.position.x < -2.0 {
289 let mut pong_ball_respawn_timer: ResourceRefMut<'_, PongBallRespawnTimer> = context.world.get_resource_mut::<PongBallRespawnTimer>().unwrap();
290 pong_ball_respawn_timer.0.tick(context.delta);
291
292 if pong_ball_respawn_timer.0.is_finished() {
293 pong_ball_transform.set_position(&context.render_state, position_default);
294 }
295 }
296}Sourcepub fn set_position_x(&mut self, render_state: &RenderState, x: f32)
pub fn set_position_x(&mut self, render_state: &RenderState, x: f32)
Set the curretn position x and sends it to the buffer.
Examples found in repository?
227fn move_player(context: &mut Context, keyboard_input: KeyboardInput, player_entity: Entity) {
228 let mut player_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut(&player_entity).unwrap();
229 let player_velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component(&player_entity).unwrap();
230
231 if keyboard_input.is_key_pressed(KeyboardKey::ArrowRight) {
232 let x: f32 = player_transform.position.x + player_velocity.x * context.delta;
233 player_transform.set_position_x(&context.render_state, x);
234 } else if keyboard_input.is_key_pressed(KeyboardKey::ArrowLeft) {
235 let x: f32 = player_transform.position.x - player_velocity.x * context.delta;
236 player_transform.set_position_x(&context.render_state, x);
237 }
238}More examples
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}79fn move_player(context: &mut Context, keyboard_input: KeyboardInput) {
80 let mut query: Query = Query::new(&context.world).with::<Animation>();
81 let result: Entity = query.entities_with_components().unwrap().first().unwrap().clone();
82 let mut animation: ComponentRefMut<'_, Animation> = context.world.get_entity_component_mut::<Animation>(&result).unwrap();
83 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&result).unwrap();
84 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&result).unwrap();
85
86 if keyboard_input.is_key_pressed(KeyboardKey::KeyW) {
87 let y: f32 = transform.position.y + velocity.y * context.delta;
88 transform.set_position_y(&context.render_state, y);
89 } else if keyboard_input.is_key_pressed(KeyboardKey::KeyS) {
90 let y: f32 = transform.position.y - velocity.y * context.delta;
91 transform.set_position_y(&context.render_state, y);
92 } else if keyboard_input.is_key_pressed(KeyboardKey::KeyD) {
93 let x: f32 = transform.position.x + velocity.x * context.delta;
94 transform.set_position_x(&context.render_state, x);
95 } else if keyboard_input.is_key_pressed(KeyboardKey::KeyA) {
96 let x: f32 = transform.position.x - velocity.x * context.delta;
97 transform.set_position_x(&context.render_state, x);
98 }
99
100 if keyboard_input.is_some_of_keys_pressed(vec![KeyboardKey::KeyW, KeyboardKey::KeyS, KeyboardKey::KeyA, KeyboardKey::KeyD]) {
101 animation.play("walk".to_string());
102 }
103
104 if keyboard_input.is_some_of_keys_released(vec![KeyboardKey::KeyW, KeyboardKey::KeyS, KeyboardKey::KeyA, KeyboardKey::KeyD]) {
105 animation.stop("walk".to_string());
106 }
107}Sourcepub fn set_position_y(&mut self, render_state: &RenderState, y: f32)
pub fn set_position_y(&mut self, render_state: &RenderState, y: f32)
Set the curretn position y and sends it to the buffer.
Examples found in repository?
55fn check_table_object_collision(context: &mut Context) {
56 let mut table_query: Query = Query::new(&context.world).with::<RigidBody>();
57 let mut object_query: Query = Query::new(&context.world).with::<RigidBody>();
58
59 let table: Entity = table_query.entities_without_components().unwrap().first().unwrap().clone();
60 let object: Entity = object_query.entities_with_components().unwrap().first().unwrap().clone();
61
62 let table_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&table).unwrap();
63
64 let (object_collision, mut object_transform, mut object_velocity, mut object_rigid_body) = (
65 context.world.get_entity_component::<Collision>(&object).unwrap(),
66 context.world.get_entity_component_mut::<Transform>(&object).unwrap(),
67 context.world.get_entity_component_mut::<Velocity>(&object).unwrap(),
68 context.world.get_entity_component_mut::<RigidBody>(&object).unwrap()
69 );
70
71 if Collision::check(CollisionAlgorithm::Aabb, &table_collision, &object_collision) {
72 if object_velocity.y < -0.001 {
73 object_velocity.y = -object_velocity.y * object_rigid_body.restitution;
74 let y: f32 = object_transform.position.y + object_velocity.y * context.delta;
75 object_transform.set_position_y(&context.render_state, y);
76 } else {
77 object_velocity.y = 0.0;
78 object_rigid_body.rest = true;
79 }
80 }
81}More examples
79fn move_player(context: &mut Context, keyboard_input: KeyboardInput) {
80 let mut query: Query = Query::new(&context.world).with::<Animation>();
81 let result: Entity = query.entities_with_components().unwrap().first().unwrap().clone();
82 let mut animation: ComponentRefMut<'_, Animation> = context.world.get_entity_component_mut::<Animation>(&result).unwrap();
83 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&result).unwrap();
84 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&result).unwrap();
85
86 if keyboard_input.is_key_pressed(KeyboardKey::KeyW) {
87 let y: f32 = transform.position.y + velocity.y * context.delta;
88 transform.set_position_y(&context.render_state, y);
89 } else if keyboard_input.is_key_pressed(KeyboardKey::KeyS) {
90 let y: f32 = transform.position.y - velocity.y * context.delta;
91 transform.set_position_y(&context.render_state, y);
92 } else if keyboard_input.is_key_pressed(KeyboardKey::KeyD) {
93 let x: f32 = transform.position.x + velocity.x * context.delta;
94 transform.set_position_x(&context.render_state, x);
95 } else if keyboard_input.is_key_pressed(KeyboardKey::KeyA) {
96 let x: f32 = transform.position.x - velocity.x * context.delta;
97 transform.set_position_x(&context.render_state, x);
98 }
99
100 if keyboard_input.is_some_of_keys_pressed(vec![KeyboardKey::KeyW, KeyboardKey::KeyS, KeyboardKey::KeyA, KeyboardKey::KeyD]) {
101 animation.play("walk".to_string());
102 }
103
104 if keyboard_input.is_some_of_keys_released(vec![KeyboardKey::KeyW, KeyboardKey::KeyS, KeyboardKey::KeyA, KeyboardKey::KeyD]) {
105 animation.stop("walk".to_string());
106 }
107}Sourcepub fn set_position_pixelated(
&mut self,
render_state: &RenderState,
position: Vector2<f32>,
)
pub fn set_position_pixelated( &mut self, render_state: &RenderState, position: Vector2<f32>, )
Set the current position and sends it to the buffer.
Useful to set a brand new position using pixelated values.
Your pixelated coordinate will be normalized.
Sourcepub fn set_position_x_pixelated(&mut self, render_state: &RenderState, x: f32)
pub fn set_position_x_pixelated(&mut self, render_state: &RenderState, x: f32)
Set the curretn position x and sends it to the buffer.
Useful to set a brand new position x using pixelated values.
Your pixelated coordinate will be normalized.
Sourcepub fn set_position_y_pixelated(&mut self, render_state: &RenderState, y: f32)
pub fn set_position_y_pixelated(&mut self, render_state: &RenderState, y: f32)
Set the curretn position y and sends it to the buffer.
Useful to set a brand new position y using pixelated values.
Your pixelated coordinate will be normalized.
Sourcepub fn get_position(&self) -> Vector2<f32>
pub fn get_position(&self) -> Vector2<f32>
Get the current position.
Sourcepub fn set_rotation(&mut self, render_state: &RenderState, rotation: f32)
pub fn set_rotation(&mut self, render_state: &RenderState, rotation: f32)
Set the current rotation and sends it to the buffer.
Examples found in repository?
84fn update(context: &mut Context) {
85 context.commands.show_fps(context.game_loop_listener.current_fps, Color::by_option(ColorOption::Black));
86
87 let mut query: Query = Query::new(&context.world).with::<Shape>();
88 let entities: Vec<Entity> = query.entities_with_components().unwrap();
89
90 for entity in &entities {
91 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(entity).unwrap();
92 let rotation: f32 = transform.get_rotation() + 100.0 * context.delta;
93 transform.set_rotation(&context.render_state, rotation);
94 }
95}Sourcepub fn get_rotation(&self) -> f32
pub fn get_rotation(&self) -> f32
Get the current rotation.
Examples found in repository?
84fn update(context: &mut Context) {
85 context.commands.show_fps(context.game_loop_listener.current_fps, Color::by_option(ColorOption::Black));
86
87 let mut query: Query = Query::new(&context.world).with::<Shape>();
88 let entities: Vec<Entity> = query.entities_with_components().unwrap();
89
90 for entity in &entities {
91 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(entity).unwrap();
92 let rotation: f32 = transform.get_rotation() + 100.0 * context.delta;
93 transform.set_rotation(&context.render_state, rotation);
94 }
95}Sourcepub fn set_scale(&mut self, render_state: &RenderState, scale: Vector2<f32>)
pub fn set_scale(&mut self, render_state: &RenderState, scale: Vector2<f32>)
Set the current scale and sends it to the buffer.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Transform
impl RefUnwindSafe for Transform
impl Send for Transform
impl Sync for Transform
impl Unpin for Transform
impl UnsafeUnpin for Transform
impl UnwindSafe for Transform
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().