Skip to main content

cranium_core/
smart_object.rs

1/* 
2This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
3If a copy of the MPL was not distributed with this file, 
4You can obtain one at https://mozilla.org/MPL/2.0/. 
5*/
6
7//! SmartObjects - objects exposing ActionTemplates to compatible AIs, as made famous by The Sims.
8
9use bevy::prelude::*;
10use crate::actionset::ActionSet;
11use crate::types;
12
13
14// The overall design looks like this:
15// 1) Each AI has 0+ (though practically speaking 1+) SmartObjects associated with it at a given moment.
16// 
17// 2) The SmartObjects are added and removed to AIs dynamically, based on the environment (e.g. for NPC AI, 
18//    this is based on the items in the 'general proximity' of the Pawn, whatever that means).
19//    
20//    At the library level, we don't care what those rules are - downstream applications are free to specify 
21//    their own Systems to add and remove SOs at their heart's content, as this is highly contextual.
22//
23// 3) SmartObjects are 'marketing' containers for ActionSets, consumed by AI Controllers. 
24//    Something is a SO if it provides an AI with an ActionSet, based on some predicate (including 'always true'), 
25//    by definition (i.e. anything that does that is an SO, even if we didn't call it that).
26//
27// 4) ActionSets are hot-reloadable Assets.
28// 
29// 5) Therefore, we cannot store the ActionSets raw. Instead, we store a key of the ActionSet.
30// 
31// 6) ...but we still need to be able to recover 'em later as data - so we store them in a CraniumKvMap Resource.
32//
33// 7) Therefore the flow for processing Actions in the AI goes: 
34//    AI -> SmartObject component key -> Res<ActionSetStore> lookup -> ActionSet -> <Actions>
35//
36//    And the Asset flow goes:
37//    File (re)load -> Asset<ActionSet> -> ResMut<ActionSetStore> -> Upsert key ActionSet.name with a *clone* of the Asset's wrapped ActionSet.
38
39
40#[derive(Resource, Default, Reflect)]
41pub struct ActionSetStore {
42    pub map_by_name: types::CraniumKvMap<String, ActionSet>
43}
44
45
46/// A collection of all SmartObjects an Entity has access to at the moment.
47#[derive(Component, Default, Reflect, Clone)]
48#[reflect(Component)]
49pub struct SmartObjects {
50    pub actionset_refs: types::ActionSetsRef
51}