Platformer example started
This commit is contained in:
1 parent
efcfaae647
commit
9f026b76d6
325 files changed
+23540
-270
No files matched your search
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95e867912f25463e8ae222819491ee0d
|
||||
timeCreated: 1770905985
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9df78f95b3ad416cb1a0a786f0b22365
|
||||
timeCreated: 1771423438
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DG.Tweening;
|
||||
using DG.Tweening.Core;
|
||||
using DG.Tweening.Plugins.Options;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer.Entity.Enemies
|
||||
{
|
||||
public class Bee : Entity
|
||||
{
|
||||
[SerializeField] private List<Transform> partolPoints;
|
||||
private bool _onPatrol;
|
||||
private bool _onPatrolMoveDamnit;
|
||||
private int _currentPartolPoint;
|
||||
|
||||
private Transform _currentTarget;
|
||||
private TweenerCore<Vector3, Vector3, VectorOptions> _patrolAction;
|
||||
|
||||
protected override void Move()
|
||||
{
|
||||
if (_onPatrol && !_onPatrolMoveDamnit)
|
||||
{
|
||||
_onPatrolMoveDamnit = true;
|
||||
_patrolAction = transform.DOMove(partolPoints[_currentPartolPoint].position, 3f);
|
||||
_patrolAction.onComplete += OnControlPointReached;
|
||||
}
|
||||
else
|
||||
{
|
||||
var direction = _currentTarget.position - transform.position;
|
||||
direction.Normalize();
|
||||
var targetPosition = _currentTarget.position + direction * -2f;
|
||||
|
||||
UnityEngine.Debug.DrawLine(transform.position, targetPosition, Color.red);
|
||||
|
||||
transform.DOMove(_currentTarget.position, 2f).onComplete += OnControlPointReached;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnControlPointReached()
|
||||
{
|
||||
if (++_currentPartolPoint >= partolPoints.Count) _currentPartolPoint = 0;
|
||||
_onPatrolMoveDamnit = false;
|
||||
}
|
||||
|
||||
public void GotTarget(Transform other)
|
||||
{
|
||||
_onPatrol = false;
|
||||
_currentTarget = other;
|
||||
_patrolAction?.Complete();
|
||||
}
|
||||
|
||||
public void LostTarget()
|
||||
{
|
||||
_onPatrol = true;
|
||||
_currentTarget = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2abf74ae7a894c5abc850fe852fdf4e7
|
||||
timeCreated: 1771433363
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer.Entity.Enemies
|
||||
{
|
||||
public class BeeRadar : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Bee bee;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) => bee.GotTarget(other.transform);
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other) => bee.LostTarget();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 582294c360404f6d952d9e28bde1fbea
|
||||
timeCreated: 1771434696
|
||||
@@ -0,0 +1,32 @@
|
||||
using Code.Infrastructure.Configs;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer.Entity.Enemies
|
||||
{
|
||||
public class Snail : Entity
|
||||
{
|
||||
[SerializeField] private Vector2 groundLeft;
|
||||
[SerializeField] private Vector2 groundRight;
|
||||
[SerializeField] private bool _directionRight;
|
||||
|
||||
protected override void Move()
|
||||
{
|
||||
CheckupDirection();
|
||||
rigidbody2D.linearVelocityX = _directionRight ? 0.5f : -0.5f;
|
||||
}
|
||||
protected override void HandleFlipX() => spriteRenderer.flipX = rigidbody2D.linearVelocity.x > -0.01f;
|
||||
|
||||
private void CheckupDirection()
|
||||
{
|
||||
if (_directionRight)
|
||||
{
|
||||
if (!Physics2D.OverlapPoint((Vector2)transform.position + groundRight,
|
||||
LayerMasks.Masks[LayerMaskType.PlatformerGround]))
|
||||
_directionRight = false;
|
||||
}
|
||||
else if (!Physics2D.OverlapPoint((Vector2)transform.position + groundLeft,
|
||||
LayerMasks.Masks[LayerMaskType.PlatformerGround]))
|
||||
_directionRight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4bb753139de944f69f426cbc1ec9ec92
|
||||
timeCreated: 1771430576
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using Code.Infrastructure.Configs;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer.Entity
|
||||
{
|
||||
public abstract class Entity : MonoBehaviour
|
||||
{
|
||||
private static readonly int VelocityY = Animator.StringToHash("VelocityY");
|
||||
private static readonly int VelocityX = Animator.StringToHash("VelocityX");
|
||||
private static readonly int OnGround = Animator.StringToHash("OnGround");
|
||||
|
||||
[SerializeField] protected Rigidbody2D rigidbody2D;
|
||||
[SerializeField] protected Animator animator;
|
||||
[SerializeField] protected SpriteRenderer spriteRenderer;
|
||||
[SerializeField] private Vector2 groundPoint;
|
||||
|
||||
protected bool onGround;
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
animator.SetFloat(VelocityX, rigidbody2D.linearVelocityX);
|
||||
animator.SetFloat(VelocityY, rigidbody2D.linearVelocityY);
|
||||
}
|
||||
|
||||
protected virtual void HandleFlipX() => spriteRenderer.flipX = rigidbody2D.linearVelocity.x < 0.01f;
|
||||
|
||||
protected virtual void FixedUpdate()
|
||||
{
|
||||
onGround = CheckGround();
|
||||
animator.SetBool(OnGround, onGround);
|
||||
Move();
|
||||
if(rigidbody2D.linearVelocity.x is > 0.01f or < -0.01f) HandleFlipX();
|
||||
}
|
||||
|
||||
protected virtual bool CheckGround()
|
||||
{
|
||||
UnityEngine.Debug.DrawLine(transform.position,(Vector2)transform.position + groundPoint, Color.green);
|
||||
return Physics2D.OverlapPoint((Vector2)transform.position + groundPoint, LayerMasks.Masks[LayerMaskType.PlatformerGround]);
|
||||
}
|
||||
|
||||
protected abstract void Move();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4b8cd0fdb9e4c258acd1f5898f193d1
|
||||
timeCreated: 1770905992
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using Code.Infrastructure.Generators;
|
||||
using UnityEngine.AddressableAssets;
|
||||
|
||||
namespace Code.Example.Platformer.Entity
|
||||
{
|
||||
[Config("Configs/Gameplay/Platformer/EnemySettings", true)]
|
||||
public class PlatformerEnemySettings
|
||||
{
|
||||
[ToConfig] public PlatformerEnemyData racerData;
|
||||
|
||||
[ToConfig] public AssetReference prefabReference;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct PlatformerEnemyData
|
||||
{
|
||||
public string name;
|
||||
public float speed;
|
||||
public int damage;
|
||||
public PlatformerEnemyType type;
|
||||
}
|
||||
|
||||
public enum PlatformerEnemyType
|
||||
{
|
||||
Dummy
|
||||
}
|
||||
public class PlatformerEnemyFactory
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49fd97d8a6464d3e86d6ea215949d1f5
|
||||
timeCreated: 1771423258
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using Code.Infrastructure.Input;
|
||||
using Reflex.Attributes;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace Code.Example.Platformer.Entity
|
||||
{
|
||||
public class PlayerEntity : Entity
|
||||
{
|
||||
private IInputService _inputService;
|
||||
|
||||
[Inject]
|
||||
private void Construct(IInputService inputService)
|
||||
{
|
||||
_inputService = inputService;
|
||||
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move,
|
||||
MovementPerformed);
|
||||
_inputService.BindActionCancelled((int)ActionMappings.Gameplay, (int)GameplayActions.Move,
|
||||
MovementCancelled);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, JumpPerformed);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move,
|
||||
MovementPerformed);
|
||||
_inputService.UnbindActionCancelled((int)ActionMappings.Gameplay, (int)GameplayActions.Move,
|
||||
MovementCancelled);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, JumpPerformed);
|
||||
}
|
||||
|
||||
protected override void Move()
|
||||
{
|
||||
rigidbody2D.linearVelocityX = _moveDirection.x * 5f;
|
||||
}
|
||||
|
||||
private Vector2 _moveDirection;
|
||||
|
||||
private void MovementPerformed(InputAction.CallbackContext callbackContext) =>
|
||||
_moveDirection = callbackContext.ReadValue<Vector2>();
|
||||
|
||||
private void MovementCancelled(InputAction.CallbackContext callbackContext) =>
|
||||
_moveDirection = Vector2.zero;
|
||||
|
||||
private void JumpPerformed(InputAction.CallbackContext callbackContext)
|
||||
{
|
||||
if (!onGround) return;
|
||||
rigidbody2D.AddForceY(100f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d5abb60d772491cbad21bb3a3689054
|
||||
timeCreated: 1770908525
|
||||
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer
|
||||
{
|
||||
public class PlatformerSceneData : MonoBehaviour
|
||||
{
|
||||
public Transform Player;
|
||||
public LevelData levelData;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct LevelData
|
||||
{
|
||||
// public List<Transform> enemyPoints;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fdfd83cbde8f4fbd97802ac19f2f32e5
|
||||
timeCreated: 1771423037
|
||||
@@ -0,0 +1,28 @@
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.SceneLoader;
|
||||
using Reflex.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer
|
||||
{
|
||||
public class PlatformerSceneInstaller : MonoBehaviour, IInstaller
|
||||
{
|
||||
// [SerializeField] private PlatformerCanvas gameplayPlatformerCanvas;
|
||||
|
||||
public void InstallBindings(ContainerBuilder containerBuilder)
|
||||
{
|
||||
// containerBuilder.RegisterValue(gameplayPlatformerCanvas);
|
||||
|
||||
containerBuilder.OnContainerBuilt += OnContainerBuild;
|
||||
}
|
||||
|
||||
private void OnContainerBuild(Container container)
|
||||
{
|
||||
// gameplayPlatformerCanvas.GameplayPlatformerCanvas.worldCamera =
|
||||
// container.Resolve<CameraStateMachineContext>().cameraLoadedData.Camera;
|
||||
//
|
||||
container.Resolve<EventBus>()
|
||||
.RaiseEvent<IOnPlatformerSceneLoadedHandler>(x => x.OnPlatformerSceneInitialized(container));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0aa23aea61d74467889be39a550c9922
|
||||
timeCreated: 1770736749
|
||||
@@ -0,0 +1,65 @@
|
||||
using Code.Infrastructure.App;
|
||||
using Code.Infrastructure.Camera;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Code.Infrastructure.Input;
|
||||
using Code.Infrastructure.Loadings;
|
||||
using Code.Infrastructure.SceneLoader;
|
||||
using Code.Infrastructure.UI;
|
||||
using Reflex.Core;
|
||||
using Unity.Cinemachine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Code.Example.Platformer
|
||||
{
|
||||
[State(typeof(AppStates),typeof(AppStateMachineContext))]
|
||||
[StateRaisingEvent]
|
||||
public partial class PlatformerState : IOnPlatformerSceneLoadedHandler, ICinematicMachineHandle
|
||||
{
|
||||
public PlatformerState(AppStateMachineContext context, EventBus eventBus) : base(context, eventBus)
|
||||
{
|
||||
}
|
||||
|
||||
public override AppStates myState => AppStates.Platformer;
|
||||
protected override void OnEnterState()
|
||||
{
|
||||
EventBus.Subscribe(this);
|
||||
|
||||
_loading = new(2, LoadingCompleted);
|
||||
|
||||
Context.UIService.OpenPanel(UIPanelType.Loading);
|
||||
Context.SceneLoader.LoadSceneAsync(SceneId.Platformer, LoadSceneMode.Single).Forget();
|
||||
Context.InputService.EnableMap(ActionMappings.Gameplay);
|
||||
Context.CameraFactory.LoadCinemachineCamera(CameraMode.Platformer).Forget();
|
||||
}
|
||||
private Loading _loading;
|
||||
|
||||
private void LoadingCompleted()
|
||||
{
|
||||
Context.UIService.ClosePanel(UIPanelType.Loading);
|
||||
}
|
||||
|
||||
|
||||
protected override void OnExitState()
|
||||
{
|
||||
Context.CameraFactory.ReleaseCinemachineCamera(CameraMode.Platformer);
|
||||
Context.InputService.DisableMap(ActionMappings.Gameplay);
|
||||
EventBus.Unsubscribe(this);
|
||||
}
|
||||
|
||||
public void OnPlatformerSceneLoaded()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnPlatformerSceneInitialized(Container container)
|
||||
{
|
||||
_loading.DoProgress();
|
||||
}
|
||||
|
||||
public void CinemachineLoaded(CinemachineCamera cinemachineCamera)
|
||||
{
|
||||
_loading.DoProgress();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 823e3299b52c48a8869581e7c27ef9a8
|
||||
timeCreated: 1770736342
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b5da862624174a36ac3ace9058b79513
|
||||
timeCreated: 1770737039
|
||||
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer.UI
|
||||
{
|
||||
public class PlatformerCanvas : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Canvas gameplayPlatformerCanvas;
|
||||
public Canvas GameplayPlatformerCanvas => gameplayPlatformerCanvas;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 964ac33334f64f8d917b153e9890e155
|
||||
timeCreated: 1770737062
|
||||
Reference in new issue
Block a user