Compare commits

...

26 Commits

Author SHA1 Message Date
7b56c52fff Fix map size width 2021-07-17 16:24:49 -05:00
6a2c77404b Use some constants 2021-07-17 16:23:45 -05:00
aa7e888d9e Update marshmallow sprite with shadow 2021-07-17 14:33:52 -05:00
170088b836 Consistent naming 2021-07-17 13:42:11 -05:00
34d1de5847 Update wall sprite again 2021-07-17 13:36:09 -05:00
70e4ac0c81 Update grass and wall sprites 2021-07-17 12:52:08 -05:00
ef4a98627e Make camera follow player 2021-07-17 02:50:05 -05:00
e816846d19 Increase player speed 2021-07-17 02:19:02 -05:00
9e16c22eb5 Add basic player movement system 2021-07-17 00:20:54 -05:00
66854be921 Optimize dependencies to help with runtime performance 2021-07-17 00:16:45 -05:00
04fe103b25 Add player sprite 2021-07-16 23:33:54 -05:00
4ab62ea921 Add marshmallow sprite 2021-07-16 23:33:40 -05:00
32f6a822a2 Start with black screen 2021-07-16 21:55:24 -05:00
89ddbd2483 Add setup system with camera and empty map 2021-07-16 21:52:57 -05:00
b7b89b801b Add assets folder and grass, wall sprites 2021-07-16 21:52:28 -05:00
3fe46fd116 Setup basic resources 2021-07-12 23:01:35 -05:00
bc5eb9b006 Implement basic player plugin 2021-07-12 22:56:00 -05:00
6705870f81 Setup basic game application 2021-07-12 22:43:28 -05:00
e863f5af88 Add basic systems 2021-07-12 22:43:15 -05:00
c8384883ad Add basic components 2021-07-12 22:43:06 -05:00
11dc9771cc Use bevy 2021-07-12 20:57:30 -05:00
6791c0cf48 Add cargo lock file 2021-07-12 20:57:18 -05:00
ad8852f4b8 Add bevy 2021-07-12 20:57:04 -05:00
bfa4d87ece Add author 2021-07-12 20:56:52 -05:00
0ac3e87765 Add fast-build configuration for cargo 2021-07-12 20:56:29 -05:00
9a38783859 Use nightly rust 2021-07-12 20:55:57 -05:00
12 changed files with 3654 additions and 2 deletions

22
.cargo/config.toml Normal file
View File

@ -0,0 +1,22 @@
# Add the contents of this file to `config.toml` to enable "fast build" configuration. Please read the notes below.
# NOTE: For maximum performance, build using a nightly compiler
# If you are using rust stable, remove the "-Zshare-generics=y" below.
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zshare-generics=y"]
# NOTE: you must manually install https://github.com/michaeleisel/zld on mac. you can easily do this with the "brew" package manager:
# `brew install michaeleisel/zld/zld`
[target.x86_64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld", "-Zshare-generics=y"]
[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"
rustflags = ["-Zshare-generics=y"]
# Optional: Uncommenting the following improves compile times, but reduces the amount of debug info to 'line number tables only'
# In most cases the gains are negligible, but if you are on macos and have slow compile times you should see significant gains.
#[profile.dev]
#debug = 1

3467
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,16 @@
[package]
name = "adventure-game"
version = "0.1.0"
authors = ["Daniel Lynn <daniel.eric.lynn@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = { version = "0.5", features = ["dynamic"] }
[profile.dev.package."*"]
opt-level = 3
[profile.dev]
opt-level = 1

BIN
assets/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 B

BIN
assets/marshmallow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 335 B

BIN
assets/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 624 B

2
rust-toolchain Normal file
View File

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

2
src/components/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub struct Name(pub String);
pub struct Player;

View File

@ -1,3 +1,29 @@
fn main() {
println!("Hello, world!");
mod components;
mod plugins;
mod resources;
mod systems;
mod prelude {
pub use crate::components::*;
pub use crate::plugins::*;
pub use crate::resources::*;
pub use crate::systems::*;
pub use bevy::prelude::*;
pub const SCREEN_WIDTH: f32 = 80.0;
pub const SCREEN_HEIGHT: f32 = 50.0;
pub const TILE_SIZE: f32 = 64.0;
}
use prelude::*;
fn main() {
App::build()
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_startup_system(add_player.system())
.add_system(player_movement.system())
.add_system(follow_player.system())
.run();
}

11
src/plugins/mod.rs Normal file
View File

@ -0,0 +1,11 @@
use crate::prelude::*;
pub struct PlayerPlugin;
impl Plugin for PlayerPlugin {
fn build(&self, app: &mut AppBuilder) {
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, true)))
.add_startup_system(add_player.system())
.add_system(greet_player.system());
}
}

3
src/resources/mod.rs Normal file
View File

@ -0,0 +1,3 @@
use crate::prelude::*;
pub struct GreetTimer(pub Timer);

111
src/systems/mod.rs Normal file
View File

@ -0,0 +1,111 @@
use crate::prelude::*;
use bevy::render::camera::Camera;
pub fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
// Camera
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
// Map
let grass_material = materials.add(asset_server.load("grass.png").into());
let tile_size = Vec2::new(TILE_SIZE, TILE_SIZE);
let sprite = Sprite::new(tile_size);
let map_size = Vec2::new(SCREEN_WIDTH, SCREEN_HEIGHT);
let half_x = (map_size.x / 2.0) as i32;
let half_y = (map_size.y / 2.0) as i32;
for y in -half_y..half_y {
for x in -half_x..half_x {
let position = Vec2::new(x as f32, y as f32);
let translation = (position * tile_size).extend(0.0);
commands.spawn_bundle(SpriteBundle {
material: grass_material.clone(),
sprite: sprite.clone(),
transform: Transform::from_translation(translation),
..Default::default()
});
}
}
}
pub fn add_player(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let player_material = materials.add(asset_server.load("marshmallow.png").into());
commands
.spawn()
.insert(Player)
.insert(Name("Player 1".to_string()))
.insert_bundle(SpriteBundle {
material: player_material,
sprite: Sprite::new(Vec2::new(TILE_SIZE, TILE_SIZE)),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
..Default::default()
});
}
pub fn player_movement(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&Player, &mut Transform)>,
time: Res<Time>,
) {
if let Ok((_, mut transform)) = query.single_mut() {
let mut direction = Vec2::ZERO;
if keyboard_input.pressed(KeyCode::A) {
direction.x -= 1.0;
}
if keyboard_input.pressed(KeyCode::D) {
direction.x += 1.0;
}
if keyboard_input.pressed(KeyCode::S) {
direction.y -= 1.0;
}
if keyboard_input.pressed(KeyCode::W) {
direction.y += 1.0;
}
let translation = &mut transform.translation;
let delta_seconds = time.delta_seconds();
translation.x += delta_seconds * direction.x * 300.0;
translation.y += delta_seconds * direction.y * 300.0;
}
}
pub fn follow_player(
mut q: QuerySet<(
Query<&Transform, With<Player>>,
Query<&mut Transform, With<Camera>>,
)>,
) {
if let Ok(player_transform) = q.q0().single() {
let player_translation = player_transform.translation;
if let Ok(mut camera_transform) = q.q1_mut().single_mut() {
let camera_translation = &mut camera_transform.translation;
camera_translation.x = player_translation.x;
camera_translation.y = player_translation.y;
}
}
}
pub fn greet_player(
query: Query<&Name, With<Player>>,
time: Res<Time>,
mut timer: ResMut<GreetTimer>,
) {
if timer.0.tick(time.delta()).just_finished() {
for name in query.iter() {
println!("Hello, {}!", name.0);
}
}
}