Skip to content

Commit

Permalink
Merge branch 'network'
Browse files Browse the repository at this point in the history
  • Loading branch information
DomonkosSuranyi committed Mar 5, 2021
2 parents 32708db + 226799f commit beca125
Show file tree
Hide file tree
Showing 115 changed files with 3,779 additions and 1,202 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
*/target/
**/*.rs.bk
Cargo.lock

Expand Down
26 changes: 6 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,10 @@ name = "westiny"
version = "0.1.0"
authors = ["Domonkos Suranyi <[email protected]>"]
edition = "2018"
readme = "README.md"
license-file = "LICENSE"

[dependencies]
amethyst = "0.15.3"
amethyst_test = "0.15.3"
array-init = "1.0.0"
log = "0.4.11"
serde = "1.0.120"
bincode = "1.3.1"
derive-new = "0.5.8"
ron = "0.6.4"

[features]
default = ["vulkan", "tiles"]
empty = ["amethyst/empty"]
metal = ["amethyst/metal"]
vulkan = ["amethyst/vulkan"]
tiles = ["amethyst/tiles"]

[[bin]]
name = "westiny_server"
path = "src/server.rs"
[workspace]
members = [ ".", "server", "common", "client"]
# Add everything to default-members as well, e.g. cargo test will execute them as well.
default-members = [ ".", "server", "common", "client"]
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
![Westiny logo](media/westiny_logo.png)
# westiny
This is a top-down sandbox game.
This is a topview sandbox game.
The game is written fully in rust.

## usage
Run server:

### server
Run:
`cargo run --release --bin westiny_server`

Run client:
`cargo run --release --bin westiny`
### client
Specify server address on client:
`export WESTINY_SERVER_ADDRESS=1.2.3.4:5745`

Run:
`cargo run --release --bin westiny_client`

Or a one-liner:
`WESTINY_SERVER_ADDRESS=1.2.3.4:5745 cargo run --release --bin westiny_client`

21 changes: 21 additions & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "westiny_client"
version = "0.1.0"
authors = ["Abel Suranyi"]
edition = "2018"

[dependencies]
westiny_common = { path = "../common" }
amethyst = "0.15.3"
amethyst_test = "0.15.3"
serde = "1.0.120"
anyhow = "1.0.38"
derive-new = "0.5.9"
log = "0.4.14"

[features]
default = ["vulkan", "tiles"]
empty = ["amethyst/empty"]
metal = ["amethyst/metal"]
vulkan = ["amethyst/vulkan"]
tiles = ["amethyst/tiles"]
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ClientPort(
4557
0
)
File renamed without changes.
Binary file added client/assets/fonts/square.ttf
Binary file not shown.
File renamed without changes.
Binary file added client/assets/spritesheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion resources/spritesheet.ron → client/assets/spritesheet.ron
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ List((
y: 16,
width: 16,
height: 16,
)
),
// 7: corpse
(
x: 33,
y: 16,
width: 16,
height: 16,
),
],
))
40 changes: 40 additions & 0 deletions client/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fmt;

use ::serde::{Deserialize, Serialize};
use amethyst::input::BindingTypes;

#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum ActionBinding {
Forward,
Backward,
StrafeLeft,
StrafeRight,
Shoot,
Use,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum AxisBinding {
Zoom
}

impl fmt::Display for ActionBinding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl fmt::Display for AxisBinding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}


#[derive(Debug)]
pub struct MovementBindingTypes;

impl BindingTypes for MovementBindingTypes {
type Axis = AxisBinding;
type Action = ActionBinding;
}
4 changes: 1 addition & 3 deletions src/entities/mod.rs → client/src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub use player::initialize_player;
pub use tilemap::initialize_tilemap;
pub use bullet::spawn_bullet;

mod player;
mod tilemap;
mod bullet;
mod tilemap;
29 changes: 29 additions & 0 deletions client/src/entities/player.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use amethyst::core::math::Point2;
use amethyst::core::Transform;
use amethyst::prelude::*;
use log::info;

use westiny_common::components::{Input, Health, Player, NetworkId};
use crate::resources::SpriteResource;
use westiny_common::resources::SpriteId;

pub fn initialize_player<B: Builder>(builder: B,
sprite_resource: &SpriteResource,
network_id: NetworkId,
start_pos: Point2<f32>
) {

let mut transform = Transform::default();
transform.set_translation_xyz(start_pos.x, start_pos.y, 0.0);

builder
.with(network_id)
.with(sprite_resource.sprite_render_for(SpriteId::Player))
.with(transform)
.with(Player)
.with(Health(100))
.with(Input::default())
.build();

info!("Player created.");
}
2 changes: 1 addition & 1 deletion src/entities/tilemap.rs → client/src/entities/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn initialize_tilemap(
);

let mut transform = Transform::default();
transform.set_translation_xyz(position.x, position.y, 0.0);
transform.set_translation_xyz(position.x, position.y, -0.9);

let _map_entity = world
.create_entity()
Expand Down
61 changes: 41 additions & 20 deletions src/main.rs → client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ use amethyst::utils::application_root_dir;
use amethyst::{GameDataBuilder, CoreApplication};
use amethyst::core::TransformBundle;
use amethyst::renderer::{RenderingBundle, RenderToWindow, RenderFlat2D, types::DefaultBackend};
use amethyst::ui::{RenderUi, UiBundle};
use amethyst::tiles::{RenderTiles2D, MortonEncoder};
use amethyst::network::simulation::laminar::{LaminarSocket, LaminarNetworkBundle, LaminarConfig};
use std::time::Duration;
use crate::resources::ClientPort;
use crate::utilities::read_ron;
use amethyst::network::simulation::laminar::{LaminarSocket, LaminarNetworkBundle};
use std::net::{SocketAddr, IpAddr};
use std::str::FromStr;
use serde::Deserialize;
use amethyst::input::InputBundle;

use crate::resources::GroundTile;
use westiny_common::events::{WestinyEvent, WestinyEventReader};
use westiny_common::utilities::read_ron;
use westiny_common::NetworkConfig;

mod systems;
mod entities;
mod components;
mod resources;
mod entities;
mod states;
mod events;
mod network;
mod utilities;
mod bindings;

#[cfg(test)]
mod test_helpers;
Expand All @@ -27,7 +29,8 @@ fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default());

let app_root = application_root_dir()?;
let resources_dir = app_root.join("resources");
let common_resources_dir = app_root.join("../resources");
let resources_dir = app_root.join("assets");
let display_config = resources_dir.join("display_config.ron");

let client_port: u16 = {
Expand All @@ -43,35 +46,53 @@ fn main() -> amethyst::Result<()> {
client_port
}).0
};
let client_socket = SocketAddr::new(IpAddr::from_str("127.0.0.1")?, client_port);
let client_socket = SocketAddr::new(IpAddr::from_str("0.0.0.0")?, client_port);

let laminar_config = {
let mut conf = LaminarConfig::default();
// send heartbeat in every 3 seconds
conf.heartbeat_interval = Some(Duration::from_secs(3));
conf
let laminar_config= {
let ron_path = common_resources_dir.join("protocol.ron");
read_ron::<NetworkConfig>(&ron_path)
.map(|net_conf| net_conf.into())
.expect(&format!("Failed to load Laminar protocol configuration file: {}", ron_path.as_os_str().to_str().unwrap()))
};

let socket = LaminarSocket::bind_with_config(client_socket, laminar_config)?;
let key_bindings = resources_dir.join("input.ron");
let input_bundle = InputBundle::<bindings::MovementBindingTypes>::new().with_bindings_from_file(key_bindings)?;

let game_data = GameDataBuilder::default()
.with_bundle(TransformBundle::new())?
.with_bundle(input_bundle)?
.with_bundle(UiBundle::<bindings::MovementBindingTypes>::new())?
.with_bundle(RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config)?
.with_clear([0.0, 0.0, 0.0, 1.0])
)
.with_plugin(RenderFlat2D::default())
.with_plugin(RenderTiles2D::<resources::GroundTile, MortonEncoder>::default()))?
.with_plugin(RenderTiles2D::<GroundTile, MortonEncoder>::default())
.with_plugin(RenderUi::default())
)?
.with_bundle(LaminarNetworkBundle::new(Some(socket)))?
.with_bundle(AudioBundle::default())?;
.with_bundle(AudioBundle::default())?
;

let mut game =
CoreApplication::<_, events::WestinyEvent, events::WestinyEventReader>::build(
CoreApplication::<_, WestinyEvent, WestinyEventReader>::build(
&resources_dir,
states::connection::ConnectState::new(&resources_dir),
states::connection::ConnectState::new(&common_resources_dir),
)?.build(game_data)?;

log::info!("Starting client");
game.run();
Ok(())
}

const DEFAULT_CLIENT_PORT: u16 = 4557;

#[derive(Deserialize)]
pub struct ClientPort(pub u16);
impl Default for ClientPort {
fn default() -> Self {
ClientPort(DEFAULT_CLIENT_PORT)
}
}
12 changes: 8 additions & 4 deletions src/resources/audio.rs → client/src/resources/audio.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@

use amethyst::prelude::*;
use amethyst::ecs::World;
use amethyst::assets::Loader;
use amethyst::audio::{SourceHandle, OggFormat};

pub struct Sounds
{
pub single_shot: SourceHandle
}
pub handles: [SourceHandle; 1],

}

pub fn initialize_audio(world: &mut World)
{
let sounds = {
let loader = world.read_resource::<Loader>();
Sounds { single_shot: loader.load("audio/shot.ogg", OggFormat, (), &world.read_resource()) }
Sounds {
handles: [
loader.load("audio/shot.ogg", OggFormat, (), &world.read_resource())
]
}
};

world.insert(sounds);
}

File renamed without changes.
Loading

0 comments on commit beca125

Please sign in to comment.