forked from SFTtech/openage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gamestate: API layer for nyan resistances.
- Loading branch information
Showing
5 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024-2024 the openage authors. See copying.md for legal info. | ||
|
||
#include "resistance.h" | ||
|
||
#include "gamestate/api/definitions.h" | ||
|
||
|
||
namespace openage::gamestate::api { | ||
|
||
bool APIResistance::is_resistance(const nyan::Object &obj) { | ||
for (auto &parent : obj.get_parents()) { | ||
if (parent == "engine.resistance.Resistance") { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool APIResistance::check_effect_type(const nyan::Object &resistance, | ||
const effect_t &type) { | ||
nyan::fqon_t immediate_parent = resistance.get_parents()[0]; | ||
nyan::ValueHolder effect_type = RESISTANCE_DEFS.get(type); | ||
|
||
std::shared_ptr<nyan::ObjectValue> effect_val = std::dynamic_pointer_cast<nyan::ObjectValue>( | ||
effect_type.get_ptr()); | ||
|
||
return effect_val->get_name() == immediate_parent; | ||
} | ||
|
||
bool APIResistance::check_property(const nyan::Object &resistance, | ||
const resistance_property_t &property) { | ||
std::shared_ptr<nyan::Dict> properties = resistance.get<nyan::Dict>("Resistance.properties"); | ||
nyan::ValueHolder property_type = RESISTANCE_PROPERTY_DEFS.get(property); | ||
|
||
return properties->contains(property_type); | ||
} | ||
|
||
effect_t APIResistance::get_effect_type(const nyan::Object &resistance) { | ||
nyan::fqon_t immediate_parent = resistance.get_parents()[0]; | ||
|
||
return RESISTANCE_TYPE_LOOKUP.get(immediate_parent); | ||
} | ||
|
||
const nyan::Object APIResistance::get_property(const nyan::Object &resistance, | ||
const resistance_property_t &property) { | ||
std::shared_ptr<nyan::Dict> properties = resistance.get<nyan::Dict>("Resistance.properties"); | ||
nyan::ValueHolder property_type = RESISTANCE_PROPERTY_DEFS.get(property); | ||
|
||
std::shared_ptr<nyan::View> db_view = resistance.get_view(); | ||
std::shared_ptr<nyan::ObjectValue> property_val = std::dynamic_pointer_cast<nyan::ObjectValue>( | ||
properties->get().at(property_type).get_ptr()); | ||
|
||
return db_view->get_object(property_val->get_name()); | ||
} | ||
|
||
} // namespace openage::gamestate::api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2024-2024 the openage authors. See copying.md for legal info. | ||
|
||
#pragma once | ||
|
||
#include <nyan/nyan.h> | ||
|
||
#include "gamestate/api/types.h" | ||
|
||
|
||
namespace openage::gamestate::api { | ||
|
||
/** | ||
* Helper class for extracting values from Resistance objects in the nyan API. | ||
*/ | ||
class APIResistance { | ||
public: | ||
/** | ||
* Check if a nyan object is an Resistance (type == \p engine.resistance.Resistance). | ||
* | ||
* @param obj nyan object. | ||
* | ||
* @return true if the object is an resistance, else false. | ||
*/ | ||
static bool is_resistance(const nyan::Object &obj); | ||
|
||
/** | ||
* Check if an resistance matches a given effect type. | ||
* | ||
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance). | ||
* @param type Effect type. | ||
* | ||
* @return true if the resistance is of the given type, else false. | ||
*/ | ||
static bool check_effect_type(const nyan::Object &resistance, | ||
const effect_t &type); | ||
|
||
/** | ||
* Check if an resistance has a given property. | ||
* | ||
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance). | ||
* @param property Property type. | ||
* | ||
* @return true if the resistance has the property, else false. | ||
*/ | ||
static bool check_property(const nyan::Object &resistance, | ||
const resistance_property_t &property); | ||
|
||
/** | ||
* Get the matching effect type of a resistance. | ||
* | ||
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance). | ||
* | ||
* @return Type of the resistance. | ||
*/ | ||
static effect_t get_effect_type(const nyan::Object &resistance); | ||
|
||
/** | ||
* Get the nyan object for a property from an resistance. | ||
* | ||
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance). | ||
* @param property Property type. | ||
* | ||
* @return \p Property nyan object (type == \p engine.resistance.property.Property). | ||
*/ | ||
static const nyan::Object get_property(const nyan::Object &resistance, | ||
const resistance_property_t &property); | ||
}; | ||
|
||
} // namespace openage::gamestate::api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters