Skip to content

Commit

Permalink
Raycast, Laser Weapon
Browse files Browse the repository at this point in the history
  • Loading branch information
Farid357 committed Mar 22, 2023
1 parent 9ddb4e7 commit 5cc541d
Show file tree
Hide file tree
Showing 80 changed files with 636 additions and 212 deletions.
4 changes: 3 additions & 1 deletion Console Game/Game/Runtime/Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Drawing;
using System.Numerics;
using Console_Game.Loop;
using Console_Game.Physics;
using Console_Game.Save_Storages;
using Console_Game.UI;
using Console_Game.Weapons;
Expand All @@ -24,7 +25,8 @@ public Game()
ITextFactory textFactory = new TextFactory(uiElementFactory);
IFactory<IWallet> walletFactory = new WalletFactory(saveStorages, textFactory);
IWeaponInput weaponInput = new WeaponInput(new Key(ConsoleKey.F));
IBulletsFactory bulletsFactory = new BulletsFactory(new Transform(), gameLoopObjects);
IReadOnlyCollidersWorld<IEnemy> enemyWorld = new CollidersWorld<IEnemy>();
IBulletsFactory bulletsFactory = new RaycastBulletsFactory(enemyWorld, new Transform(), gameLoopObjects);
IText bulletsText = textFactory.Create(new Transform(new Vector2(1.5f, 1.3f)));
IFactory<IWeaponWithMagazine> weaponFactory = new StartPlayerWeaponFactory(gameLoopObjects, bulletsText, bulletsFactory);
IWeaponWithMagazine weapon = weaponFactory.Create();
Expand Down
3 changes: 2 additions & 1 deletion Console Game/Game/Runtime/Game/Loop/GameLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public sealed class GameLoop : IGameLoop
{
private readonly IReadOnlyGameTimer _gameTimer;
private readonly IReadOnlyGamePause _gamePause;
private readonly IGameLoopObjects _gameLoopObjects = new GameLoopObjects();
private readonly IGameLoopObjects _gameLoopObjects;

private readonly float _timeStep;
private float _lastUpdateTime;
Expand All @@ -17,6 +17,7 @@ public GameLoop(IReadOnlyGameTimer gameTimer, float timeStep, IReadOnlyGamePause
{
_gameTimer = gameTimer ?? throw new ArgumentNullException(nameof(gameTimer));
_gamePause = gamePause ?? throw new ArgumentNullException(nameof(gamePause));
_gameLoopObjects = new GameLoopObjects();
_timeStep = timeStep.ThrowIfLessOrEqualsToZeroException();
}

Expand Down
36 changes: 0 additions & 36 deletions Console Game/Game/Runtime/GameObject/Model/GameObject.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public GameObjectWithLifeTime(IGameObject gameObject, ITimer lifeTime)

public bool IsActive => _gameObject.IsActive;

public void Enable() => _gameObject.Enable();

public void Disable() => _gameObject.Disable();
// public void Enable() => _gameObject.Enable();
//
// public void Disable() => _gameObject.Disable();

public void Destroy() => _gameObject.Destroy();

Expand Down
38 changes: 20 additions & 18 deletions Console Game/Game/Runtime/GameObject/Model/GameObjects.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Console_Game
{
Expand All @@ -13,34 +12,37 @@ public GameObjects(List<IGameObject> gameObjects)
_gameObjects = gameObjects ?? throw new ArgumentNullException(nameof(gameObjects));
}

public GameObjects() : this(new List<IGameObject>())
{

}

public bool IsActive => _gameObjects.All(gameObject => gameObject.IsActive);
public bool IsActive { get; private set; }

public IReadOnlyList<IGameObject> All => _gameObjects;

public void Enable()
{
_gameObjects.ForEach(gameObject => gameObject.Enable());
}

public void Disable()
{
_gameObjects.ForEach(gameObject => gameObject.Disable());
}
// public void Enable()
// {
// _gameObjects.ForEach(gameObject => gameObject.Enable());
// }
//
// public void Disable()
// {
// _gameObjects.ForEach(gameObject => gameObject.Disable());
// }

public void Destroy()
{
IsActive = false;
_gameObjects.ForEach(gameObject => gameObject.Destroy());
}


public void Add(IGameObject instance) => _gameObjects.Add(instance);

public void Remove(IGameObject instance) => _gameObjects.Remove(instance);


public void Update(float deltaTime)
{
foreach (var gameObject in _gameObjects)
{
if (gameObject.IsActive)
gameObject.Update(deltaTime);
}
}
}
}
8 changes: 4 additions & 4 deletions Console Game/Game/Runtime/GameObject/Model/IGameObject.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Console_Game
{
public interface IGameObject
public interface IGameObject : IGameLoopObject
{
bool IsActive { get; }

void Enable();

void Disable();
// void Enable();
//
// void Disable();

void Destroy();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;

namespace Console_Game
{
public sealed class SelfCleaningGameObjects : IGameObjects
{
private readonly IGameObjects _gameObjects;

public SelfCleaningGameObjects(IGameObjects gameObjects)
{
_gameObjects = gameObjects ?? throw new ArgumentNullException(nameof(gameObjects));
}

public bool IsActive => _gameObjects.IsActive;

public IReadOnlyList<IGameObject> All => _gameObjects.All;

public void Add(IGameObject instance)
{
_gameObjects.Add(instance);
}

public void Remove(IGameObject instance)
{
_gameObjects.Remove(instance);
}

public void Update(float deltaTime)
{
_gameObjects.Update(deltaTime);
CleanNotActiveObjects();
}

private void CleanNotActiveObjects()
{
foreach (var gameObject in All)
{
if (gameObject.IsActive == false)
_gameObjects.Remove(gameObject);
}
}

public void Destroy()
{
_gameObjects.Destroy();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using Console_Game.Stats;
using Console_Game.UI;

namespace Console_Game
{
public sealed class CharacterLevelsFactory : ILevelsFactory
{
private readonly ITextFactory _textFactory;

public CharacterLevelsFactory(ITextFactory textFactory)
{
_textFactory = textFactory ?? throw new ArgumentNullException(nameof(textFactory));
}

public List<ILevel> Create()
{
ILevelView levelView = new LevelView("Player");
ITransform textTransform = new Transform(new Vector2(100, 250));
IText text = _textFactory.Create(textTransform);
ILevelView levelView = new LevelView("Player", text);

return new List<ILevel>
{
Expand Down
9 changes: 6 additions & 3 deletions Console Game/Game/Runtime/Level/View/LevelView.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
using System;
using Console_Game.UI;

namespace Console_Game.Stats
{
[Serializable]
public sealed class LevelView : ILevelView
{
private readonly string _levelOwnerName;

public LevelView(string levelOwnerName)
private readonly IText _text;

public LevelView(string levelOwnerName, IText text)
{
_levelOwnerName = levelOwnerName ?? throw new ArgumentNullException(nameof(levelOwnerName));
_text = text ?? throw new ArgumentNullException(nameof(text));
}

public void Visualize(int xp, int maxXp)
{
Console.WriteLine($"{_levelOwnerName} Xp: {xp}, MaxXp: {maxXp}");
_text.Visualize($"{_levelOwnerName} Xp: {xp}, MaxXp: {maxXp}");
}
}
}
15 changes: 15 additions & 0 deletions Console Game/Game/Runtime/Physics/Colliders/Box.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Numerics;

namespace Console_Game.Physics
{
public struct Box
{
public Box(Vector3 size)
{
Size = size;
}

public Vector3 Size { get; }

}
}
22 changes: 22 additions & 0 deletions Console Game/Game/Runtime/Physics/Colliders/BoxCollider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Numerics;

namespace Console_Game.Physics
{
public sealed class BoxCollider : ICollider
{
private readonly Box _box;

public BoxCollider(Box box, Vector3 center)
{
_box = box;
Center = center;
}

public Vector3 Center { get; }

public bool Contains(Vector3 point)
{
return Vector3.Distance(Center, point) <= Vector3.Distance(Center, _box.Size);
}
}
}
11 changes: 11 additions & 0 deletions Console Game/Game/Runtime/Physics/Colliders/ICollider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Numerics;

namespace Console_Game.Physics
{
public interface ICollider
{
Vector3 Center { get; }

bool Contains(Vector3 point);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;

namespace Console_Game.Physics
{
public sealed class CollidersWorld<TModel> : ICollidersWorld<TModel>
{
private readonly Dictionary<ICollider, TModel> _models;

public CollidersWorld()
{
_models = new Dictionary<ICollider, TModel>();
}

public IReadOnlyDictionary<ICollider, TModel> Models => _models;

public void Add(ICollider collider, TModel model)
{
_models.Add(collider, model);
}

public void Remove(ICollider collider)
{
_models.Remove(collider);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;

namespace Console_Game.Physics
{
public sealed class GameObjectsCollidersWorld<TModel> : IGameLoopObject, ICollidersWorld<TModel> where TModel : IGameObject
{
private readonly ICollidersWorld<TModel> _world;

public GameObjectsCollidersWorld(ICollidersWorld<TModel> world)
{
_world = world ?? throw new ArgumentNullException(nameof(world));
}

public IReadOnlyDictionary<ICollider, TModel> Models => _world.Models;

public void Add(ICollider collider, TModel model) => _world.Add(collider, model);

public void Remove(ICollider collider) => _world.Remove(collider);

public void Update(float deltaTime)
{
foreach (var keyPair in _world.Models)
{
ICollider collider = keyPair.Key;
IGameObject gameObject = keyPair.Value;

if (gameObject.IsActive == false)
Remove(collider);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Console_Game.Physics
{
public interface ICollidersWorld<TModel> : IReadOnlyCollidersWorld<TModel>
{
void Add(ICollider collider, TModel model);

void Remove(ICollider collider);
}
}
Loading

0 comments on commit 5cc541d

Please sign in to comment.