[update] Implemented a damage calculation.

This commit is contained in:
falsycat 2019-10-09 00:00:00 +00:00
parent e17c312871
commit 5e2623adf1
3 changed files with 47 additions and 2 deletions

View File

@ -3,6 +3,7 @@ module sjplayer.CircleElement;
import std.algorithm, import std.algorithm,
std.conv, std.conv,
std.math,
std.exception; std.exception;
import gl4d; import gl4d;
@ -34,7 +35,36 @@ class CircleElement : ElementInterface {
} }
override DamageCalculationResult CalculateDamage(vec2 p1, vec2 p2) const { override DamageCalculationResult CalculateDamage(vec2 p1, vec2 p2) const {
// TODO: if (!alive) return DamageCalculationResult(0, 0);
const m = matrix.transposed.inverse;
const a = (m * vec3(p1, 1)).xy;
const b = (m * vec3(p2, 1)).xy;
const s = b - a;
const s_length = s.length;
if (s_length == 0) {
// TODO: nearness calculation
return DamageCalculationResult(a.length < 1? damage: 0, 0);
}
const d = cross(vec3(s, 0), vec3(a, 0)).length / s_length;
if (d > 1) {
const nearness = (1 - (d-1).min(1f)).pow(2);
return DamageCalculationResult(0, nearness * nearness_coe);
}
const a_length = a.length;
const b_length = b.length;
const hit =
a.dot(s) * b.dot(s) <= 0 ||
a_length < 1 ||
b_length < 1;
if (hit) return DamageCalculationResult(damage, 0);
// TODO: nearness calculation
return DamageCalculationResult(0, 0); return DamageCalculationResult(0, 0);
} }

View File

@ -81,7 +81,14 @@ class Context {
/// ///
ElementInterface.DamageCalculationResult CalculateDamage() const { ElementInterface.DamageCalculationResult CalculateDamage() const {
assert(false); // TODO: auto result = ElementInterface.DamageCalculationResult(0, 0);
elements_.
map!(x => x.CalculateDamage(actor_.pos, actor_.pos - actor_.speed)).
each!((x) {
result.damage += x.damage;
result.nearness += x.nearness;
});
return result;
} }
/// ///

View File

@ -43,6 +43,14 @@ int main(string[] args) {
context.actor.Update(); context.actor.Update();
context.posteffect.Update(); context.posteffect.Update();
const dmg = context.CalculateDamage();
if (dmg.damage != 0) {
"damage: %f (%f)".writefln(dmg.damage, beat);
}
if (dmg.nearness != 0) {
"nearness: %f (%f)".writefln(dmg.nearness, beat);
}
context.StartDrawing(); context.StartDrawing();
context.DrawBackground(); context.DrawBackground();