Skip to content

Commit

Permalink
Make bullets explode on collision with obstacles (#12)
Browse files Browse the repository at this point in the history
* Move obstacle state to separate file

* Create obstacle destroyer

* Move obstacle configuring to separate file

* Create bullet destroyer

* Create bullet exploder
  • Loading branch information
danliukuri authored Sep 4, 2023
1 parent 822f48d commit 6bdcc5d
Show file tree
Hide file tree
Showing 27 changed files with 292 additions and 48 deletions.
3 changes: 2 additions & 1 deletion Assets/Configurations/Player/Bullet/PlayerBulletConfig.asset

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Configurations/Player/PlayerConfig.asset

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 26 additions & 13 deletions Assets/Prefabs/Player/Bullet/PlayerBullet.prefab

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,54 @@ namespace BlockBreaker.Architecture.GameStates.Gameplay
{
public class SetupGameplayState : IEnterableState
{
private readonly ObstacleDataProvider[] _blockObstacles;
private readonly IObjectPool<PlayerBulletDataProvider> _bulletsPool;
private readonly IComponentConfigurator<ObstacleDataProvider> _obstacleConfigurator;
private readonly ObstaclesProvider _obstaclesProvider;
private readonly IObjectPool<PlayerCarpetDataProvider> _playerCarpetPool;
private readonly IObjectPool<PlayerDataProvider> _playerPool;
private readonly IActiveService[] _services;

public SetupGameplayState(IObjectPool<PlayerCarpetDataProvider> playerCarpetPool,
IObjectPool<PlayerDataProvider> playerPool, ObstacleDataProvider[] blockObstacles,
public SetupGameplayState(IObjectPool<PlayerBulletDataProvider> bulletsPool,
IComponentConfigurator<ObstacleDataProvider> obstacleConfigurator, ObstaclesProvider obstaclesProvider,
IObjectPool<PlayerCarpetDataProvider> playerCarpetPool, IObjectPool<PlayerDataProvider> playerPool,
IActiveService[] services)
{
_bulletsPool = bulletsPool;
_obstacleConfigurator = obstacleConfigurator;
_obstaclesProvider = obstaclesProvider;
_playerCarpetPool = playerCarpetPool;
_playerPool = playerPool;
_blockObstacles = blockObstacles;
_services = services;
}

public void Enter()
{
PlayerDataProvider player = _playerPool.Get();
PlayerCarpetDataProvider carpet = _playerCarpetPool.Get();

SetUpObstacles();
SetUpPlayer(player.Data, carpet);

foreach (IActiveService service in _services)
service.Enable();
}

private void SetUpObstacles()
{
foreach (ObstacleDataProvider obstacle in _obstaclesProvider.ObstacleDataProviders)
_obstacleConfigurator.Configure(obstacle);
_obstaclesProvider.InitializeObstacleData();
}

private void SetUpPlayer(PlayerData player, PlayerCarpetDataProvider carpet)
{
player.SizeConverter = new PlayerSizeConverter(player);
player.SizeCalculator = new PLayerSizeCalculator(player, carpet);
player.SizeSetter = new PlayerSizeSetter(player);
player.CarpetSizeSetter = new PlayerCarpetSizeSetter(carpet.transform);
player.Shooter = new PlayerBulletShooter(player);
player.Shooter = new PlayerBulletShooter(_bulletsPool, player);

float newPlayerSize = player.SizeCalculator.CalculateSize(_blockObstacles);
float newPlayerSize = player.SizeCalculator.CalculateSize(_obstaclesProvider.Obstacles);
player.SizeSetter.Set(newPlayerSize);
player.CarpetSizeSetter.Set(newPlayerSize);
}
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Data/Dynamic/Obstacle.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/Scripts/Data/Dynamic/Obstacle/ObstacleData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using BlockBreaker.Data.Static.Configuration.Obstacle;
using BlockBreaker.Features.Obstacle;
using UnityEngine;

namespace BlockBreaker.Data.Dynamic.Obstacle
{
public class ObstacleData
{
public ObstacleConfig Config { get; set; }
public Transform Transform { get; set; }

public ObstacleDestroyer Destroyer { get; set; }
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Data/Dynamic/Obstacle/ObstacleData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Assets/Scripts/Data/Dynamic/Player/PlayerBulletData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BlockBreaker.Data.Static.Configuration.Player.Bullet;
using BlockBreaker.Features.Player.Bullet;
using UnityEngine;

namespace BlockBreaker.Data.Dynamic.Player
Expand All @@ -11,5 +12,8 @@ public class PlayerBulletData
public Vector3 InitialPosition { get; set; }
public float Size { get; set; }
public float Radius { get; set; }

public PlayerBulletDestroyer Destroyer { get; set; }
public PlayerBulletExploder Exploder { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class PlayerBulletConfig : ScriptableObject
[field: SerializeField, Min(default)] public Vector3 SpawnDirection { get; private set; }
[field: SerializeField, Min(default)] public float Size { get; private set; }
public float Radius => Size / 2f;
[field: SerializeField, Min(default)] public float ExplosionPower { get; private set; }
}
}
21 changes: 21 additions & 0 deletions Assets/Scripts/Features/Obstacle/ObstacleConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using BlockBreaker.Data.Dynamic.Obstacle;
using BlockBreaker.Data.Static.Configuration.Obstacle;
using BlockBreaker.Infrastructure.Services;

namespace BlockBreaker.Features.Obstacle
{
public class ObstacleConfigurator : IComponentConfigurator<ObstacleDataProvider>
{
private readonly ObstacleConfig _config;

public ObstacleConfigurator(ObstacleConfig config) => _config = config;

public void Configure(ObstacleDataProvider component)
{
ObstacleData data = component.Data;
data.Config = _config;
data.Transform = component.transform;
data.Destroyer = new ObstacleDestroyer();
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Features/Obstacle/ObstacleConfigurator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Assets/Scripts/Features/Obstacle/ObstacleDataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using BlockBreaker.Data.Static.Configuration.Obstacle;
using BlockBreaker.Data.Dynamic.Obstacle;
using UnityEngine;
using Zenject;

namespace BlockBreaker.Features.Obstacle
{
public class ObstacleDataProvider : MonoBehaviour
{
public ObstacleConfig Config { get; private set; }
public ObstacleData Data { get; private set; }

[Inject]
public void Construct(ObstacleConfig obstacle) => Config = obstacle;
public void Construct(ObstacleData data) => Data = data;
}
}
9 changes: 9 additions & 0 deletions Assets/Scripts/Features/Obstacle/ObstacleDestroyer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UnityEngine;

namespace BlockBreaker.Features.Obstacle
{
public class ObstacleDestroyer
{
public void Destroy(GameObject gameObject) => gameObject.SetActive(false);
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Features/Obstacle/ObstacleDestroyer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Assets/Scripts/Features/Obstacle/ObstaclesProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using BlockBreaker.Data.Dynamic.Obstacle;

namespace BlockBreaker.Features.Obstacle
{
public class ObstaclesProvider
{
public IEnumerable<ObstacleDataProvider> ObstacleDataProviders { get; }
public ObstacleData[] Obstacles { get; private set; }

public ObstaclesProvider(IEnumerable<ObstacleDataProvider> obstacleDataProviders) =>
ObstacleDataProviders = obstacleDataProviders;

public ObstacleData[] InitializeObstacleData() =>
Obstacles = ObstacleDataProviders.Select(obstacle => obstacle.Data).ToArray();
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Features/Obstacle/ObstaclesProvider.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BlockBreaker.Features.Obstacle;
using UnityEngine;

namespace BlockBreaker.Features.Player.Bullet
{
[RequireComponent(typeof(PlayerBulletDataProvider))]
public class PlayerBulletCollisionHandler : MonoBehaviour
{
private PlayerBulletDataProvider _bulletProvider;

private void Awake() => _bulletProvider = GetComponent<PlayerBulletDataProvider>();

private void OnCollisionEnter(Collision other)
{
if (other.gameObject.TryGetComponent(out ObstacleDataProvider obstacleProvider))
_bulletProvider.Data.Exploder.Explode(_bulletProvider);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 24 additions & 12 deletions Assets/Scripts/Features/Player/Bullet/PlayerBulletConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,42 @@ namespace BlockBreaker.Features.Player.Bullet
{
public class PlayerBulletConfigurator : IComponentConfigurator<PlayerBulletDataProvider>
{
private readonly PlayerBulletData _bullet;
private readonly PlayerBulletDestroyer _bulletDestroyer;
private readonly PlayerBulletExploder _bulletExploder;
private readonly PlayerBulletConfig _config;
private readonly PlayerData _player;

public PlayerBulletConfigurator(PlayerBulletData bullet, PlayerBulletConfig config, PlayerData player)
public PlayerBulletConfigurator(PlayerBulletDestroyer bulletDestroyer, PlayerBulletExploder bulletExploder,
PlayerBulletConfig config, PlayerData player)
{
_bullet = bullet;
_bulletDestroyer = bulletDestroyer;
_bulletExploder = bulletExploder;
_config = config;
_player = player;
}

public void Configure(PlayerBulletDataProvider component)
{
Transform transform = _bullet.Transform = component.transform;
_bullet.Rigidbody = component.GetComponent<Rigidbody>();
_bullet.Config = _config;
_bullet.Size = _config.Size;
_bullet.Radius = _config.Radius;
PlayerBulletData bullet = component.Data;
bullet.InitialPosition = ConfigureInitialPosition();
bullet.Transform = ConfigureTransform(component.transform, bullet);
bullet.Rigidbody = component.GetComponent<Rigidbody>();
bullet.Config = _config;
bullet.Size = _config.Size;
bullet.Radius = _config.Radius;
bullet.Destroyer = _bulletDestroyer;
bullet.Exploder = _bulletExploder;
}

transform.localScale = Vector3.one * _config.Size;
private Vector3 ConfigureInitialPosition() =>
_config.SpawnDirection * (_player.Radius + _config.Radius) + _player.InitialPosition;

Vector3 initialPosition = _bullet.InitialPosition =
_config.SpawnDirection * (_player.Radius + _config.Radius) + _player.InitialPosition;
transform.position = new Vector3(initialPosition.x, initialPosition.y + _bullet.Radius, initialPosition.z);
private Transform ConfigureTransform(Transform transform, PlayerBulletData bullet)
{
transform.localScale = Vector3.one * _config.Size;
Vector3 initialPosition = bullet.InitialPosition;
transform.position = new Vector3(initialPosition.x, initialPosition.y + bullet.Radius, initialPosition.z);
return transform;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Features/Player/Bullet/PlayerBulletDestroyer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using UnityEngine.Pool;

namespace BlockBreaker.Features.Player.Bullet
{
public class PlayerBulletDestroyer
{
public IObjectPool<PlayerBulletDataProvider> BulletsPool { get; set; }

public void Destroy(PlayerBulletDataProvider bullet) => BulletsPool?.Release(bullet);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6bdcc5d

Please sign in to comment.