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::BLACK);
16 let object: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(Circle::default()), Color::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::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::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::RED);
23 let blue_object: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(circle.clone()), Color::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::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::BLUE);
30 let my_rectangle: Shape = Shape::new(Orientation::Horizontal, GeometryType::Rectangle, Color::GREEN);
31 let my_triangle: Shape = Shape::new(Orientation::Horizontal, GeometryType::Triangle, Color::RED);
32 let my_circle: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(Circle::new(64, 0.5)), Color::BLACK);
33
34 context.commands.spawn(
35 vec![
36 Box::new(my_square),
37 Box::new(Transform::new(
38 Position::new(Vector2::new(-0.60, -0.25), Strategy::Normalized),
39 0.0,
40 Vector2::new(0.10, 0.10)
41 )),
42 Box::new(MySquare())
43 ]
44 );
45 context.commands.spawn(
46 vec![
47 Box::new(my_rectangle),
48 Box::new(Transform::new(
49 Position::new(Vector2::new(-0.35, 0.20), Strategy::Normalized),
50 0.0,
51 Vector2::new(0.50, 0.50)
52 )),
53 Box::new(MyRectangle())
54 ]
55 );
56 context.commands.spawn(
57 vec![
58 Box::new(my_triangle),
59 Box::new(Transform::new(
60 Position::new(Vector2::new(0.50, 0.50), Strategy::Normalized),
61 0.0,
62 Vector2::new(0.25, 0.25)
63 )),
64 Box::new(MyTriangle())
65 ]
66 );
67 context.commands.spawn(
68 vec![
69 Box::new(my_circle),
70 Box::new(Transform::new(
71 Position::new(Vector2::new(0.80, 0.50), Strategy::Normalized),
72 0.0,
73 Vector2::new(0.25, 0.25)
74 )),
75 Box::new(MyCircle())
76 ]
77 );
78}
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::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::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::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::PURPLE);
69 let little_ball: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(Circle::new(64, 0.2)), Color::BLACK);
70 let start_text: Text = Text::new(
71 &mut context.render_state,
72 Font::new(Fonts::RobotoMonoItalic.get_path(), 40.0),
73 Position::new(Vector2::new(298.0, 380.0), Strategy::Pixelated),
74 Color::BLACK,
75 "> enter <".to_string()
76 );
77
78 let mut thread_rng: ThreadRng = rand::rng();
79 let random_direction: bool = thread_rng.random_bool(1.0 / 3.0);
80
81 let velocity_x: f32 = if random_direction {
82 1.2
83 } else {
84 -1.2
85 };
86
87 context.commands.add_resources(vec![
88 Box::new(LittleBallRespawnTimer::new()),
89 Box::new(NextState::default())
90 ]);
91 context.commands.spawn(vec![Box::new(start_text)]);
92
93 context.commands.spawn(
94 vec![
95 Box::new(player),
96 Box::new(Player()),
97 Box::new(Transform::new(
98 Position::new(Vector2::new(0.0, -0.85), Strategy::Normalized),
99 0.0,
100 Vector2::new(0.15, 0.10)
101 )),
102 Box::new(Velocity::new(Vector2::new(2.0, 2.0))),
103 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
104 ]
105 );
106
107 context.commands.spawn(
108 vec![
109 Box::new(little_ball),
110 Box::new(LittleBall()),
111 Box::new(Transform::new(
112 Position::new(Vector2::new(0.0, -0.5), Strategy::Normalized),
113 0.0,
114 Vector2::new(0.10, 0.10)
115 )),
116 Box::new(Velocity::new(Vector2::new(velocity_x, -0.5))),
117 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
118 ]
119 );
120
121 spawn_border(context, Vector2::new(1.05, 0.0));
122 spawn_border(context, Vector2::new(-1.05, 0.0));
123 spawn_targets(context);
124}
125
126fn update(context: &mut Context) {
127 let input: Input = {
128 let input_ref: ResourceRefMut<'_, Input> = context.world.get_resource_mut::<Input>().unwrap();
129 input_ref.clone()
130 };
131 let is_hover: bool = input.mouse_position.x >= 298.0 && (input.mouse_position.y > 380.0 && input.mouse_position.y < 416.0);
132
133 if
134 input.is_key_released(KeyCode::Enter) ||
135 (input.is_mouse_button_released(MouseButton::Left) && is_hover)
136 {
137 let mut next_state: ResourceRefMut<'_, NextState> = context.world.get_resource_mut::<NextState>().unwrap();
138 next_state.0 = GameState::Running;
139
140 let mut query: Query = Query::new(&context.world).with::<Text>();
141 if let Some(entity) = query.entities_with_components().unwrap().first() {
142 context.commands.despawn(entity.clone());
143 }
144 }
145
146 if context.world.get_resource::<NextState>().unwrap().0 == GameState::Running {
147 let mut player_query: Query = Query::new(&context.world).with::<Player>();
148 let player_entity: Entity = player_query.entities_with_components().unwrap().first().unwrap().clone();
149
150 let mut little_ball_query: Query = Query::new(&context.world).with::<LittleBall>();
151 let little_ball_entity: Entity = little_ball_query.entities_with_components().unwrap().first().unwrap().clone();
152
153 let mut thread_rng: ThreadRng = rand::rng();
154 let random_factor: f32 = thread_rng.random_range(-0.5..0.5);
155
156 move_player(context, input, player_entity);
157 move_little_ball(context, little_ball_entity);
158 check_player_little_ball_collision(context, player_entity, little_ball_entity, random_factor);
159 check_little_ball_borders_collision(context, little_ball_entity, random_factor);
160 check_litte_ball_targets_collision(context, little_ball_entity, random_factor);
161 respawn_little_ball_after_outbounds(context, little_ball_entity);
162 }
163}
164
165fn spawn_border(context: &mut Context, position: Vector2<f32>) {
166 let border: Shape = Shape::new(Orientation::Vertical, GeometryType::Rectangle, Color::CYAN);
167
168 context.commands.spawn(
169 vec![
170 Box::new(border),
171 Box::new(Border()),
172 Box::new(Transform::new(
173 Position::new(position, Strategy::Normalized),
174 0.0,
175 Vector2::new(0.01, context.window_configuration.height as f32)
176 )),
177 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
178 ]
179 );
180}
181
182fn spawn_targets(context: &mut Context) {
183 let width: f32 = 0.15;
184 let height: f32 = 0.10;
185
186 let rows: i32 = 8;
187 let columns: i32 = 10;
188 let spacing_x: f32 = 0.09;
189 let spacing_y: f32 = 0.02;
190
191 let start_x: f32 = -(columns as f32 * (width + spacing_x)) / 2.0;
192 let start_y: f32 = 1.0 - 0.1;
193
194 for row in 0..rows {
195 for column in 0..columns {
196 let x: f32 = start_x + column as f32 * (width + spacing_x);
197 let y: f32 = start_y - row as f32 * (height + spacing_y);
198
199 let mut color: Color = Color::RED;
200
201 if row == 2 || row == 3 {
202 color = Color::ORANGE;
203 } else if row == 4 || row == 5 {
204 color = Color::GREEN;
205 } else if row == 6 || row == 7 {
206 color = Color::YELLOW;
207 }
208
209 context.commands.spawn(
210 vec![
211 Box::new(Shape::new(Orientation::Horizontal, GeometryType::Rectangle, color)),
212 Box::new(Target()),
213 Box::new(Transform::new(
214 Position::new(Vector2::new(x, y), Strategy::Normalized),
215 0.0,
216 Vector2::new(width, height)
217 )),
218 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle))),
219 ]
220 );
221 }
222 }
223}
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
238fn move_little_ball(context: &mut Context, little_ball_entity: Entity) {
239 let mut little_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
240 let little_ball_velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&little_ball_entity).unwrap();
241
242 let new_position: Vector2<f32> = little_ball_transform.position.to_vec() + little_ball_velocity.to_vec() * context.delta;
243 little_ball_transform.set_position(&context.render_state, new_position);
244}
245
246fn check_player_little_ball_collision(context: &mut Context, player_entity: Entity, little_ball_entity: Entity, random_factor: f32) {
247 let mut little_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
248 let mut little_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&little_ball_entity).unwrap();
249 let little_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&little_ball_entity).unwrap();
250
251 let player_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&player_entity).unwrap();
252
253 if Collision::check(CollisionAlgorithm::Aabb, &player_collision, &little_ball_collision) {
254 let velocity_magnitude: f32 = little_ball_velocity.to_vec().magnitude();
255 let collision_point: f32 = (
256 (little_ball_collision.collider.position.x - player_collision.collider.position.x) /
257 (player_collision.collider.scale.x * 0.5)
258 ).clamp(-1.0, 1.0);
259
260 let mut new_direction: Vector2<f32> = Vector2::new(
261 collision_point * 1.5,
262 1.0 - collision_point.abs() * 0.3
263 ).normalize();
264
265 new_direction.x += random_factor * 0.15;
266 new_direction = new_direction.normalize();
267
268 little_ball_velocity.x = new_direction.x * velocity_magnitude;
269 little_ball_velocity.y = new_direction.y * velocity_magnitude;
270 little_ball_transform.position.y += 0.03;
271 }
272}
273
274fn check_little_ball_borders_collision(context: &mut Context, little_ball_entity: Entity, random_factor: f32) {
275 let mut border_query: Query = Query::new(&context.world).with::<Border>();
276 let borders_entities: Vec<Entity> = border_query.entities_with_components().unwrap();
277
278 let mut little_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
279 let mut little_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&little_ball_entity).unwrap();
280 let little_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&little_ball_entity).unwrap();
281
282 for border in &borders_entities {
283 let border_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(border).unwrap();
284
285 if Collision::check(CollisionAlgorithm::Aabb, &little_ball_collision, &border_collision) {
286 let velocity_magnitude: f32 = little_ball_velocity.to_vec().magnitude();
287 let collision_normal: Vector2<f32> = if border_collision.collider.position.x > 0.0 {
288 Vector2::new(-1.0, 0.0)
289 } else {
290 Vector2::new(1.0, 0.0)
291 };
292
293 let new_direction: Vector2<f32> = (
294 little_ball_velocity.to_vec().normalize() - 2.0 *
295 little_ball_velocity.to_vec().normalize().dot(collision_normal) * collision_normal
296 ).normalize();
297
298 let randomized_direction: Vector2<f32> = Vector2::new(
299 new_direction.x + random_factor * 0.3,
300 new_direction.y
301 ).normalize();
302
303 little_ball_velocity.x = randomized_direction.x * velocity_magnitude;
304 little_ball_velocity.y = randomized_direction.y * velocity_magnitude;
305
306 let collision_offset: Vector2<f32> = collision_normal * 0.02;
307 little_ball_transform.position.x += collision_offset.x;
308 }
309 }
310}
311
312fn check_litte_ball_targets_collision(context: &mut Context, little_ball_entity: Entity, random_factor: f32) {
313 let mut targets_query: Query = Query::new(&context.world).with::<Target>();
314 let targets_entities: Vec<Entity> = targets_query.entities_with_components().unwrap();
315
316 let mut little_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
317 let mut little_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&little_ball_entity).unwrap();
318 let little_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&little_ball_entity).unwrap();
319
320 for target in &targets_entities {
321 let target_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(target).unwrap();
322
323 if Collision::check(CollisionAlgorithm::Aabb, &little_ball_collision, &target_collision) {
324 let velocity_magnitude: f32 = little_ball_velocity.to_vec().magnitude();
325 let impact_vector: Vector2<f32> = (target_collision.collider.position - little_ball_collision.collider.position).normalize();
326
327 let mut new_direction: Vector2<f32> = Vector2::new(
328 -impact_vector.x * 0.8 + random_factor * 0.2,
329 -impact_vector.y * 0.8 + random_factor * 0.2
330 ).normalize();
331
332 new_direction.y = new_direction.y.signum() * new_direction.y.abs().max(0.3);
333
334 little_ball_velocity.x = new_direction.x * velocity_magnitude;
335 little_ball_velocity.y = new_direction.y * velocity_magnitude;
336
337 little_ball_transform.position.x -= impact_vector.x * 0.05;
338 little_ball_transform.position.y -= impact_vector.y * 0.05;
339 context.commands.despawn(target.clone());
340 }
341 }
342}
343
344fn respawn_little_ball_after_outbounds(context: &mut Context, little_ball_entity: Entity) {
345 let mut litte_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&little_ball_entity).unwrap();
346 let position_default: Vector2<f32> = Vector2::new(0.0, -0.25);
347
348 if litte_ball_transform.position.y < -1.0 {
349 let mut little_ball_respawn_timer: ResourceRefMut<'_, LittleBallRespawnTimer> = context.world.get_resource_mut::<LittleBallRespawnTimer>().unwrap();
350 little_ball_respawn_timer.0.tick(context.delta);
351
352 if little_ball_respawn_timer.0.is_finished() {
353 litte_ball_transform.set_position(&context.render_state, position_default);
354 }
355 }
356}
175fn move_gray_racket(context: &mut Context, input: Input) {
176 let mut query: Query = Query::new(&context.world).with::<GrayRacket>();
177 let entities: Vec<Entity> = query.entities_with_components().unwrap();
178 let gray_racket_entity: &Entity = entities.first().unwrap();
179
180 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(gray_racket_entity).unwrap();
181 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(gray_racket_entity).unwrap();
182
183 if input.is_key_pressed(KeyCode::KeyW) {
184 transform.position.y += velocity.y * context.delta;
185 let new_position: Vector2<f32> = Vector2::new(transform.position.x, transform.position.y);
186 transform.set_position(&context.render_state, new_position);
187 } else if input.is_key_pressed(KeyCode::KeyS) {
188 transform.position.y -= velocity.y * context.delta;
189 let new_position: Vector2<f32> = Vector2::new(transform.position.x, transform.position.y);
190 transform.set_position(&context.render_state, new_position);
191 }
192}
193
194fn move_pink_racket(context: &mut Context, input: Input) {
195 let mut query: Query = Query::new(&context.world).with::<PinkRacket>();
196 let entities: Vec<Entity> = query.entities_with_components().unwrap();
197 let pink_racket_entity: &Entity = entities.first().unwrap();
198
199 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(pink_racket_entity).unwrap();
200 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(pink_racket_entity).unwrap();
201
202 if input.is_key_pressed(KeyCode::ArrowUp) {
203 transform.position.y += velocity.y * context.delta;
204 let new_position: Vector2<f32> = Vector2::new(transform.position.x, transform.position.y);
205 transform.set_position(&context.render_state, new_position);
206 } else if input.is_key_pressed(KeyCode::ArrowDown) {
207 transform.position.y -= velocity.y * context.delta;
208 let new_position: Vector2<f32> = Vector2::new(transform.position.x, transform.position.y);
209 transform.set_position(&context.render_state, new_position);
210 }
211}
212
213fn move_pong_ball(context: &mut Context, pong_ball: &Entity) {
214 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&pong_ball).unwrap();
215 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&pong_ball).unwrap();
216
217 let new_position: Vector2<f32> = transform.position.to_vec() + velocity.to_vec() * context.delta;
218 transform.set_position(&context.render_state, new_position);
219}
220
221fn check_rackets_ball_collision(context: &mut Context, pong_ball: &Entity, random_factor: f32) {
222 let mut racket_query: Query = Query::new(&context.world).with::<Racket>();
223 let rackets: Vec<Entity> = racket_query.entities_with_components().unwrap();
224 let mut game_audio: ResourceRefMut<'_, GameAudio> = context.world.get_resource_mut::<GameAudio>().unwrap();
225
226 for racket in &rackets {
227 let racket_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(racket).unwrap();
228 let racket_transform: ComponentRef<'_, Transform> = context.world.get_entity_component::<Transform>(racket).unwrap();
229
230 let pong_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&pong_ball).unwrap();
231 let mut pong_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&pong_ball).unwrap();
232 let mut pong_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&pong_ball).unwrap();
233
234 if Collision::check(CollisionAlgorithm::Aabb, &racket_collision, &pong_ball_collision) {
235 game_audio.0.play_static_sound("racket_hit".to_string()).ok();
236
237 let relative_collision_point: f32 = pong_ball_transform.position.y - racket_transform.position.y;
238 let rebound_angle: f32 = relative_collision_point * 1.0 + random_factor;
239
240 let pong_ball_new_velocity: Vector2<f32>;
241
242 if racket_transform.position.x > 0.0 {
243 pong_ball_new_velocity = Vector2::new(-1.0, rebound_angle).normalize() * pong_ball_velocity.to_vec().magnitude();
244 pong_ball_velocity.x = pong_ball_new_velocity.x; pong_ball_velocity.y = pong_ball_new_velocity.y;
245 pong_ball_transform.position.x -= 0.1;
246 } else if racket_transform.position.x < 0.0 {
247 pong_ball_new_velocity = Vector2::new(1.0, rebound_angle).normalize() * pong_ball_velocity.to_vec().magnitude();
248 pong_ball_velocity.x = pong_ball_new_velocity.x; pong_ball_velocity.y = pong_ball_new_velocity.y;
249 pong_ball_transform.position.x += 0.1;
250 }
251 let new_position: Vector2<f32> = Vector2::new(pong_ball_transform.position.x, pong_ball_transform.position.y);
252 pong_ball_transform.set_position(&context.render_state, new_position);
253 }
254 }
255}
256
257fn check_borders_ball_collision(context: &mut Context, pong_ball: &Entity, random_factor: f32) {
258 let mut border_query: Query = Query::new(&context.world).with::<Border>();
259 let borders: Vec<Entity> = border_query.entities_with_components().unwrap();
260
261 for border in &borders {
262 let border_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(border).unwrap();
263 let border_transform: ComponentRef<'_, Transform> = context.world.get_entity_component::<Transform>(border).unwrap();
264
265 let pong_ball_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&pong_ball).unwrap();
266 let mut pong_ball_velocity: ComponentRefMut<'_, Velocity> = context.world.get_entity_component_mut::<Velocity>(&pong_ball).unwrap();
267 let mut pong_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&pong_ball).unwrap();
268
269 let pong_ball_new_velocity: Vector2<f32>;
270
271 if Collision::check(CollisionAlgorithm::Aabb, &border_collision, &pong_ball_collision) {
272 if border_transform.position.y > 0.0 {
273 pong_ball_new_velocity = Vector2::new(pong_ball_velocity.x.signum(), -1.0 + random_factor).normalize() * pong_ball_velocity.to_vec().magnitude();
274 pong_ball_velocity.x = pong_ball_new_velocity.x; pong_ball_velocity.y = pong_ball_new_velocity.y;
275 pong_ball_transform.position.y -= 0.1;
276 } else if border_transform.position.y < 0.0 {
277 pong_ball_new_velocity = Vector2::new(pong_ball_velocity.x.signum(), 1.0 + random_factor).normalize() * pong_ball_velocity.to_vec().magnitude();
278 pong_ball_velocity.x = pong_ball_new_velocity.x; pong_ball_velocity.y = pong_ball_new_velocity.y;
279 pong_ball_transform.position.y += 0.1;
280 }
281 let new_position: Vector2<f32> = Vector2::new(pong_ball_transform.position.x, pong_ball_transform.position.y);
282 pong_ball_transform.set_position(&context.render_state, new_position);
283 }
284 }
285}
286
287fn respawn_pong_ball_after_outbounds(context: &mut Context, pong_ball: &Entity) {
288 let mut pong_ball_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(pong_ball).unwrap();
289 let position_default: Vector2<f32> = Vector2::new(0.0, 0.0);
290
291 if pong_ball_transform.position.x > 2.0 || pong_ball_transform.position.x < -2.0 {
292 let mut pong_ball_respawn_timer: ResourceRefMut<'_, PongBallRespawnTimer> = context.world.get_resource_mut::<PongBallRespawnTimer>().unwrap();
293 pong_ball_respawn_timer.0.tick(context.delta);
294
295 if pong_ball_respawn_timer.0.is_finished() {
296 pong_ball_transform.set_position(&context.render_state, position_default);
297 }
298 }
299}
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?
225fn move_player(context: &mut Context, input: Input, player_entity: Entity) {
226 let mut player_transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut(&player_entity).unwrap();
227 let player_velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component(&player_entity).unwrap();
228
229 if input.is_key_pressed(KeyCode::ArrowRight) {
230 let x: f32 = player_transform.position.x + player_velocity.x * context.delta;
231 player_transform.set_position_x(&context.render_state, x);
232 } else if input.is_key_pressed(KeyCode::ArrowLeft) {
233 let x: f32 = player_transform.position.x - player_velocity.x * context.delta;
234 player_transform.set_position_x(&context.render_state, x);
235 }
236}
More examples
66fn update(context: &mut Context) {
67 let input: ResourceRef<'_, Input> = context.world.get_resource::<Input>().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 input.is_key_pressed(KeyCode::ArrowRight) {
79 let x: f32 = transform.position.x + velocity.x * context.delta;
80 transform.set_position_x(&context.render_state, x);
81 } else if input.is_key_pressed(KeyCode::ArrowLeft) {
82 let x: f32 = transform.position.x - velocity.x * context.delta;
83 transform.set_position_x(&context.render_state, x);
84 }
85}
74fn move_player(context: &mut Context, input: Input) {
75 let mut query: Query = Query::new(&context.world).with::<Animation>();
76 let result: Entity = query.entities_with_components().unwrap().first().unwrap().clone();
77 let mut animation: ComponentRefMut<'_, Animation> = context.world.get_entity_component_mut::<Animation>(&result).unwrap();
78 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&result).unwrap();
79 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&result).unwrap();
80
81 if input.is_key_pressed(KeyCode::KeyW) {
82 let y: f32 = transform.position.y + velocity.y * context.delta;
83 transform.set_position_y(&context.render_state, y);
84 } else if input.is_key_pressed(KeyCode::KeyS) {
85 let y: f32 = transform.position.y - velocity.y * context.delta;
86 transform.set_position_y(&context.render_state, y);
87 } else if input.is_key_pressed(KeyCode::KeyD) {
88 let x: f32 = transform.position.x + velocity.x * context.delta;
89 transform.set_position_x(&context.render_state, x);
90 } else if input.is_key_pressed(KeyCode::KeyA) {
91 let x: f32 = transform.position.x - velocity.x * context.delta;
92 transform.set_position_x(&context.render_state, x);
93 }
94
95 if input.is_some_of_keys_pressed(vec![KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD]) {
96 animation.play("walk".to_string());
97 }
98
99 if input.is_some_of_keys_released(vec![KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD]) {
100 animation.stop("walk".to_string());
101 }
102}
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?
58fn check_table_object_collision(context: &mut Context) {
59 let mut table_query: Query = Query::new(&context.world).with::<RigidBody>();
60 let mut object_query: Query = Query::new(&context.world).with::<RigidBody>();
61
62 let table: Entity = table_query.entities_without_components().unwrap().first().unwrap().clone();
63 let object: Entity = object_query.entities_with_components().unwrap().first().unwrap().clone();
64
65 let table_collision: ComponentRef<'_, Collision> = context.world.get_entity_component::<Collision>(&table).unwrap();
66
67 let (object_collision, mut object_transform, mut object_velocity, mut object_rigid_body) = (
68 context.world.get_entity_component::<Collision>(&object).unwrap(),
69 context.world.get_entity_component_mut::<Transform>(&object).unwrap(),
70 context.world.get_entity_component_mut::<Velocity>(&object).unwrap(),
71 context.world.get_entity_component_mut::<RigidBody>(&object).unwrap()
72 );
73
74 if Collision::check(CollisionAlgorithm::Aabb, &table_collision, &object_collision) {
75 if object_velocity.y < -0.001 {
76 object_velocity.y = -object_velocity.y * object_rigid_body.restitution;
77 let y: f32 = object_transform.position.y + object_velocity.y * context.delta;
78 object_transform.set_position_y(&context.render_state, y);
79 } else {
80 object_velocity.y = 0.0;
81 object_rigid_body.rest = true;
82 }
83 }
84}
More examples
74fn move_player(context: &mut Context, input: Input) {
75 let mut query: Query = Query::new(&context.world).with::<Animation>();
76 let result: Entity = query.entities_with_components().unwrap().first().unwrap().clone();
77 let mut animation: ComponentRefMut<'_, Animation> = context.world.get_entity_component_mut::<Animation>(&result).unwrap();
78 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(&result).unwrap();
79 let velocity: ComponentRef<'_, Velocity> = context.world.get_entity_component::<Velocity>(&result).unwrap();
80
81 if input.is_key_pressed(KeyCode::KeyW) {
82 let y: f32 = transform.position.y + velocity.y * context.delta;
83 transform.set_position_y(&context.render_state, y);
84 } else if input.is_key_pressed(KeyCode::KeyS) {
85 let y: f32 = transform.position.y - velocity.y * context.delta;
86 transform.set_position_y(&context.render_state, y);
87 } else if input.is_key_pressed(KeyCode::KeyD) {
88 let x: f32 = transform.position.x + velocity.x * context.delta;
89 transform.set_position_x(&context.render_state, x);
90 } else if input.is_key_pressed(KeyCode::KeyA) {
91 let x: f32 = transform.position.x - velocity.x * context.delta;
92 transform.set_position_x(&context.render_state, x);
93 }
94
95 if input.is_some_of_keys_pressed(vec![KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD]) {
96 animation.play("walk".to_string());
97 }
98
99 if input.is_some_of_keys_released(vec![KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD]) {
100 animation.stop("walk".to_string());
101 }
102}
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?
80fn update(context: &mut Context) {
81 context.commands.show_fps(context.game_loop_listener.current_fps, Color::BLACK);
82
83 let mut query: Query = Query::new(&context.world).with::<Shape>();
84 let entities: Vec<Entity> = query.entities_with_components().unwrap();
85
86 for entity in &entities {
87 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(entity).unwrap();
88 let rotation: f32 = transform.get_rotation() + 100.0 * context.delta;
89 transform.set_rotation(&context.render_state, rotation);
90 }
91}
Sourcepub fn get_rotation(&self) -> f32
pub fn get_rotation(&self) -> f32
Get the current rotation.
Examples found in repository?
80fn update(context: &mut Context) {
81 context.commands.show_fps(context.game_loop_listener.current_fps, Color::BLACK);
82
83 let mut query: Query = Query::new(&context.world).with::<Shape>();
84 let entities: Vec<Entity> = query.entities_with_components().unwrap();
85
86 for entity in &entities {
87 let mut transform: ComponentRefMut<'_, Transform> = context.world.get_entity_component_mut::<Transform>(entity).unwrap();
88 let rotation: f32 = transform.get_rotation() + 100.0 * context.delta;
89 transform.set_rotation(&context.render_state, rotation);
90 }
91}
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 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()
.