Make Nature a trait
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-11-27 17:36:42 +01:00
parent efd1acdfa5
commit 996a35ffa4
7 changed files with 56 additions and 33 deletions

View File

@@ -272,7 +272,7 @@ extern "C" fn pokemon_allowed_experience_gain(ptr: ExternPointer<Arc<Pokemon>>)
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_nature(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Nature>> {
extern "C" fn pokemon_nature(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Nature>> {
ptr.as_ref().nature().clone().into()
}

View File

@@ -21,7 +21,7 @@ unsafe extern "C" fn nature_library_drop(ptr: OwnedPtr<NatureLibrary>) {
unsafe extern "C" fn nature_library_load_nature(
mut ptr: ExternPointer<NatureLibrary>,
name: BorrowedPtr<c_char>,
nature: OwnedPtr<Arc<Nature>>,
nature: OwnedPtr<Arc<dyn Nature>>,
) {
ptr.as_mut()
.load_nature(CStr::from_ptr(name).into(), nature.as_ref().unwrap().clone())
@@ -32,7 +32,7 @@ unsafe extern "C" fn nature_library_load_nature(
unsafe extern "C" fn nature_library_get_nature(
ptr: ExternPointer<NatureLibrary>,
name: BorrowedPtr<c_char>,
) -> IdentifiablePointer<Arc<Nature>> {
) -> IdentifiablePointer<Arc<dyn Nature>> {
if let Some(nature) = ptr.as_ref().get_nature(&CStr::from_ptr(name).into()) {
nature.clone().into()
} else {
@@ -44,7 +44,7 @@ unsafe extern "C" fn nature_library_get_nature(
#[no_mangle]
unsafe extern "C" fn nature_library_get_nature_name(
ptr: ExternPointer<NatureLibrary>,
nature: BorrowedPtr<Arc<Nature>>,
nature: BorrowedPtr<Arc<dyn Nature>>,
) -> OwnedPtr<c_char> {
CString::new(ptr.as_ref().get_nature_name(nature.as_ref().unwrap()).str())
.unwrap()

View File

@@ -1,5 +1,5 @@
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{Nature, Statistic};
use crate::static_data::{Nature, NatureImpl, Statistic};
use std::ptr::drop_in_place;
use std::sync::Arc;
@@ -10,31 +10,31 @@ extern "C" fn nature_new(
decrease_stat: Statistic,
increase_modifier: f32,
decrease_modifier: f32,
) -> IdentifiablePointer<Arc<Nature>> {
Nature::new(increase_stat, decrease_stat, increase_modifier, decrease_modifier).into()
) -> IdentifiablePointer<Arc<NatureImpl>> {
NatureImpl::new(increase_stat, decrease_stat, increase_modifier, decrease_modifier).into()
}
/// Reduce the reference count for a nature.
#[no_mangle]
unsafe extern "C" fn nature_drop(ptr: OwnedPtr<Arc<Nature>>) {
unsafe extern "C" fn nature_drop(ptr: OwnedPtr<Arc<NatureImpl>>) {
drop_in_place(ptr)
}
/// The stat that should receive the increased modifier.
#[no_mangle]
extern "C" fn nature_increased_stat(ptr: ExternPointer<Arc<Nature>>) -> Statistic {
extern "C" fn nature_increased_stat(ptr: ExternPointer<Arc<dyn Nature>>) -> Statistic {
ptr.as_ref().increased_stat()
}
/// The stat that should receive the decreased modifier.
#[no_mangle]
extern "C" fn nature_decreased_stat(ptr: ExternPointer<Arc<Nature>>) -> Statistic {
extern "C" fn nature_decreased_stat(ptr: ExternPointer<Arc<dyn Nature>>) -> Statistic {
ptr.as_ref().decreased_stat()
}
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
#[no_mangle]
extern "C" fn nature_get_stat_modifier(ptr: ExternPointer<Arc<Nature>>, stat: Statistic) -> f32 {
extern "C" fn nature_get_stat_modifier(ptr: ExternPointer<Arc<dyn Nature>>, stat: Statistic) -> f32 {
ptr.as_ref().get_stat_modifier(stat)
}