Moving attacks to Domain-Driven Design and Entity-Component architecture
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -193,9 +193,11 @@ class _WolfRendererState extends State<WolfRenderer>
|
||||
damageFlashOpacity = _calculateScreenEffects(damageFlashOpacity);
|
||||
|
||||
// 3. Combat
|
||||
if (player.updateWeapon(elapsed.inMilliseconds)) {
|
||||
_performRaycastAttack(elapsed);
|
||||
}
|
||||
player.updateWeapon(
|
||||
currentTime: elapsed.inMilliseconds,
|
||||
entities: entities,
|
||||
isWalkable: _isWalkable,
|
||||
);
|
||||
|
||||
// 4. Render
|
||||
setState(() {});
|
||||
@@ -206,62 +208,6 @@ class _WolfRendererState extends State<WolfRenderer>
|
||||
damageFlashOpacity = 0.5;
|
||||
}
|
||||
|
||||
void _performRaycastAttack(Duration elapsed) {
|
||||
Enemy? closestEnemy;
|
||||
double minDistance = 15.0;
|
||||
|
||||
for (Entity entity in entities) {
|
||||
if (entity is Enemy && entity.state != EntityState.dead) {
|
||||
double dx = entity.x - player.x;
|
||||
double dy = entity.y - player.y;
|
||||
double angleToEnemy = math.atan2(dy, dx);
|
||||
|
||||
double angleDiff = player.angle - angleToEnemy;
|
||||
while (angleDiff <= -math.pi) {
|
||||
angleDiff += 2 * math.pi;
|
||||
}
|
||||
while (angleDiff > math.pi) {
|
||||
angleDiff -= 2 * math.pi;
|
||||
}
|
||||
|
||||
double dist = math.sqrt(dx * dx + dy * dy);
|
||||
double threshold = 0.2 / dist;
|
||||
|
||||
if (angleDiff.abs() < threshold) {
|
||||
if (_hasLineOfSightToEnemy(entity, dist)) {
|
||||
if (dist < minDistance) {
|
||||
minDistance = dist;
|
||||
closestEnemy = entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (closestEnemy != null) {
|
||||
closestEnemy.takeDamage(
|
||||
player.currentWeapon.damage,
|
||||
elapsed.inMilliseconds,
|
||||
);
|
||||
|
||||
if (closestEnemy.state == EntityState.dead) {
|
||||
player.score += 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool _hasLineOfSightToEnemy(Enemy enemy, double distance) {
|
||||
double dirX = math.cos(player.angle);
|
||||
double dirY = math.sin(player.angle);
|
||||
|
||||
for (double i = 0.5; i < distance; i += 0.2) {
|
||||
int checkX = (player.x + dirX * i).toInt();
|
||||
int checkY = (player.y + dirY * i).toInt();
|
||||
if (!_isWalkable(checkX, checkY)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Returns movement deltas instead of modifying class variables
|
||||
({double dx, double dy}) _processInputs(Duration elapsed) {
|
||||
inputManager.update();
|
||||
|
||||
Reference in New Issue
Block a user