2020-04-28 23:54:23 +02:00
|
|
|
/*
|
2021-04-19 10:13:51 +02:00
|
|
|
Copyright (c) 2019-2021 Péter Magyar
|
2020-04-28 23:54:23 +02:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "entity_resource_health.h"
|
|
|
|
|
|
|
|
#include "../../database/ess_resource_db.h"
|
|
|
|
#include "../../singletons/ess.h"
|
|
|
|
#include "../entity.h"
|
2020-06-20 15:26:21 +02:00
|
|
|
#include "entity_resource.h"
|
2020-04-28 23:54:23 +02:00
|
|
|
|
|
|
|
void EntityResourceHealth::_init() {
|
|
|
|
set_current_value(100);
|
|
|
|
stamina_stat_id = 0;
|
|
|
|
health_stat_id = 0;
|
|
|
|
|
2020-05-23 16:29:47 +02:00
|
|
|
if (ESS::get_singleton()->stat_is_property("Stamina")) {
|
|
|
|
stamina_stat_id = ESS::get_singleton()->stat_get_id("Stamina");
|
2020-04-28 23:54:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 16:29:47 +02:00
|
|
|
if (ESS::get_singleton()->stat_is_property("Health")) {
|
|
|
|
health_stat_id = ESS::get_singleton()->stat_get_id("Health");
|
2020-04-28 23:54:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void EntityResourceHealth::_ons_added(Node *entity) {
|
|
|
|
refresh();
|
|
|
|
}
|
2020-05-01 17:15:44 +02:00
|
|
|
void EntityResourceHealth::_notification_sstat_changed(int statid, float current) {
|
|
|
|
if (statid == stamina_stat_id || statid == health_stat_id)
|
2020-04-28 23:54:23 +02:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
void EntityResourceHealth::refresh() {
|
2020-04-29 01:05:31 +02:00
|
|
|
ERR_FAIL_COND(get_owner() == NULL);
|
|
|
|
|
2020-05-01 17:15:44 +02:00
|
|
|
float stamina = get_owner()->stat_gets_current(stamina_stat_id);
|
|
|
|
float health = get_owner()->stat_gets_current(health_stat_id);
|
2020-04-29 01:05:31 +02:00
|
|
|
|
2020-05-01 17:15:44 +02:00
|
|
|
int val = int(stamina) * 10 + int(health);
|
2020-04-28 23:54:23 +02:00
|
|
|
|
|
|
|
set_max_value(val);
|
|
|
|
set_current_value(val);
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:05:31 +02:00
|
|
|
void EntityResourceHealth::resolve_references() {
|
|
|
|
}
|
|
|
|
|
2020-04-28 23:54:23 +02:00
|
|
|
EntityResourceHealth::EntityResourceHealth() {
|
|
|
|
stamina_stat_id = 0;
|
|
|
|
health_stat_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityResourceHealth::~EntityResourceHealth() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityResourceHealth::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("_init"), &EntityResourceHealth::_init);
|
|
|
|
ClassDB::bind_method(D_METHOD("_ons_added", "entity"), &EntityResourceHealth::_ons_added);
|
2020-05-01 17:15:44 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_notification_sstat_changed", "statid", "current"), &EntityResourceHealth::_notification_sstat_changed);
|
2020-04-28 23:54:23 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("refresh"), &EntityResourceHealth::refresh);
|
|
|
|
}
|