pub struct Vec2<R: Real = f32> {
pub x: R,
pub y: R,
}Fields§
§x: R§y: RImplementations§
Source§impl<R: Real> Vec2<R>
impl<R: Real> Vec2<R>
Sourcepub fn new(x: R, y: R) -> Self
pub fn new(x: R, y: R) -> Self
Examples found in repository?
examples/example-pong.rs (line 48)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
fn new(state: &mut onyx::State<Action>, center: Vec2, size: Vec2, velocity: Vec2) -> Rect {
let transform = state.graph().add(center, Vec2::new(1.0, 1.0));
let child = state.graph().add_child(transform, -0.5 * size, size);
state.renderer().add(child);
Self { center, size, velocity, transform }
}
fn min(&self) -> Vec2 {
self.center - 0.5 * self.size
}
fn max(&self) -> Vec2 {
self.center + 0.5 * self.size
}
}
impl onyx::Scene for Scene {
type Action = Action;
fn new(state: &mut onyx::State<Action>) -> Self {
let size = state.context().size();
let plank_size = Vec2::new(10.0, 100.0);
let left_plank = Rect::new(state, 0.5 * Vec2::new(plank_size.x, size.y), plank_size, Vec2::zero());
let right_plank = Rect::new(state, Vec2::new(size.x - 0.5 * plank_size.x, 0.5 * size.y), plank_size, Vec2::zero());
let ball_size = Vec2::new(20.0, 20.0);
let ball = Rect::new(state, 0.5 * size, ball_size, Vec2::new(100.0, 100.0));
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Horizontal)
.alignment(onyx::ui::Alignment::Center, onyx::ui::Alignment::Min)
.child(onyx::ui::label("0").id("left"))
.child(onyx::ui::label(":"))
.child(onyx::ui::label("0").id("right"))
);
let mut keys = HashMap::new();
keys.insert(onyx::Key::W, (Direction::Up, Plank::Left));
keys.insert(onyx::Key::S, (Direction::Down, Plank::Left));
keys.insert(onyx::Key::Up, (Direction::Up, Plank::Right));
keys.insert(onyx::Key::Down, (Direction::Down, Plank::Right));
Scene {
left_plank,
right_plank,
ball,
left_score: 0,
right_score: 0,
keys,
}
}
fn event(&mut self, event: onyx::Event) -> Option<Action> {
match event {
onyx::Event::KeyPressed(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Start })
} else {
None
}
},
onyx::Event::KeyReleased(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Stop })
} else {
None
}
},
_ => None,
}
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Action>) -> onyx::Transition {
let plank = match action.plank {
Plank::Left => &mut self.left_plank,
Plank::Right => &mut self.right_plank,
};
let velocity = match action.direction {
Direction::Down => Vec2::new(0.0, 100.0),
Direction::Up => Vec2::new(0.0, -100.0),
};
match action.kind {
Kind::Start => plank.velocity += velocity,
Kind::Stop => plank.velocity -= velocity,
}
state.none()
}Source§impl<R: Real> Vec2<R>
impl<R: Real> Vec2<R>
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Examples found in repository?
examples/example-pong.rs (line 69)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
fn new(state: &mut onyx::State<Action>) -> Self {
let size = state.context().size();
let plank_size = Vec2::new(10.0, 100.0);
let left_plank = Rect::new(state, 0.5 * Vec2::new(plank_size.x, size.y), plank_size, Vec2::zero());
let right_plank = Rect::new(state, Vec2::new(size.x - 0.5 * plank_size.x, 0.5 * size.y), plank_size, Vec2::zero());
let ball_size = Vec2::new(20.0, 20.0);
let ball = Rect::new(state, 0.5 * size, ball_size, Vec2::new(100.0, 100.0));
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Horizontal)
.alignment(onyx::ui::Alignment::Center, onyx::ui::Alignment::Min)
.child(onyx::ui::label("0").id("left"))
.child(onyx::ui::label(":"))
.child(onyx::ui::label("0").id("right"))
);
let mut keys = HashMap::new();
keys.insert(onyx::Key::W, (Direction::Up, Plank::Left));
keys.insert(onyx::Key::S, (Direction::Down, Plank::Left));
keys.insert(onyx::Key::Up, (Direction::Up, Plank::Right));
keys.insert(onyx::Key::Down, (Direction::Down, Plank::Right));
Scene {
left_plank,
right_plank,
ball,
left_score: 0,
right_score: 0,
keys,
}
}
fn event(&mut self, event: onyx::Event) -> Option<Action> {
match event {
onyx::Event::KeyPressed(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Start })
} else {
None
}
},
onyx::Event::KeyReleased(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Stop })
} else {
None
}
},
_ => None,
}
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Action>) -> onyx::Transition {
let plank = match action.plank {
Plank::Left => &mut self.left_plank,
Plank::Right => &mut self.right_plank,
};
let velocity = match action.direction {
Direction::Down => Vec2::new(0.0, 100.0),
Direction::Up => Vec2::new(0.0, -100.0),
};
match action.kind {
Kind::Start => plank.velocity += velocity,
Kind::Stop => plank.velocity -= velocity,
}
state.none()
}
fn update(&mut self, state: &mut onyx::State<Action>) -> onyx::Transition {
let min = Vec2::zero();
let max = state.context().size();
self.ball.center += self.ball.velocity * state.context().dt();
self.left_plank.center += self.left_plank.velocity * state.context().dt();
self.right_plank.center += self.right_plank.velocity * state.context().dt();
if self.ball.max().x > max.x - self.right_plank.size.x && self.ball.max().x < max.x {
if self.ball.min().y < self.right_plank.max().y && self.ball.max().y > self.right_plank.min().y {
self.ball.center.x = max.x - self.right_plank.size.x - 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.min().x > max.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.left_score += 1;
state.ui().set_text("left", &self.left_score.to_string());
}
if self.ball.min().x < min.x + self.left_plank.size.x && self.ball.max().x > min.x {
if self.ball.min().y < self.left_plank.max().y && self.ball.max().y > self.left_plank.min().y {
self.ball.center.x = min.x + self.left_plank.size.x + 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.max().x < min.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.right_score += 1;
state.ui().set_text("right", &self.right_score.to_string());
}
if self.ball.min().y < min.y {
self.ball.center.y = min.y + 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
if self.ball.max().y > max.y {
self.ball.center.y = max.y - 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
state.graph().set_position(self.ball.transform, self.ball.center);
state.graph().set_position(self.left_plank.transform, self.left_plank.center);
state.graph().set_position(self.right_plank.transform, self.right_plank.center);
state.none()
}Trait Implementations§
Source§impl<R: Real> AddAssign for Vec2<R>
impl<R: Real> AddAssign for Vec2<R>
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Performs the
+= operation. Read moreSource§impl<'de, R> Deserialize<'de> for Vec2<R>where
R: Deserialize<'de> + Real,
impl<'de, R> Deserialize<'de> for Vec2<R>where
R: Deserialize<'de> + Real,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<R: Real> SubAssign for Vec2<R>
impl<R: Real> SubAssign for Vec2<R>
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Performs the
-= operation. Read moreimpl<R: Copy + Real> Copy for Vec2<R>
impl<R: Real> StructuralPartialEq for Vec2<R>
Auto Trait Implementations§
impl<R> Freeze for Vec2<R>where
R: Freeze,
impl<R> RefUnwindSafe for Vec2<R>where
R: RefUnwindSafe,
impl<R> Send for Vec2<R>where
R: Send,
impl<R> Sync for Vec2<R>where
R: Sync,
impl<R> Unpin for Vec2<R>where
R: Unpin,
impl<R> UnwindSafe for Vec2<R>where
R: UnwindSafe,
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit)Source§impl<T> Content for Twhere
T: Copy,
impl<T> Content for Twhere
T: Copy,
Source§fn read<F, E>(size: usize, f: F) -> Result<T, E>
fn read<F, E>(size: usize, f: F) -> Result<T, E>
Prepares an output buffer, then turns this buffer into an
Owned.Source§fn get_elements_size() -> usize
fn get_elements_size() -> usize
Returns the size of each element.
Source§fn to_void_ptr(&self) -> *const ()
fn to_void_ptr(&self) -> *const ()
Produces a pointer to the data.
Source§fn ref_from_ptr<'a>(ptr: *mut (), size: usize) -> Option<*mut T>
fn ref_from_ptr<'a>(ptr: *mut (), size: usize) -> Option<*mut T>
Builds a pointer to this type from a raw pointer.
Source§fn is_size_suitable(size: usize) -> bool
fn is_size_suitable(size: usize) -> bool
Returns true if the size is suitable to store a type like this.