Skip to content

Commit 96e0631

Browse files
Aaron-Goldmanmschwartz
authored andcommitted
simplify attack check process (#235)
1 parent aa07dba commit 96e0631

File tree

1 file changed

+30
-50
lines changed

1 file changed

+30
-50
lines changed

src/GameState/enemies/GEnemyProcess.cpp

+30-50
Original file line numberDiff line numberDiff line change
@@ -198,67 +198,47 @@ TBool GEnemyProcess::MaybeHit() {
198198
return EFalse;
199199
}
200200

201-
static const TFloat DX = 20,
202-
DY = 26;
201+
static const TInt DX = 8,
202+
DY = 8;
203203

204204
TBool GEnemyProcess::MaybeAttack() {
205205
TRect myRect, hisRect;
206206
mSprite->GetRect(myRect);
207207
mPlayerSprite->GetRect(hisRect);
208208

209209
if (!mPlayerSprite->mInvulnerable) {
210-
if (myRect.x1 >= hisRect.x2) {
211-
// to right of player
212-
if (ABS(hisRect.x2 - myRect.x1) > DX) {
213-
mAttackTimer = 1;
214-
return EFalse;
215-
}
216-
if (ABS(mPlayerSprite->y - mSprite->y) > DY) {
217-
mAttackTimer = 1;
218-
return EFalse;
219-
}
220-
if (--mAttackTimer <= 0) {
221-
NewState(ATTACK_STATE, DIRECTION_LEFT);
222-
}
223-
return ETrue;
224-
} else if (myRect.x2 <= hisRect.x1) {
225-
// to left of player
226-
if (ABS(hisRect.x1 - myRect.x2) > DX) {
227-
mAttackTimer = 1;
228-
return EFalse;
229-
}
230-
if (ABS(mPlayerSprite->y - mSprite->y) > DY) {
231-
mAttackTimer = 1;
232-
return EFalse;
233-
}
234-
if (--mAttackTimer <= 0) {
235-
NewState(ATTACK_STATE, DIRECTION_RIGHT);
236-
}
237-
return ETrue;
238-
}
239-
240-
if (myRect.y1 >= hisRect.y2) {
241-
// enemy below player
242-
if (ABS(mPlayerSprite->y - mSprite->y) > DY) {
243-
// too far away
244-
mAttackTimer = 1;
245-
return EFalse;
210+
if (myRect.y1 <= hisRect.y2 && myRect.y2 >= hisRect.y1) {
211+
// vertical overlap
212+
if (myRect.x1 >= hisRect.x2 && myRect.x1 - hisRect.x2 < DX) {
213+
// to right of player
214+
if (--mAttackTimer <= 0) {
215+
NewState(ATTACK_STATE, DIRECTION_LEFT);
216+
}
217+
return ETrue;
246218
}
247-
if (--mAttackTimer <= 0) {
248-
NewState(ATTACK_STATE, DIRECTION_UP);
219+
if (myRect.x2 <= hisRect.x1 && hisRect.x1 - myRect.x2 < DX) {
220+
// to left of player
221+
if (--mAttackTimer <= 0) {
222+
NewState(ATTACK_STATE, DIRECTION_RIGHT);
223+
}
224+
return ETrue;
249225
}
250-
return ETrue;
251-
} else if (myRect.y2 <= hisRect.y1) {
252-
// enemy above player
253-
if (ABS(mPlayerSprite->y - mSprite->y) > DY) {
254-
// too far away
255-
mAttackTimer = 1;
256-
return EFalse;
226+
} else if (myRect.x1 <= hisRect.x2 && myRect.x2 >= hisRect.x1) {
227+
// horizontal overlap
228+
if (myRect.y1 >= hisRect.y2 && myRect.y1 - hisRect.y2 < DY) {
229+
// below player
230+
if (--mAttackTimer <= 0) {
231+
NewState(ATTACK_STATE, DIRECTION_UP);
232+
}
233+
return ETrue;
257234
}
258-
if (--mAttackTimer <= 0) {
259-
NewState(ATTACK_STATE, DIRECTION_DOWN);
235+
if (myRect.y2 <= hisRect.y1 && hisRect.y1 - myRect.y2 < DY) {
236+
// above player
237+
if (--mAttackTimer <= 0) {
238+
NewState(ATTACK_STATE, DIRECTION_DOWN);
239+
}
240+
return ETrue;
260241
}
261-
return ETrue;
262242
}
263243

264244
// player not near us

0 commit comments

Comments
 (0)