Skip to content

Commit f39edee

Browse files
committed
Refine type of AttackableEntities.
GitOrigin-RevId: 80a26b6c7ce3e0bbddcdc7b5181db914b706b79b
1 parent a5ab5ca commit f39edee

File tree

4 files changed

+88
-75
lines changed

4 files changed

+88
-75
lines changed

hera/behavior/Attack.tsx

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,51 @@ export default class Attack {
2222
): StateLike | null {
2323
const entities = getAttackableEntities(vector, state);
2424
const { attackable, radius, selectedUnit } = state;
25-
if (attackable && radius) {
25+
if (attackable && radius && entities) {
2626
const entityB = entities.unit || entities.building;
27-
if (entityB) {
28-
const onAction = (state: State) => {
29-
if (entities.unit && entities.building) {
30-
return {
31-
attackable: new Map([[vector, attackable.get(vector)!]]),
32-
confirmAction: null,
33-
radius: {
34-
...radius,
35-
fields: new Map([[vector, radius.fields.get(vector)!]]),
36-
},
37-
selectedAttackable: vector,
38-
showCursor: false,
39-
};
40-
} else if (entityB && selectedUnit?.getAttackWeapon(entityB)) {
41-
const { selectedPosition, selectedUnit } = state;
42-
if (selectedPosition && selectedUnit) {
43-
requestAnimationFrame(() =>
44-
attackAction(
45-
actions,
46-
selectedPosition,
47-
selectedUnit,
48-
vector,
49-
entityB,
50-
state,
51-
),
52-
);
53-
}
27+
const onAction = (state: State) => {
28+
if (entities.unit && entities.building) {
29+
return {
30+
attackable: new Map([[vector, attackable.get(vector)!]]),
31+
confirmAction: null,
32+
radius: {
33+
...radius,
34+
fields: new Map([[vector, radius.fields.get(vector)!]]),
35+
},
36+
selectedAttackable: vector,
37+
showCursor: false,
38+
};
39+
} else if (entityB && selectedUnit?.getAttackWeapon(entityB)) {
40+
const { selectedPosition, selectedUnit } = state;
41+
if (selectedPosition && selectedUnit) {
42+
requestAnimationFrame(() =>
43+
attackAction(
44+
actions,
45+
selectedPosition,
46+
selectedUnit,
47+
vector,
48+
entityB,
49+
state,
50+
),
51+
);
5452
}
55-
return { confirmAction: null };
56-
};
53+
}
54+
return { confirmAction: null };
55+
};
5756

58-
return shouldConfirm
59-
? {
60-
confirmAction: {
61-
icon: 'attack',
62-
onAction,
63-
position: vector,
64-
},
65-
radius: {
66-
...radius,
67-
locked: true,
68-
},
69-
}
70-
: onAction(state);
71-
}
57+
return shouldConfirm
58+
? {
59+
confirmAction: {
60+
icon: 'attack',
61+
onAction,
62+
position: vector,
63+
},
64+
radius: {
65+
...radius,
66+
locked: true,
67+
},
68+
}
69+
: onAction(state);
7270
}
7371

7472
return null;

hera/behavior/Move.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ export default class Move {
300300
);
301301
const moveToField = field || (parent ? RadiusItem(parent) : null);
302302
const entities = getAttackableEntities(vector, state);
303-
304303
if (moveToField && parent && entities) {
305304
const onAction = (state: State) => {
306305
const entity = entities.unit || entities.building;
@@ -320,15 +319,15 @@ export default class Move {
320319
selectedAttackable: vector,
321320
showCursor: false,
322321
};
323-
} else if (entity && selectedUnit.getAttackWeapon(entity)) {
322+
} else if (selectedUnit.getAttackWeapon(entity)) {
324323
requestAnimationFrame(() =>
325324
moveAndAttack(
326325
actions,
327326
selectedPosition,
328327
parent,
329328
vector,
330329
path,
331-
(entities.unit || entities.building)!,
330+
entity,
332331
radius.fields,
333332
state,
334333
),

hera/behavior/attack/AttackSelector.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,27 @@ export default function AttackSelector({
5858
return getAttackableEntities(position, state);
5959
}
6060
}
61-
return { building: null, unit: null };
61+
return null;
6262
}, [position, selectedAttackable, selectedPosition, selectedUnit, state]);
6363

6464
const selectBuilding = useCallback(() => {
65-
if (entities.building) {
65+
if (entities?.building) {
6666
onSelect(entities.building);
6767
}
68-
}, [entities.building, onSelect]);
68+
}, [entities, onSelect]);
6969

7070
const selectUnit = useCallback(() => {
71-
if (entities.unit) {
71+
if (entities?.unit) {
7272
onSelect(entities.unit);
7373
}
74-
}, [entities.unit, onSelect]);
74+
}, [entities, onSelect]);
7575

7676
if (!selectedPosition || !selectedUnit) {
7777
return null;
7878
}
7979

8080
if (selectedAttackable) {
81-
if (entities.unit && entities.building) {
81+
if (entities?.unit && entities.building) {
8282
return (
8383
<EntityPickerFlyout
8484
animationConfig={animationConfig}
@@ -98,7 +98,7 @@ export default function AttackSelector({
9898
);
9999
}
100100
} else if (position) {
101-
if (entities.building || entities.unit) {
101+
if (entities) {
102102
const isShortRange = selectedUnit.info.isShortRange();
103103
const targetPosition = origin || selectedPosition;
104104
const futureMap =

hera/behavior/attack/getAttackableEntities.tsx

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@ import Unit from '@deities/athena/map/Unit.tsx';
33
import Vector from '@deities/athena/map/Vector.tsx';
44
import { State } from '../../Types.tsx';
55

6-
type AttackableEntities = {
7-
building: Building | null;
8-
unit: Unit | null;
9-
};
6+
type AttackableEntities =
7+
| Readonly<{
8+
building: Building;
9+
unit: Unit;
10+
}>
11+
| {
12+
building: Building;
13+
unit?: null;
14+
}
15+
| {
16+
building?: null;
17+
unit: Unit;
18+
}
19+
| null;
1020

1121
export default function getAttackableEntities(
1222
vector: Vector,
@@ -19,22 +29,28 @@ export default function getAttackableEntities(
1929
attackable.has(vector) &&
2030
vision.isVisible(map, vector)
2131
) {
22-
const unit = map.units.get(vector);
23-
const building = map.buildings.get(vector);
24-
return {
25-
building:
26-
building &&
27-
map.isOpponent(building, selectedUnit) &&
28-
selectedUnit.getAttackWeapon(building)
29-
? building
30-
: null,
31-
unit:
32-
unit &&
33-
map.isOpponent(unit, selectedUnit) &&
34-
selectedUnit.getAttackWeapon(unit)
35-
? unit
36-
: null,
37-
};
32+
const targetUnit = map.units.get(vector);
33+
const targetBuilding = map.buildings.get(vector);
34+
const building =
35+
targetBuilding &&
36+
map.isOpponent(targetBuilding, selectedUnit) &&
37+
selectedUnit.getAttackWeapon(targetBuilding)
38+
? targetBuilding
39+
: null;
40+
const unit =
41+
targetUnit &&
42+
map.isOpponent(targetUnit, selectedUnit) &&
43+
selectedUnit.getAttackWeapon(targetUnit)
44+
? targetUnit
45+
: null;
46+
47+
return building && unit
48+
? { building, unit }
49+
: building
50+
? { building }
51+
: unit
52+
? { unit }
53+
: null;
3854
}
39-
return { building: null, unit: null };
55+
return null;
4056
}

0 commit comments

Comments
 (0)