-
Notifications
You must be signed in to change notification settings - Fork 0
Start of apples #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alilee
wants to merge
2
commits into
main
Choose a base branch
from
step3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Start of apples #11
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| use bevy::prelude::*; | ||
| use bevy::core::FixedTimestep; | ||
|
|
||
| use crate::board::Board; | ||
|
|
||
| pub struct Plugin; | ||
|
|
||
| impl bevy::prelude::Plugin for Plugin { | ||
| fn build(&self, app: &mut App) { | ||
| app.add_system_set( | ||
| SystemSet::new() | ||
| .with_run_criteria(FixedTimestep::step(5.0)) | ||
| .with_system(add_apple) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[derive(Component)] | ||
| pub struct Apple; | ||
|
|
||
| fn add_apple(mut commands: Commands, mut board: ResMut<Board>) { | ||
| let loc = board.spawn_apple(); | ||
| info!("spawning apple: {:?}", &loc); | ||
| commands.spawn().insert(Apple).insert(loc); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| use bevy::prelude::*; | ||
| use bevy::math::*; | ||
| use crate::App; | ||
| use crate::location::Location; | ||
| use bevy::prelude::*; | ||
| use std::collections::HashSet; | ||
|
|
||
| use rand::prelude::*; | ||
|
|
||
| use crate::direction::Direction; | ||
| use crate::location::Location; | ||
| use crate::App; | ||
|
|
||
| pub struct Plugin; | ||
|
|
||
|
|
@@ -14,30 +18,62 @@ impl bevy::prelude::Plugin for Plugin { | |
|
|
||
| pub(crate) struct Board { | ||
| pub(crate) limit: UVec2, | ||
| taken: HashSet<UVec2>, | ||
| } | ||
|
|
||
| impl Board { | ||
| pub fn adjacent(&self, loc: &Location, dir: &Direction) -> Option<Location> { | ||
| let coord = loc.coord; | ||
|
|
||
| let result_coord = match dir { | ||
| Direction::Up if coord.y < self.limit.y => uvec2(coord.x, coord.y + 1), | ||
| Direction::Down if coord.y > 0 => uvec2(coord.x, coord.y - 1), | ||
| Direction::Left if coord.x > 0 => uvec2(coord.x - 1, coord.y), | ||
| Direction::Up if coord.y < self.limit.y => uvec2(coord.x, coord.y + 1), | ||
| Direction::Down if coord.y > 0 => uvec2(coord.x, coord.y - 1), | ||
| Direction::Left if coord.x > 0 => uvec2(coord.x - 1, coord.y), | ||
| Direction::Right if coord.x < self.limit.x => uvec2(coord.x + 1, coord.y), | ||
| _ => return None, | ||
| }; | ||
|
|
||
| Some(Location::new(result_coord)) | ||
| } | ||
|
|
||
| pub fn starting_location(&self) -> Location { | ||
| Location::new(uvec2(self.limit.x / 2, (self.limit.y - 1) / 2 + 1)) | ||
| pub fn reset(&mut self) -> Location { | ||
| self.taken.clear(); | ||
| let coord = uvec2(self.limit.x / 2, (self.limit.y - 1) / 2 + 1); | ||
| self.taken.insert(coord); | ||
| Location::new(coord) | ||
| } | ||
|
|
||
| pub fn spawn_apple(&mut self) -> Location { | ||
| let x_range = 0..=self.limit.x; | ||
| let y_range = 0..=self.limit.y; | ||
| let mut rng = thread_rng(); | ||
| loop { | ||
| let coord = uvec2( | ||
| rng.gen_range(x_range.clone()), | ||
| rng.gen_range(y_range.clone()), | ||
| ); | ||
| if !self.taken.contains(&coord) { | ||
| self.taken.insert(coord); | ||
| return Location::new(coord); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn block(&mut self, loc: &Location) { | ||
| assert!(!self.taken.contains(&loc.coord)); | ||
| self.taken.insert(loc.coord); | ||
| } | ||
|
|
||
| pub fn clear(&mut self, loc: &Location) { | ||
| self.taken.remove(&loc.coord); | ||
| } | ||
| } | ||
|
|
||
| impl Default for Board { | ||
| fn default() -> Self { | ||
| Board { limit: uvec2(15, 8) } | ||
| Board { | ||
| limit: uvec2(15, 8), | ||
| taken: HashSet::new(), | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll eventually need to know what is at the location though. |
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the major design decision. How much game state/knowledge should be encapsulated in Board vs. the ECS?