impl src::battler and src::mon which is Godot free
parent
f7f46ba96e
commit
e6fa3a9293
|
|
@ -1,111 +0,0 @@
|
||||||
mod main {
|
|
||||||
use {super::data};
|
|
||||||
|
|
||||||
pub fn main() {
|
|
||||||
let db = data::DB::new().expect("could not make db");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_main() {
|
|
||||||
main();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod data {
|
|
||||||
mod models {
|
|
||||||
use {
|
|
||||||
native_db::{
|
|
||||||
native_db,
|
|
||||||
ToKey,
|
|
||||||
},
|
|
||||||
native_model::{
|
|
||||||
native_model,
|
|
||||||
Model,
|
|
||||||
},
|
|
||||||
serde::{Deserialize, Serialize},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod v1 {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
|
|
||||||
#[native_model(id = 1, version = 1)]
|
|
||||||
#[native_db]
|
|
||||||
pub struct Person {
|
|
||||||
#[primary_key]
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
use native_db::*;
|
|
||||||
|
|
||||||
pub type Person = models::v1::Person;
|
|
||||||
|
|
||||||
static MODELS: std::sync::LazyLock<Models> = std::sync::LazyLock::new(|| {
|
|
||||||
let mut models = Models::new();
|
|
||||||
models.define::<Person>().expect("failed to define person");
|
|
||||||
models
|
|
||||||
});
|
|
||||||
|
|
||||||
pub struct DB<'a> {
|
|
||||||
db: Database<'a>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> DB<'a> {
|
|
||||||
pub fn new() -> Result<DB<'a>, String> {
|
|
||||||
let db = match Builder::new().create_in_memory(&MODELS) {
|
|
||||||
Ok(db) => Ok(db),
|
|
||||||
Err(msg) => Err(msg.to_string()),
|
|
||||||
}?;
|
|
||||||
Ok(DB{
|
|
||||||
db: db,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert<T: ToInput>(self: &DB<'a>, v: T) -> Result<(), String> {
|
|
||||||
let t = match self.db.rw_transaction() {
|
|
||||||
Ok(t) => t,
|
|
||||||
Err(msg) => return Err(msg.to_string()),
|
|
||||||
};
|
|
||||||
if let Err(msg) = t.insert(v) {
|
|
||||||
return Err(msg.to_string());
|
|
||||||
}
|
|
||||||
if let Err(msg) = t.commit() {
|
|
||||||
return Err(msg.to_string());
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get<T: ToInput, S: ToKey>(self: &DB<'a>, primary: S) -> Result<Option<T>, String> {
|
|
||||||
let t = match self.db.r_transaction() {
|
|
||||||
Ok(t) => t,
|
|
||||||
Err(msg) => return Err(msg.to_string()),
|
|
||||||
};
|
|
||||||
match t.get().primary(primary) {
|
|
||||||
Ok(v) => Ok(v),
|
|
||||||
Err(msg) => Err(msg.to_string()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn mvp() {
|
|
||||||
let db = DB::new().expect("could not make db");
|
|
||||||
let person = Person{name: "me".to_string()};
|
|
||||||
db.insert(person.clone()).expect("could not insert person");
|
|
||||||
assert_eq!(person, db.get("me".to_string()).expect("could not get").expect("got none"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -6,6 +6,4 @@ struct MyExtension;
|
||||||
unsafe impl ExtensionLibrary for MyExtension {
|
unsafe impl ExtensionLibrary for MyExtension {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod player;
|
pub mod src;
|
||||||
pub mod mon;
|
|
||||||
pub mod mon_move;
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
use crate::mon;
|
|
||||||
|
|
||||||
pub struct MonMove {
|
|
||||||
power: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MonMove {
|
|
||||||
const fn new(power: i32) -> Self {
|
|
||||||
Self{power: power}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn use_on(&self, dest: &mut mon::Mon) {
|
|
||||||
let power = self.power as f32 * dest.damage_scalar();
|
|
||||||
dest.hit_for(power as i32);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const TACKLE: MonMove = MonMove::new(25);
|
|
||||||
|
|
@ -1,45 +0,0 @@
|
||||||
use godot::prelude::*;
|
|
||||||
|
|
||||||
use godot::classes::{ISprite2D, Sprite2D};
|
|
||||||
#[derive(GodotClass)]
|
|
||||||
#[class(base=Sprite2D)]
|
|
||||||
struct BreeLPlayer {
|
|
||||||
base: Base<Sprite2D>,
|
|
||||||
speed: f64,
|
|
||||||
radians: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[godot_api]
|
|
||||||
impl ISprite2D for BreeLPlayer {
|
|
||||||
fn init(base: Base<Sprite2D>) -> Self {
|
|
||||||
BreeLPlayer{base: base, speed: 200.0, radians: 1.0}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn physics_process(&mut self, delta: f64) {
|
|
||||||
let radians = self.radians * delta as f32;
|
|
||||||
self.base_mut().rotate(radians);
|
|
||||||
|
|
||||||
let rotation = self.base().get_rotation();
|
|
||||||
let velocity = Vector2::UP.rotated(rotation) * self.speed as f32;
|
|
||||||
self.base_mut().translate(velocity * delta as f32);
|
|
||||||
|
|
||||||
self.increase_speed(100.0 * delta as f64);
|
|
||||||
self.radians *= 1.01 as f32;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[godot_api]
|
|
||||||
impl BreeLPlayer {
|
|
||||||
#[func]
|
|
||||||
fn increase_speed(&mut self, amount: f64) {
|
|
||||||
self.speed += amount;
|
|
||||||
self.base_mut().emit_signal("speed_increased", &[]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[signal]
|
|
||||||
fn speed_increased();
|
|
||||||
|
|
||||||
fn drop(&mut self) {
|
|
||||||
eprintln!("drop");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,137 @@
|
||||||
|
pub mod mon {
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub struct Species {
|
||||||
|
pub name: String,
|
||||||
|
pub hp: i32,
|
||||||
|
pub atk: i32,
|
||||||
|
pub def: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod species_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_literal() {
|
||||||
|
let _: Species = Species{
|
||||||
|
name: "".to_string(),
|
||||||
|
hp: 1,
|
||||||
|
atk: 2,
|
||||||
|
def: 3,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum Dex {
|
||||||
|
Pika,
|
||||||
|
Mari,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Dex {
|
||||||
|
pub fn new(&self) -> Species {
|
||||||
|
self.clone().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<Species> for Dex {
|
||||||
|
fn into(self) -> Species {
|
||||||
|
match self {
|
||||||
|
Dex::Pika => Species{
|
||||||
|
name: "Pika".to_string(),
|
||||||
|
hp: 10,
|
||||||
|
atk: 11,
|
||||||
|
def: 9,
|
||||||
|
},
|
||||||
|
Dex::Mari => Species{
|
||||||
|
name: "Mari".to_string(),
|
||||||
|
hp: 10,
|
||||||
|
atk: 8,
|
||||||
|
def: 12,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod dex_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dex_to_species() {
|
||||||
|
let dex = Dex::Pika;
|
||||||
|
let _: Species = dex.new();
|
||||||
|
let _: Species = dex.into();
|
||||||
|
let _: Species = Dex::Pika.new();
|
||||||
|
let _: Species = Dex::Pika.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Instance {
|
||||||
|
pub species: Species,
|
||||||
|
pub damage: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Instance {
|
||||||
|
pub fn roll(dex: Dex) -> Instance {
|
||||||
|
Instance{
|
||||||
|
species: dex.into(),
|
||||||
|
damage: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod instance_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_instance_roll() {
|
||||||
|
let i: Instance = Instance::roll(Dex::Pika);
|
||||||
|
assert_eq!(0, i.damage);
|
||||||
|
assert_eq!(Dex::Pika.new(), i.species);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod battler {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn attack(w: &mut mon::Instance, r: &mut mon::Instance) {
|
||||||
|
let atk_delta = r.species.atk - 10;
|
||||||
|
let def_delta = w.species.def - 10;
|
||||||
|
|
||||||
|
let power = match atk_delta - def_delta {
|
||||||
|
v if v < 1 => 1,
|
||||||
|
v => v,
|
||||||
|
};
|
||||||
|
|
||||||
|
w.damage = match w.damage + power {
|
||||||
|
v if v < w.species.hp => v,
|
||||||
|
_ => w.species.hp,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod species_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_attack() {
|
||||||
|
let mut pika = mon::Instance::roll(mon::Dex::Pika);
|
||||||
|
let mut mari = mon::Instance::roll(mon::Dex::Mari);
|
||||||
|
|
||||||
|
attack(&mut pika, &mut mari);
|
||||||
|
assert_eq!(1, pika.damage);
|
||||||
|
|
||||||
|
attack(&mut mari, &mut pika);
|
||||||
|
assert_eq!(1, mari.damage);
|
||||||
|
|
||||||
|
for i in 0..pika.species.hp+5 {
|
||||||
|
attack(&mut pika, &mut mari);
|
||||||
|
}
|
||||||
|
assert_eq!(pika.species.hp, pika.damage);
|
||||||
|
assert_eq!(1, mari.damage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue