| 413 | } |
| 414 | |
| 415 | void 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 | |
| 449 | Own<UnitAction> MeleeAttack::alloc(Unit* unit) { |
| 450 | UnitAction* action = new MeleeAttack(unit); |
no test coverage detected