Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into Trans-Updates
Browse files Browse the repository at this point in the history
# Conflicts:
#	docs/CHANGELOG.md
  • Loading branch information
mrhatman committed Dec 26, 2019
2 parents 1532be5 + 7bebf48 commit 0f1e39e
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 112 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,24 @@ Please visit the [features page][feat] for a list of features Amethyst provides.

## Navigation

* [**Link to the book (0.11)**][bks11]
* [**Link to the book (0.10)**][bks10]
* [**Link to the book (0.13)**][bkstable]
* [**Link to the book (0.12)**][bks12]
* [**Link to the book (master)**][bkm]
* [**Link to the examples (0.11)**][exr11]
* [**Link to the examples (0.10)**][exr10]
* [**Link to the examples (0.13)**][exr13]
* [**Link to the examples (0.12)**][exr12]
* [**Link to the examples (master)**][exm]

## Usage

While the engine can be hard to use at times, we made a lot of [documentation][bks11] that will teach you everything you need to use Amethyst comfortably.
While the engine can be hard to use at times, we made a lot of [documentation][bkstable] that will teach you everything you need to use Amethyst comfortably.

If you don't understand a part of the documentation, please let us know. Join us on Discord or open an issue; we are always happy to help!

[bks11]: https://book.amethyst.rs/stable/
[bks10]: https://book.amethyst.rs/v0.10.0/
[bkstable]: https://book.amethyst.rs/stable/
[bks12]: https://book.amethyst.rs/v0.12.0/
[bkm]: https://book.amethyst.rs/master/
[exr11]: https://github.com/amethyst/amethyst/tree/v0.11.0/examples
[exr10]: https://github.com/amethyst/amethyst/tree/v0.10.0/examples
[exr13]: https://github.com/amethyst/amethyst/tree/v0.13.2/examples
[exr12]: https://github.com/amethyst/amethyst/tree/v0.12.0/examples
[exm]: https://github.com/amethyst/amethyst/tree/master/examples

## Getting started
Expand Down
13 changes: 12 additions & 1 deletion amethyst_input/src/axis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use super::{Button, ControllerAxis};
use super::{Button, ControllerAxis, MouseAxis};

/// Represents any input represented by a float value from -1 to 1.
/// Retrieve the value of this with [axis_value](struct.InputHandler.html#method.axis_value).
Expand All @@ -26,6 +26,17 @@ pub enum Axis {
/// linearly interpolate remaining ranges.
dead_zone: f64,
},
/// Represents a mouse as a 2D input device
Mouse {
/// The axis being bound
axis: MouseAxis,
/// Should the API be allowed to return values outside [-1..1]?
over_extendable: bool,
/// Zone to which the movement is relative on the X axis
radius_x: f32,
/// Zone to which the movement is relative on the Y axis
radius_y: f32,
},
/// Represents the wheel on a PC mouse.
MouseWheel {
/// If this value is true then this axis is for the horizontal mouse wheel rather than the vertical mouse wheel.
Expand Down
37 changes: 27 additions & 10 deletions amethyst_input/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,23 @@ pub struct Bindings<T: BindingTypes> {
#[derive(Clone, Derivative)]
#[derivative(Debug(bound = ""))]
pub enum BindingError<T: BindingTypes> {
/// You attempted to bind a mousewheel axis twice.
MouseWheelAxisAlreadyBound(T::Axis),
/// Combo provided for action binding has two (or more) of the same button.
ComboContainsDuplicates(T::Action),
/// Combo provided was already bound to the contained action.
ComboAlreadyBound(T::Action),
/// A combo of length one was provided, and it overlaps with an axis binding.
ButtonBoundToAxis(T::Axis, Axis),
/// Axis buttons provided have overlap with an existing axis.
AxisButtonAlreadyBoundToAxis(T::Axis, Axis),
/// Axis buttons have overlap with an action combo of length one.
AxisButtonAlreadyBoundToAction(T::Action, Button),
/// Axis buttons provided have overlap with an existing axis.
AxisButtonAlreadyBoundToAxis(T::Axis, Axis),
/// A combo of length one was provided, and it overlaps with an axis binding.
ButtonBoundToAxis(T::Axis, Axis),
/// Combo provided was already bound to the contained action.
ComboAlreadyBound(T::Action),
/// Combo provided for action binding has two (or more) of the same button.
ComboContainsDuplicates(T::Action),
/// That specific axis on that specific controller is already in use for an
/// axis binding.
ControllerAxisAlreadyBound(T::Axis),
/// The given axis was already bound for use
MouseAxisAlreadyBound(T::Axis),
/// You attempted to bind a mousewheel axis twice.
MouseWheelAxisAlreadyBound(T::Axis),
}

impl<T: BindingTypes> PartialEq for BindingError<T> {
Expand Down Expand Up @@ -205,6 +207,9 @@ where
BindingError::ControllerAxisAlreadyBound(ref id) => {
write!(f, "Controller axis provided is already in use by {}", id)
}
BindingError::MouseAxisAlreadyBound(ref id) => {
write!(f, "Mouse axis provided is already in use by {}", id)
}
BindingError::MouseWheelAxisAlreadyBound(ref id) => {
write!(f, "Mouse wheel axis provided is already in use by {}", id)
}
Expand Down Expand Up @@ -485,6 +490,18 @@ impl<T: BindingTypes> Bindings<T> {
}
}
}
Axis::Mouse { axis, .. } => {
for (k, a) in self.axes.iter().filter(|(k, _a)| *k != id) {
if let Axis::Mouse {
axis: mouse_axis, ..
} = a
{
if axis == mouse_axis {
return Err(BindingError::MouseAxisAlreadyBound(k.clone()));
}
}
}
}
Axis::MouseWheel {
horizontal: ref input_horizontal,
} => {
Expand Down
31 changes: 31 additions & 0 deletions amethyst_input/src/input_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ where
/// First number represents mapped ID visible to the user code,
/// while second is the ID used by incoming events.
connected_controllers: SmallVec<[(u32, u32); 8]>,
mouse_last_position: Option<(f32, f32)>,
mouse_position: Option<(f32, f32)>,
mouse_wheel_vertical: f32,
mouse_wheel_horizontal: f32,
Expand Down Expand Up @@ -404,6 +405,7 @@ where
pub fn send_frame_begin(&mut self) {
self.mouse_wheel_vertical = 0.0;
self.mouse_wheel_horizontal = 0.0;
self.mouse_last_position = self.mouse_position.clone();
}

/// Returns an iterator over all keys that are down.
Expand Down Expand Up @@ -552,6 +554,35 @@ where
}
})
.unwrap_or(0.0),
Axis::Mouse {
axis,
over_extendable,
radius_x,
radius_y,
} => {
let current_pos = self.mouse_position.unwrap_or((0., 0.));
let last_pos = self.mouse_last_position.unwrap_or(current_pos);
let delta = match axis {
// These calculations have to be inverses in order to point into the right direction of movement
MouseAxis::X => last_pos.0 - current_pos.0,
MouseAxis::Y => last_pos.1 - current_pos.1,
};

let rel_delta = match axis {
MouseAxis::X => delta / radius_x,
MouseAxis::Y => delta / radius_y,
};

if over_extendable {
rel_delta
} else if rel_delta > 1. {
1.
} else if rel_delta < -1. {
-1.
} else {
rel_delta
}
}
Axis::MouseWheel { horizontal } => self.mouse_wheel_value(horizontal),
})
}
Expand Down
2 changes: 2 additions & 0 deletions amethyst_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use self::{
controller::{ControllerAxis, ControllerButton, ControllerEvent},
event::InputEvent,
input_handler::InputHandler,
mouse::MouseAxis,
scroll_direction::ScrollDirection,
system::{InputSystem, InputSystemDesc},
util::{
Expand All @@ -39,6 +40,7 @@ mod button;
mod controller;
mod event;
mod input_handler;
mod mouse;
mod scroll_direction;
mod system;
mod util;
Expand Down
10 changes: 10 additions & 0 deletions amethyst_input/src/mouse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};

/// Mouse axis
#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum MouseAxis {
/// The X axis represents moving the mouse left / right
X,
/// The Y axis represents the mouse moving up / down
Y,
}
9 changes: 8 additions & 1 deletion amethyst_network/src/simulation/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::simulation::Message;
use bytes::Bytes;
use std::net::SocketAddr;
use std::{io, net::SocketAddr};

/// Events which can be received from the network.
#[derive(Debug)]
Expand All @@ -10,4 +11,10 @@ pub enum NetworkSimulationEvent {
Connect(SocketAddr),
// A host has disconnected from us
Disconnect(SocketAddr),
// An error occurred while receiving a message.
RecvError(io::Error),
// An error occurred while sending a message.
SendError(io::Error, Message),
// An error occurred while managing connections.
ConnectionError(io::Error, Option<SocketAddr>),
}
8 changes: 4 additions & 4 deletions amethyst_network/src/simulation/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use std::net::SocketAddr;
#[derive(Debug, PartialEq, Eq)]
pub struct Message {
/// The destination to send the message.
pub(crate) destination: SocketAddr,
pub destination: SocketAddr,
/// The serialized payload itself.
pub(crate) payload: Bytes,
pub payload: Bytes,
/// The requirement around whether or not this message should be resent if lost.
pub(crate) delivery: DeliveryRequirement,
pub delivery: DeliveryRequirement,
/// The requirement around when this message should be sent.
pub(crate) urgency: UrgencyRequirement,
pub urgency: UrgencyRequirement,
}

impl Message {
Expand Down
17 changes: 12 additions & 5 deletions amethyst_network/src/simulation/transport/laminar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use amethyst_core::{
shrev::EventChannel,
};
use amethyst_error::Error;
pub use laminar::{Config as LaminarConfig, Socket as LaminarSocket};
pub use laminar::{Config as LaminarConfig, ErrorKind, Socket as LaminarSocket};
use laminar::{Packet, SocketEvent};

use bytes::Bytes;
Expand Down Expand Up @@ -67,13 +67,14 @@ impl<'s> System<'s> for LaminarNetworkSendSystem {
Write<'s, TransportResource>,
Write<'s, LaminarSocketResource>,
Read<'s, NetworkSimulationTime>,
Write<'s, EventChannel<NetworkSimulationEvent>>,
);

fn run(&mut self, (mut transport, mut socket, sim_time): Self::SystemData) {
fn run(&mut self, (mut transport, mut socket, sim_time, mut event_channel): Self::SystemData) {
if let Some(socket) = socket.get_mut() {
let messages = transport.drain_messages_to_send(|_| sim_time.should_send_message_now());

for message in messages.iter() {
for message in messages {
let packet = match message.delivery {
DeliveryRequirement::Unreliable => {
Packet::unreliable(message.destination, message.payload.to_vec())
Expand Down Expand Up @@ -107,8 +108,14 @@ impl<'s> System<'s> for LaminarNetworkSendSystem {
),
};

if let Err(e) = socket.send(packet) {
error!("There was an error when attempting to send packet: {:?}", e);
match socket.send(packet) {
Err(ErrorKind::IOError(e)) => {
event_channel.single_write(NetworkSimulationEvent::SendError(e, message));
}
Err(e) => {
error!("Error sending message: {:?}", e);
}
Ok(_) => {}
}
}
}
Expand Down
39 changes: 22 additions & 17 deletions amethyst_network/src/simulation/transport/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
use crate::simulation::{
events::NetworkSimulationEvent,
message::Message,
requirements::DeliveryRequirement,
timing::{NetworkSimulationTime, NetworkSimulationTimeSystem},
transport::{
TransportResource, NETWORK_RECV_SYSTEM_NAME, NETWORK_SEND_SYSTEM_NAME,
NETWORK_SIM_TIME_SYSTEM_NAME,
},
Message,
};
use amethyst_core::{
bundle::SystemBundle,
Expand All @@ -17,7 +17,7 @@ use amethyst_core::{
};
use amethyst_error::Error;
use bytes::Bytes;
use log::{error, warn};
use log::warn;
use std::{
collections::HashMap,
io::{self, Read as IORead, Write as IOWrite},
Expand Down Expand Up @@ -97,10 +97,10 @@ impl<'s> System<'s> for TcpStreamManagementSystem {
let s = match TcpStream::connect(message.destination) {
Ok(s) => s,
Err(e) => {
warn!(
"Error attempting to connection to {}: {:?}",
message.destination, e
);
event_channel.single_write(NetworkSimulationEvent::ConnectionError(
e,
Some(message.destination),
));
return;
}
};
Expand Down Expand Up @@ -146,7 +146,8 @@ impl<'s> System<'s> for TcpConnectionListenerSystem {
break;
}
Err(e) => {
error!("Error listening for connections: {}", e);
event_channel
.single_write(NetworkSimulationEvent::ConnectionError(e, None));
break;
}
};
Expand All @@ -163,18 +164,19 @@ impl<'s> System<'s> for TcpNetworkSendSystem {
Write<'s, TransportResource>,
Write<'s, TcpNetworkResource>,
Read<'s, NetworkSimulationTime>,
Write<'s, EventChannel<NetworkSimulationEvent>>,
);

fn run(&mut self, (mut transport, mut net, sim_time): Self::SystemData) {
fn run(&mut self, (mut transport, mut net, sim_time, mut channel): Self::SystemData) {
let messages = transport.drain_messages_to_send(|_| sim_time.should_send_message_now());
for message in messages.iter() {
for message in messages {
match message.delivery {
DeliveryRequirement::ReliableOrdered(Some(_)) => {
warn!("Streams are not supported by TCP and will be ignored.");
write_message(message, &mut net);
write_message(message, &mut net, &mut channel);
}
DeliveryRequirement::ReliableOrdered(_) | DeliveryRequirement::Default => {
write_message(message, &mut net);
write_message(message, &mut net, &mut channel);
}
delivery => panic!(
"{:?} is unsupported. TCP only supports ReliableOrdered by design.",
Expand All @@ -185,13 +187,14 @@ impl<'s> System<'s> for TcpNetworkSendSystem {
}
}

fn write_message(message: &Message, net: &mut TcpNetworkResource) {
fn write_message(
message: Message,
net: &mut TcpNetworkResource,
channel: &mut EventChannel<NetworkSimulationEvent>,
) {
if let Some((_, stream)) = net.get_stream(message.destination) {
if let Err(e) = stream.write(&message.payload) {
error!(
"There was an error when attempting to send message: {:?}",
e
);
channel.single_write(NetworkSimulationEvent::SendError(e, message));
}
}
}
Expand Down Expand Up @@ -239,7 +242,9 @@ impl<'s> System<'s> for TcpNetworkRecvSystem {
*active = false;
}
io::ErrorKind::WouldBlock => {}
_ => error!("Encountered an error receiving data: {:?}", e),
_ => {
event_channel.single_write(NetworkSimulationEvent::RecvError(e));
}
}
break;
}
Expand Down
Loading

0 comments on commit 0f1e39e

Please sign in to comment.