This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make plants actually photosynthesize (#88)
* Use a single source of truth for map size * Refactor terrain module * Store map geometry in a dedicated resource, rather than using config * Refactor plugin structure to make app easier to test * Set up basic integration tests * Enable photosynthesis * Allow users to configure terrain type distribution during config * Clippy
- Loading branch information
1 parent
40c5445
commit 2a8ae05
Showing
17 changed files
with
447 additions
and
200 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,67 @@ | ||
//! A helpful trait to allow us to iterate over all variants of an enum type | ||
use std::marker::PhantomData; | ||
|
||
/// Marks an enum whose variants can be iterated over in the order they are defined. | ||
pub trait IterableEnum: Sized { | ||
/// The number of variants of this action type | ||
const N_VARIANTS: usize; | ||
|
||
/// Iterates over the possible variants in the order they were defined. | ||
fn variants() -> EnumIter<Self> { | ||
EnumIter::default() | ||
} | ||
|
||
/// Returns the default value for the variant stored at the provided index if it exists. | ||
/// | ||
/// This is mostly used internally, to enable space-efficient iteration. | ||
fn get_at(index: usize) -> Option<Self>; | ||
|
||
/// Returns the position in the defining enum of the given action | ||
fn index(&self) -> usize; | ||
} | ||
|
||
/// An iterator of enum variants. | ||
/// | ||
/// Created by calling [`IterableEnum::variants`]. | ||
#[derive(Debug, Clone)] | ||
pub struct EnumIter<A: IterableEnum> { | ||
/// Keeps track of which variant should be provided next. | ||
/// | ||
/// Alternatively, `min(index - 1, 0)` counts how many variants have already been iterated | ||
/// through. | ||
index: usize, | ||
/// Marker used to keep track of which `IterableEnum` this `EnumIter` iterates through. | ||
/// | ||
/// For more information, see [`PhantomData`](std::marker::PhantomData). | ||
_phantom: PhantomData<A>, | ||
} | ||
|
||
impl<A: IterableEnum> Iterator for EnumIter<A> { | ||
type Item = A; | ||
|
||
fn next(&mut self) -> Option<A> { | ||
let item = A::get_at(self.index); | ||
if item.is_some() { | ||
self.index += 1; | ||
} | ||
|
||
item | ||
} | ||
} | ||
|
||
impl<A: IterableEnum> ExactSizeIterator for EnumIter<A> { | ||
fn len(&self) -> usize { | ||
A::N_VARIANTS | ||
} | ||
} | ||
|
||
// We can't derive this, because otherwise it won't work when A is not default | ||
impl<A: IterableEnum> Default for EnumIter<A> { | ||
fn default() -> Self { | ||
EnumIter { | ||
index: 0, | ||
_phantom: PhantomData::default(), | ||
} | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.