Skip to content

Commit

Permalink
Add harvester object
Browse files Browse the repository at this point in the history
Can be moved around with right-click.
  • Loading branch information
mrtracy committed Jul 22, 2024
1 parent 1fd71a4 commit 17f402b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
Binary file added assets/harvester_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 46 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(clippy::too_many_arguments, clippy::type_complexity)]

use bevy::asset::AssetMetaCheck;
use bevy::math::vec2;
use bevy::math::{vec2, vec3};
use bevy::prelude::*;
use bevy::window::PrimaryWindow;

Expand All @@ -27,25 +27,37 @@ fn main() {
.run();
}

fn setup(mut commands: Commands) {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Harvester::new_bundle(&asset_server, 15));
}

pub fn sys_spawn_on_click(
mut commands: Commands,
buttons: Res<ButtonInput<MouseButton>>,
q_windows: Query<&Window, With<PrimaryWindow>>,
camera: Query<(&Camera, &GlobalTransform)>,
mut harvester: Query<&mut Transform, With<Harvester>>,
asset_server: Res<AssetServer>,
bounds: Res<LevelBounds>,
) {
let get_world_click_pos = || -> Option<Vec2> {
let Some(pos) = q_windows
.get_single()
.ok()
.and_then(|w| w.cursor_position())
else {
return None;
};
let Ok((camera, gt)) = camera.get_single() else {
return None;
};
camera.viewport_to_world_2d(gt, pos)
};

if buttons.just_pressed(MouseButton::Left) {
if let Some(pos) = q_windows.single().cursor_position() {
if let Ok((camera, gt)) = camera.get_single() {
let pos = camera.viewport_to_world_2d(gt, pos).unwrap();
if !bounds.in_bounds(pos) {
return;
}
if let Some(pos) = get_world_click_pos() {
if bounds.in_bounds(pos) {
commands.spawn(Plant::new_bundle(
asset_server.load("plant_base_test.png"),
asset_server.load("Crops/Carrot/carrot.png"),
Expand All @@ -54,6 +66,15 @@ pub fn sys_spawn_on_click(
}
}
}
if buttons.just_pressed(MouseButton::Right) {
if let Some(pos) = get_world_click_pos() {
if bounds.in_bounds(pos) {
let _ = harvester
.get_single_mut()
.map(|mut h| *h = h.with_translation(vec3(pos.x, pos.y, 0.)));
}
}
}
}

pub fn sys_plant_move(
Expand Down Expand Up @@ -137,3 +158,20 @@ impl Plant {

#[derive(Component)]
pub struct Fruit;

#[derive(Component)]
pub struct Harvester {
pub range_units: usize,
}

impl Harvester {
pub fn new_bundle(asset_server: &AssetServer, range_units: usize) -> impl Bundle {
(
Harvester { range_units },
SpriteBundle {
texture: asset_server.load("harvester_test.png"),
..Default::default()
},
)
}
}

0 comments on commit 17f402b

Please sign in to comment.