src/entity.c
// Assumes entities never attack each other
Assumes entities never attack each other
Assumes entities never attack each other
void attack(Entity *entity, bool isPlayerAttacking)
{
	struct EntityPhysicsProps *attackerProps, *defenderProps;
	struct Combat *attackerCombat, *defenderCombat;
	float defense = isPlayerAttacking ? entity->combat.defense : player.combat.defense + player.bonuses.defense;
	attackerProps = isPlayerAttacking ? &player.props : &entity->props;
	attackerCombat = isPlayerAttacking ? &player.combat : &entity->combat;
	defenderProps = isPlayerAttacking ? &entity->props : &player.props;
	defenderCombat = isPlayerAttacking ? &entity->combat : &player.combat;
	defenderCombat->health -= (attackerCombat->attack - ceil(defense / 2));
	defenderCombat->health = max(0, defenderCombat->health);
	defenderCombat->currImmuneFrames = defenderCombat->immuneFrames;
	defenderProps->yVel = -3.0 * (1.0 - defenderCombat->knockbackResist);
	defenderProps->xVel = (4.0 * sgn(defenderProps->x - attackerProps->x)) * (1.0 - defenderCombat->knockbackResist);
	if(!isPlayerAttacking) player.ticksSinceHit = 0;
}
void attack ( Entity * entity , bool isPlayerAttacking ) { struct EntityPhysicsProps * attackerProps , * defenderProps ; struct Combat * attackerCombat , * defenderCombat ; float defense = isPlayerAttacking ? entity -> combat . defense : player . combat . defense + player . bonuses . defense ; attackerProps = isPlayerAttacking ? & player . props : & entity -> props ; attackerCombat = isPlayerAttacking ? & player . combat : & entity -> combat ; defenderProps = isPlayerAttacking ? & entity -> props : & player . props ; defenderCombat = isPlayerAttacking ? & entity -> combat : & player . combat ; defenderCombat -> health -= ( attackerCombat -> attack - ceil ( defense / 2 ) ) ; defenderCombat -> health = max ( 0 , defenderCombat -> health ) ; defenderCombat -> currImmuneFrames = defenderCombat -> immuneFrames ; defenderProps -> yVel = -3.0 * ( 1.0 - defenderCombat -> knockbackResist ) ; defenderProps -> xVel = ( 4.0 * sgn ( defenderProps -> x - attackerProps -> x ) ) * ( 1.0 - defenderCombat -> knockbackResist ) ; if ( ! isPlayerAttacking ) player . ticksSinceHit = 0 ; }
Assumes entities never attack each other
Assumes entities never attack each other