Skip to content

Commit

Permalink
Add Death Knights;
Browse files Browse the repository at this point in the history
Add Virus;
VarValue hash;
RandomFloat support seed;
  • Loading branch information
makscee committed Mar 11, 2024
1 parent ca5a4f4 commit 47dded7
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 14 deletions.
7 changes: 6 additions & 1 deletion assets/ron/heroes/protector.unit.ron
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
children: [],
mapping: {
Offset: Mul(
UnitVec(Sum(IntFloat(State(Index)), Sum(GameTime, Sin(Mul(RandomFloat, GameTime))))),
UnitVec(
Sum(
IntFloat(State(Index)),
Sum(GameTime, Sin(Mul(RandomFloat(Owner), GameTime))),
),
),
Mul(Sin(Mul(GameTime, Float(0.3))), Float(0.19)),
),
},
Expand Down
8 changes: 5 additions & 3 deletions assets/ron/heroes/scavenger.unit.ron
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
material: Shape(
shape: Rectangle,
fill: Line,
size: Vec2E(Sum(Float(0.4), Mul(RandomFloat, Float(1.1)))),
thickness: Sum(Float(0.8), Mul(RandomFloat, Float(3.0))),
size: Vec2E(Sum(Float(0.4), Mul(RandomFloat(Owner), Float(1.1)))),
thickness: Sum(Float(0.8), Mul(RandomFloat(Owner), Float(3.0))),
alpha: Float(0.2),
),
children: [],
mapping: {Rotation: Sum(IntFloat(State(Index)), Sin(Sum(GameTime, Mul(RandomFloat, Float(10.0)))))},
mapping: {
Rotation: Sum(IntFloat(State(Index)), Sin(Sum(GameTime, Mul(RandomFloat(Owner), Float(10.0))))),
},
count: 10,
),
)
34 changes: 34 additions & 0 deletions assets/ron/heroes/virus.unit.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(
name: "Virus",
hp: 1,
atk: 1,
stacks: 1,
level: 1,
houses: "Death Knights",
description: "%trigger → %effect on %target",
trigger: Fire(trigger: BattleStart, target: EnemyUnits, effect: UseAbility("Plague"), period: 0),
representation: (
material: Shape(
shape: Circle,
fill: Line,
fill_color: Solid,
size: Vec2E(Float(0.12)),
point1: Vec2(1.0, 1.0),
point2: Vec2(1.0, 1.0),
thickness: Float(1.0),
alpha: Float(1.0),
colors: [State(Color)],
parts: [Float(0.0)],
),
children: [],
mapping: {
Offset: Vec2EE(
Mul(Sub(RandomFloat(Index), Float(0.5)), Sin(Sum(GameTime, Index))),
Sub(RandomFloat(Owner), Float(0.5)),
),
},
count: 16,
),
state: (history: {}, birth: 0.0),
statuses: [],
)
82 changes: 82 additions & 0 deletions assets/ron/houses/death_knights.house.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#![enable(implicit_some)]
(
name: "Death Knights",
color: ("#658D1B"),
statuses: [
(
name: "Plague",
description: "Take {Charges} DMG every turn, [Summon Skeleton] after death",
trigger: List(
[
Fire(trigger: TurnEnd, effect: WithTarget(Owner, Damage(Context(Charges)))),
Fire(trigger: BeforeDeath, effect: UseAbility("Summon Skeleton")),
],
),
),
],
abilities: [
(
name: "Plague",
description: "Target takes 1 DMG every turn, [Summon Skeleton] after death",
effect: List([AddStatus("Plague"), Vfx("apply_status")]),
),
(
name: "Summon Skeleton",
description: "Summon enemy {1}/{4}",
effect: WithVar(Faction, OppositeFaction, Summon("Skeleton")),
),
],
summons: [
(
name: "Skeleton",
hp: 4,
atk: 1,
stacks: 1,
level: 1,
houses: "Death Knights",
description: "",
trigger: Fire(trigger: Noop, target: Owner, effect: Noop, period: 0),
representation: (
material: Shape(
shape: Rectangle,
fill: Opaque,
fill_color: Solid,
size: Vec2(0.05, 1.0),
point1: Vec2(1.0, 1.0),
point2: Vec2(1.0, 1.0),
thickness: Float(1.0),
alpha: Float(1.0),
colors: [State(Color)],
parts: [Float(0.0)],
),
children: [],
mapping: {
Offset: Mul(
UnitVec(
Mul(
Sub(
RandomFloat(Sum(Index, Sum(Int(200), ToInt(GameTime)))),
Float(0.5),
),
Mul(PI, Float(2.0)),
),
),
Float(0.5),
),
Rotation: Mul(
RandomFloat(
Sum(
Mul(Index, Int(10)),
Sum(Int(29), ToInt(Sum(GameTime, Mul(Index, Float(0.1))))),
),
),
Mul(PI, Float(2.0)),
),
},
count: 12,
),
state: (history: {}, birth: 0.0),
statuses: [],
),
],
)
2 changes: 1 addition & 1 deletion src/plugins/hero_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ impl EditorNodeGenerator for Expression {
match self {
Expression::Zero
| Expression::GameTime
| Expression::RandomFloat
| Expression::PI
| Expression::Age
| Expression::SlotPosition
Expand Down Expand Up @@ -818,6 +817,7 @@ impl EditorNodeGenerator for Expression {
| Expression::FilterMaxEnemy(x)
| Expression::FindUnit(x)
| Expression::UnitCount(x)
| Expression::RandomFloat(x)
| Expression::StatusCharges(x) => show_node(
x.as_mut(),
format!("{path}:x"),
Expand Down
22 changes: 13 additions & 9 deletions src/resourses/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use rand::{
Rng, SeedableRng,
};
use rand_chacha::ChaCha8Rng;
use std::f32::consts::PI;
use std::hash::{Hash, Hasher};
use std::{collections::hash_map::DefaultHasher, f32::consts::PI};

use super::*;

Expand All @@ -13,7 +14,6 @@ pub enum Expression {
#[default]
Zero,
GameTime,
RandomFloat,
PI,
Age,
SlotPosition,
Expand Down Expand Up @@ -69,6 +69,7 @@ pub enum Expression {
FindUnit(Box<Expression>),
UnitCount(Box<Expression>),
ToInt(Box<Expression>),
RandomFloat(Box<Expression>),

Vec2EE(Box<Expression>, Box<Expression>),
Sum(Box<Expression>, Box<Expression>),
Expand All @@ -91,10 +92,6 @@ impl Expression {
pub fn get_value(&self, context: &Context, world: &mut World) -> Result<VarValue> {
match self {
Expression::Zero => Ok(VarValue::Int(0)),
Expression::RandomFloat => {
let mut rng = ChaCha8Rng::seed_from_u64(context.owner().to_bits());
Ok(VarValue::Float(rng.gen_range(0.0..1.0)))
}
Expression::Float(x) => Ok(VarValue::Float(*x)),
Expression::Int(x) => Ok(VarValue::Int(*x)),
Expression::Bool(x) => Ok(VarValue::Bool(*x)),
Expand Down Expand Up @@ -134,6 +131,13 @@ impl Expression {
let x = x.get_int(context, world)?;
Ok(VarValue::Bool(x % 2 == 0))
}
Expression::RandomFloat(x) => {
let x = x.get_value(context, world)?;
let mut hasher = DefaultHasher::new();
x.hash(&mut hasher);
let mut rng = ChaCha8Rng::seed_from_u64(hasher.finish());
Ok(VarValue::Float(rng.gen_range(0.0..1.0)))
}
Expression::GameTime => Ok(VarValue::Float(GameTimer::get().play_head())),
Expression::PI => Ok(VarValue::Float(PI)),
Expression::Sum(a, b) => {
Expand Down Expand Up @@ -418,7 +422,6 @@ impl Expression {
match self {
Self::Zero
| Self::GameTime
| Self::RandomFloat
| Self::PI
| Self::Owner
| Self::Caster
Expand Down Expand Up @@ -469,6 +472,7 @@ impl Expression {
| Self::FilterMaxEnemy(x)
| Self::FindUnit(x)
| Self::UnitCount(x)
| Self::RandomFloat(x)
| Self::Vec2E(x) => vec![x],

Self::Vec2EE(a, b)
Expand Down Expand Up @@ -530,7 +534,6 @@ impl Expression {
match self {
Expression::Zero
| Expression::GameTime
| Expression::RandomFloat
| Expression::PI
| Expression::Owner
| Expression::Caster
Expand Down Expand Up @@ -582,6 +585,7 @@ impl Expression {
| Expression::FilterMaxEnemy(_)
| Expression::FindUnit(_)
| Expression::UnitCount(_)
| Expression::RandomFloat(_)
| Expression::StatusCharges(_) => hex_color!("#448AFF"),
Expression::Vec2EE(_, _)
| Expression::Sum(_, _)
Expand All @@ -604,7 +608,6 @@ impl Expression {
match self {
Expression::Zero
| Expression::GameTime
| Expression::RandomFloat
| Expression::PI
| Expression::Age
| Expression::SlotPosition
Expand Down Expand Up @@ -655,6 +658,7 @@ impl Expression {
| Expression::FilterMaxEnemy(v)
| Expression::FindUnit(v)
| Expression::UnitCount(v)
| Expression::RandomFloat(v)
| Expression::StatusCharges(v) => format!(
"{} ({})",
self.to_string().to_case(Case::Lower),
Expand Down
30 changes: 30 additions & 0 deletions src/resourses/vars.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{cmp::Ordering, fmt::Display};

use anyhow::anyhow;
use bevy_egui::egui::epaint::util::FloatOrd;
use strum_macros::{Display, EnumString};

use super::*;
Expand Down Expand Up @@ -95,6 +96,7 @@ impl VarValue {
pub fn get_int(&self) -> Result<i32> {
match self {
VarValue::Int(value) => Ok(*value),
VarValue::Float(value) => Ok(*value as i32),
VarValue::Bool(value) => Ok(*value as i32),
VarValue::None => Ok(0),
_ => Err(anyhow!("Int not supported by {self:?}")),
Expand Down Expand Up @@ -255,6 +257,34 @@ impl Display for VarValue {
}
}

impl std::hash::Hash for VarValue {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
VarValue::None => core::mem::discriminant(self).hash(state),
VarValue::Float(v) => (*v).ord().hash(state),
VarValue::Int(v) => (*v).hash(state),
VarValue::Vec2(Vec2 { x, y }) => {
(*x).ord().hash(state);
(*y).ord().hash(state);
}
VarValue::Bool(v) => (*v).hash(state),
VarValue::String(v) => (*v).hash(state),
VarValue::Faction(v) => (*v).hash(state),
VarValue::Entity(v) => (*v).to_bits().hash(state),
VarValue::EntityList(v) => {
for v in v {
(*v).to_bits().hash(state)
}
}
VarValue::Color(v) => {
v.r().ord().hash(state);
v.g().ord().hash(state);
v.b().ord().hash(state);
}
};
}
}

impl VarName {
pub fn show_editor(&mut self, id: impl std::hash::Hash, ui: &mut Ui) {
ComboBox::from_id_source(id)
Expand Down

0 comments on commit 47dded7

Please sign in to comment.