Skip to content

Instantly share code, notes, and snippets.

@Farid357
Created March 22, 2023 14:13
Show Gist options
  • Save Farid357/e2eebfd85ca3af89b101fb5a790be990 to your computer and use it in GitHub Desktop.
Save Farid357/e2eebfd85ca3af89b101fb5a790be990 to your computer and use it in GitHub Desktop.
using System;
using System.Numerics;
using Console_Game.Physics;
using Console_Game.Tools;
namespace Console_Game.Weapons
{
public sealed class LaserWeapon : IWeapon
{
private readonly IRaycast<IEnemy> _raycast;
private readonly ILaserWeaponView _view;
private readonly Vector2 _position;
private readonly Vector2 _shootDirection = new Vector2(1, 0);
private readonly int _damage;
public LaserWeapon(IRaycast<IEnemy> raycast, Vector2 position, ILaserWeaponView view, int damage)
{
_raycast = raycast ?? throw new ArgumentNullException(nameof(raycast));
_position = position;
_view = view ?? throw new ArgumentNullException(nameof(view));
_damage = damage.ThrowIfLessThanZeroException();
}
public bool CanShoot => true;
public void Shoot()
{
_raycast.Throw(_position, _shootDirection);
_view.ShowLaser(_position, _shootDirection);
if (_raycast.HasHit)
{
IHealth enemy = _raycast.HitTarget().Health;
enemy.TakeDamage(_damage);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment