Skip to content

Commit

Permalink
Merge pull request #12 from marceline-cramer/marceline-tobias-modjam
Browse files Browse the repository at this point in the history
Passive regen + other fixes
  • Loading branch information
chaosprint authored Jul 25, 2023
2 parents 434496c + be7d87f commit 65262c8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/fpsrule/ambient.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ player_health = { type = "I32", attributes = ["Debuggable", "Networked"] }

hit_freeze = { type = "I32", attributes = ["Debuggable", "Networked"] }

heal_timeout = { type = "I32", attributes = ["Debuggable"] }

[messages.shoot.fields]
ray_origin = { type = "Vec3" }
Expand Down
51 changes: 45 additions & 6 deletions src/fpsrule/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use ambient_api::components::core::{player::player, transform::translation};
use ambient_api::prelude::*;
use components::{heal_timeout, player_health};

#[main]
pub fn main() {
spawn_query(player()).bind(|results| {
Expand All @@ -12,22 +14,26 @@ pub fn main() {
entity::add_component(id, components::hit_freeze(), 0);
entity::add_component(id, components::player_killcount(), 0);
entity::add_component(id, components::player_deathcount(), 0);
entity::add_component(id, components::heal_timeout(), 0);
});
}
});

messages::Shoot::subscribe(move |_source, msg| {
let result = physics::raycast_first(msg.ray_origin, msg.ray_dir);

if let Some(hit) = result {
if entity::has_component(hit.entity, components::player_health()) {
let old_health = entity::get_component(hit.entity, components::player_health());
if old_health.is_none() {
return;
}
let old_health = old_health.unwrap();
if hit.entity == msg.source {
eprintln!("self hit");
return;
}

if let Some(old_health) = entity::get_component(hit.entity, components::player_health())
{
if old_health <= 0 {
return;
}

let new_health = (old_health - 10).max(0);
entity::set_component(hit.entity, components::player_health(), new_health);

Expand All @@ -46,6 +52,11 @@ pub fn main() {
);
run_async(async move {
sleep(114. / 60.).await;

if !entity::exists(hit.entity) {
return;
}

entity::set_component(
hit.entity,
translation(),
Expand All @@ -56,6 +67,34 @@ pub fn main() {
});
} else {
entity::set_component(hit.entity, components::hit_freeze(), 20);
entity::set_component(hit.entity, heal_timeout(), 150);
}
}
}
});

query((player(), heal_timeout())).each_frame(move |entities| {
for (e, (_, old_timeout)) in entities {
let new_timeout = old_timeout - 1;
entity::set_component(e, heal_timeout(), new_timeout);
}
});

let healables = query((player(), player_health())).build();
run_async(async move {
loop {
sleep(1.0).await;

for (e, (_, old_health)) in healables.evaluate() {
if let Some(timeout) = entity::get_component(e, components::heal_timeout()) {
if timeout > 0 {
continue;
}
}

let new_health = old_health + 1;
if new_health <= 100 {
entity::set_component(e, components::player_health(), new_health);
}
}
}
Expand Down

0 comments on commit 65262c8

Please sign in to comment.