MCPcopy Create free account
hub / github.com/IppClub/Dora-SSR / onAttack

Method onAttack

Source/Platformer/UnitAction.cpp:415–447  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

413}
414
415void MeleeAttack::onAttack() {
416 Sensor* sensor = _owner->getAttackSensor();
417 if (sensor) {
418 ARRAY_START(Body, body, sensor->getSensedBodies()) {
419 Unit* target = DoraAs<Unit>(body->getOwner());
420 BLOCK_START {
421 BREAK_UNLESS(target);
422 bool attackRight = _owner->getPosition().x < target->getPosition().x;
423 bool faceRight = _owner->isFaceRight();
424 BREAK_IF(attackRight != faceRight); // !(hitRight == faceRight || hitLeft == faceLeft)
425 Relation relation = SharedData.getRelation(_owner, target);
426 auto targetAllow = TargetAllow(_owner->getEntity()->get(ActionSetting::TargetAllow, 0u));
427 BREAK_IF(!targetAllow.isAllow(relation));
428 /* Get hit point */
429 Entity* entity = target->getEntity();
430 auto usePreciseHit = _owner->getUnitDef()->get(ActionSetting::UsePreciseHit, false);
431 auto attackPower = _owner->getEntity()->get(ActionSetting::AttackPower, Vec2::zero);
432 Vec2 hitPoint = usePreciseHit ? Attack::getHitPoint(_owner, target, _polygon) : Vec2(target->getPosition());
433 auto data = target->getUserData();
434 data->set(ActionSetting::HitPoint, Value::alloc(hitPoint));
435 data->set(ActionSetting::HitPower, Value::alloc(attackPower));
436 data->set(ActionSetting::HitFromRight, Value::alloc(!attackRight));
437 /* Make damage */
438 float damage = Attack::getDamage(target);
439 entity->set(ActionSetting::HP, entity->get<double>(ActionSetting::HP) - damage);
440 auto attackTarget = _owner->getEntity()->get(ActionSetting::AttackTarget, Slice::Empty);
441 if (attackTarget == "Single"_slice) return true;
442 }
443 BLOCK_END
444 }
445 ARRAY_END
446 }
447}
448
449Own<UnitAction> MeleeAttack::alloc(Unit* unit) {
450 UnitAction* action = new MeleeAttack(unit);

Callers 1

updateMethod · 0.80

Calls 15

make_pairFunction · 0.85
getAttackSensorMethod · 0.80
getSensedBodiesMethod · 0.80
getEntityMethod · 0.80
isAllowMethod · 0.80
getUnitDefMethod · 0.80
getStoreMethod · 0.80
getOrderMethod · 0.80
ARRAY_STARTFunction · 0.70
TargetAllowClass · 0.70
getRelationMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected