Skip to content

Commit

Permalink
Start of allowing entites in entites
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklanng committed Apr 15, 2024
1 parent 8810536 commit cd3116e
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 326 deletions.
65 changes: 3 additions & 62 deletions src/data/entity.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import gleam/bool
import gleam/int
import gleam/list
import gleam/option.{type Option, None, Some}
Expand Down Expand Up @@ -61,54 +60,6 @@ pub type Component {
Invisible
}

pub type ComponentType {
TNamed
TPhysical

TPaperDollHead
TPaperDollChest
TPaperDollBack
TPaperDollPrimaryHand
TPaperDollOffHand
TPaperDollLegs
TPaperDollFeet

TEquipable

TMeleeWeapon

TInvulnerable
TSentient
TInvisible
}

fn is_component_type(
component: Component,
component_type: ComponentType,
) -> Bool {
case component_type, component {
TNamed, Named(..) -> True
TPhysical, Physical(..) -> True

TPaperDollHead, PaperDollHead(..) -> True
TPaperDollChest, PaperDollChest(..) -> True
TPaperDollBack, PaperDollBack(..) -> True
TPaperDollPrimaryHand, PaperDollPrimaryHand(..) -> True
TPaperDollOffHand, PaperDollOffHand(..) -> True
TPaperDollLegs, PaperDollLegs(..) -> True
TPaperDollFeet, PaperDollFeet(..) -> True

TEquipable, Equipable(..) -> True

TMeleeWeapon, MeleeWeapon(..) -> True

TInvulnerable, Invulnerable(..) -> True
TSentient, Sentient(..) -> True
TInvisible, Invisible(..) -> True
_, _ -> False
}
}

fn component_priority(component: Component) -> Int {
case component {
Id(_) -> 0
Expand Down Expand Up @@ -138,7 +89,6 @@ pub type Query {
QuerySentient(bool: Bool)
QueryInvisible(bool: Bool)
QueryName(name: Option(String))
QueryNameForced(name: Option(String))
QueryStatus(hp: Option(Int))
QueryEquipable(slots: List(PaperDollSlotType))
QueryPaperDoll(slots: List(#(PaperDollSlotType, Option(String))))
Expand All @@ -155,7 +105,6 @@ pub fn query(entity: Entity, query: Query) -> Query {
fn query_loop(q: Query, c: Component) -> Query {
case c, q {
Named(name), QueryName(_) -> QueryName(name: Some(name))
Named(name), QueryNameForced(_) -> QueryNameForced(name: Some(name))
Physical(hp, _size), QueryStatus(_) -> QueryStatus(hp: Some(hp))
Equipable(slots), QueryEquipable(_) -> QueryEquipable(slots: slots)

Expand Down Expand Up @@ -228,24 +177,16 @@ pub fn add_components(entity: Entity, components: List(Component)) -> Entity {
)
}

pub fn remove_all_components_of_type(
entity: Entity,
component_type: ComponentType,
) {
Entity(
components: entity.components
|> list.filter(fn(component) {
bool.negate(is_component_type(component, component_type))
}),
)
pub fn filter_components(entity: Entity, pred: fn(Component) -> Bool) {
Entity(components: list.filter(entity.components, pred))
}

fn name_optional_entity(entity: Option(Entity)) -> Option(String) {
case entity {
Some(equiped) -> {
case query(equiped, QueryName(None)) {
QueryName(Some(_) as some) -> some
_ -> Some("unknown")
_ -> Some("<unknown>")
}
}
None -> None
Expand Down
Loading

0 comments on commit cd3116e

Please sign in to comment.