Added project
This commit is contained in:
53
Platformer/Assets/Scripts/Arrow.cs
Normal file
53
Platformer/Assets/Scripts/Arrow.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(
|
||||
typeof(Rigidbody2D),
|
||||
typeof(SpriteRenderer),
|
||||
typeof(TriggerDamage)
|
||||
)
|
||||
]
|
||||
public class Arrow : MonoBehaviour, IObjectDestroyer {
|
||||
|
||||
[SerializeField] private float force;
|
||||
[SerializeField] private float lifeTime;
|
||||
|
||||
private SpriteRenderer m_SpriteRenderer;
|
||||
private Rigidbody2D m_Rigidbody2D;
|
||||
private TriggerDamage m_TriggerDamage;
|
||||
private PlayerController player;
|
||||
|
||||
public float Force {
|
||||
get => force;
|
||||
set => force = value;
|
||||
}
|
||||
|
||||
private void Awake() {
|
||||
m_SpriteRenderer = GetComponent<SpriteRenderer>();
|
||||
m_Rigidbody2D = GetComponent<Rigidbody2D>();
|
||||
m_TriggerDamage = GetComponent<TriggerDamage>();
|
||||
}
|
||||
|
||||
public void Destroy(GameObject gameObj) {
|
||||
player.ReturnArrowToPool(this);
|
||||
}
|
||||
|
||||
public void SetImpulse(Vector2 direction, float force, int bonusDamage, PlayerController player) {
|
||||
this.player = player;
|
||||
m_TriggerDamage.Init(this);
|
||||
m_TriggerDamage.Parent = player.gameObject;
|
||||
m_TriggerDamage.Damage += bonusDamage;
|
||||
m_Rigidbody2D.AddForce(direction * force, ForceMode2D.Impulse);
|
||||
if (force < 0) {
|
||||
transform.rotation = Quaternion.Euler(0, 180, 0);
|
||||
}
|
||||
StartCoroutine(StartLife());
|
||||
}
|
||||
|
||||
private IEnumerator StartLife() {
|
||||
yield return new WaitForSeconds(lifeTime);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/Arrow.cs.meta
Normal file
11
Platformer/Assets/Scripts/Arrow.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b601e959c22107713a80e89318f32664
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
34
Platformer/Assets/Scripts/BuffEmitter.cs
Normal file
34
Platformer/Assets/Scripts/BuffEmitter.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BuffEmitter : MonoBehaviour {
|
||||
|
||||
[SerializeField] private Buff buff;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (GameManager.Instance.buffReceiverContainer.ContainsKey(other.gameObject)) {
|
||||
var receiver = GameManager.Instance.buffReceiverContainer[other.gameObject];
|
||||
receiver.AddBuff(buff);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other) {
|
||||
if (GameManager.Instance.buffReceiverContainer.ContainsKey(other.gameObject)) {
|
||||
var receiver = GameManager.Instance.buffReceiverContainer[other.gameObject];
|
||||
receiver.RemoveBuff(buff);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Buff {
|
||||
public BuffType type;
|
||||
public float additiveBonus;
|
||||
public float multipleBonus;
|
||||
}
|
||||
|
||||
public enum BuffType : byte {
|
||||
Damage, Force, Armor
|
||||
}
|
||||
11
Platformer/Assets/Scripts/BuffEmitter.cs.meta
Normal file
11
Platformer/Assets/Scripts/BuffEmitter.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70090cf2bf6b9c3ebac876a51d1af58f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
34
Platformer/Assets/Scripts/BuffReceiver.cs
Normal file
34
Platformer/Assets/Scripts/BuffReceiver.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BuffReceiver : MonoBehaviour {
|
||||
|
||||
private List<Buff> buffs;
|
||||
public Action OnBuffChanged;
|
||||
|
||||
private void Start() {
|
||||
GameManager.Instance.buffReceiverContainer.Add(gameObject, this);
|
||||
buffs = new List<Buff>();
|
||||
}
|
||||
|
||||
public List<Buff> Buffs => buffs;
|
||||
|
||||
public void AddBuff(Buff buff) {
|
||||
if (!buffs.Contains(buff)) {
|
||||
buffs.Add(buff);
|
||||
}
|
||||
if (OnBuffChanged != null) {
|
||||
OnBuffChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveBuff(Buff buff) {
|
||||
if (buffs.Contains(buff)) {
|
||||
buffs.Remove(buff);
|
||||
}
|
||||
if (OnBuffChanged != null) {
|
||||
OnBuffChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/BuffReceiver.cs.meta
Normal file
11
Platformer/Assets/Scripts/BuffReceiver.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7135b77ebd5c335dd988f23328cd771d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
39
Platformer/Assets/Scripts/Cell.cs
Normal file
39
Platformer/Assets/Scripts/Cell.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine;
|
||||
|
||||
public class Cell : MonoBehaviour {
|
||||
|
||||
public Action OnUpdateCell;
|
||||
|
||||
[SerializeField] private Image icon;
|
||||
private Item item;
|
||||
|
||||
private void Awake() {
|
||||
icon.sprite = null;
|
||||
}
|
||||
|
||||
public void Init(Item item) {
|
||||
this.item = item;
|
||||
if (item == null) {
|
||||
icon.sprite = null;
|
||||
}
|
||||
else {
|
||||
icon.sprite = item.Icon;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnClickCell() {
|
||||
if (item == null) {
|
||||
return;
|
||||
}
|
||||
GameManager.Instance.inventory.Items.Remove(item);
|
||||
var buff = new Buff {
|
||||
type = item.Type,
|
||||
additiveBonus = item.Value
|
||||
};
|
||||
GameManager.Instance.inventory.BuffReceiver.AddBuff(buff);
|
||||
OnUpdateCell?.Invoke();
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/Cell.cs.meta
Normal file
11
Platformer/Assets/Scripts/Cell.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67941f4cfd54e8c9585900408cdbfedc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Platformer/Assets/Scripts/Coin.cs
Normal file
21
Platformer/Assets/Scripts/Coin.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Coin : MonoBehaviour {
|
||||
|
||||
[SerializeField] private Animator m_Animator;
|
||||
private static readonly int StartDestroy1 = Animator.StringToHash("StartDestroy");
|
||||
|
||||
private void Start() {
|
||||
GameManager.Instance.coinContainer.Add(gameObject, this);
|
||||
}
|
||||
|
||||
public void StartDestroy() {
|
||||
m_Animator.SetTrigger(StartDestroy1);
|
||||
}
|
||||
|
||||
public void EndDestroy() {
|
||||
GameManager.Instance.inventory.CoinsCount++;
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/Coin.cs.meta
Normal file
11
Platformer/Assets/Scripts/Coin.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79cf8c06e1fa98406913dff5c9275384
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
51
Platformer/Assets/Scripts/CollisionDamage.cs
Normal file
51
Platformer/Assets/Scripts/CollisionDamage.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class CollisionDamage : MonoBehaviour {
|
||||
|
||||
[SerializeField] private int damage = 10;
|
||||
[SerializeField] private Animator animator;
|
||||
|
||||
private Health m_Health;
|
||||
private static readonly int Direction = Animator.StringToHash("Direction");
|
||||
|
||||
public float direction { get; private set; }
|
||||
|
||||
private void OnCollisionStay2D(Collision2D other) {
|
||||
if (GameManager.Instance.healthContainer.ContainsKey(other.gameObject)) {
|
||||
m_Health = GameManager.Instance.healthContainer[other.gameObject];
|
||||
direction = (other.transform.position - transform.position).x;
|
||||
animator.SetFloat(Direction, Mathf.Abs(direction));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCollisionExit2D(Collision2D other) {
|
||||
Reset();
|
||||
}
|
||||
|
||||
private void OnTriggerStay2D(Collider2D other) {
|
||||
if (GameManager.Instance.healthContainer.ContainsKey(other.gameObject)) {
|
||||
m_Health = GameManager.Instance.healthContainer[other.gameObject];
|
||||
direction = (other.transform.position - transform.position).x;
|
||||
animator.SetFloat(Direction, Mathf.Abs(direction));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other) {
|
||||
Reset();
|
||||
}
|
||||
|
||||
public void SetDamage() {
|
||||
if (m_Health != null) {
|
||||
m_Health.TakeHit(damage);
|
||||
}
|
||||
Reset();
|
||||
}
|
||||
|
||||
private void Reset() {
|
||||
direction = 0f;
|
||||
animator.SetFloat(Direction, 0f);
|
||||
m_Health = null;
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/CollisionDamage.cs.meta
Normal file
11
Platformer/Assets/Scripts/CollisionDamage.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c883816862bbe870bf86ab2bd39744a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
37
Platformer/Assets/Scripts/EnemyPatrol.cs
Normal file
37
Platformer/Assets/Scripts/EnemyPatrol.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
public class EnemyPatrol : MonoBehaviour {
|
||||
|
||||
[SerializeField] private GameObject leftBorder;
|
||||
[SerializeField] private GameObject rightBorder;
|
||||
[SerializeField] private float speed;
|
||||
[SerializeField] private Rigidbody2D _rigidbody;
|
||||
[SerializeField] private SpriteRenderer spriteRender;
|
||||
[SerializeField] private CollisionDamage collisionDamage;
|
||||
[SerializeField] private GroundDetection groundDetection;
|
||||
[SerializeField] private Animator animator;
|
||||
|
||||
private bool m_IsRightDirection;
|
||||
|
||||
private void Update() {
|
||||
if (groundDetection.isGrounded) {
|
||||
if (transform.position.x > rightBorder.transform.position.x || collisionDamage.direction < 0) {
|
||||
m_IsRightDirection = false;
|
||||
} else if (transform.position.x < leftBorder.transform.position.x || collisionDamage.direction > 0) {
|
||||
m_IsRightDirection = true;
|
||||
}
|
||||
_rigidbody.velocity = m_IsRightDirection ? Vector2.right : Vector2.left;
|
||||
_rigidbody.velocity *= speed;
|
||||
}
|
||||
if (_rigidbody.velocity.x > 0) {
|
||||
spriteRender.flipX = true;
|
||||
}
|
||||
if (_rigidbody.velocity.x < 0) {
|
||||
spriteRender.flipX = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/EnemyPatrol.cs.meta
Normal file
11
Platformer/Assets/Scripts/EnemyPatrol.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30f7f3d4a20f4525ba14e4c44f93a938
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
25
Platformer/Assets/Scripts/FollowCamera.cs
Normal file
25
Platformer/Assets/Scripts/FollowCamera.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class FollowCamera : MonoBehaviour {
|
||||
|
||||
[Header("Set in Inspector")]
|
||||
public GameObject POI;
|
||||
public float easing = 0.05f;
|
||||
public Vector2 minXY = Vector2.zero;
|
||||
|
||||
private float m_CamZ;
|
||||
|
||||
private void Awake() {
|
||||
m_CamZ = transform.position.z;
|
||||
}
|
||||
|
||||
private void FixedUpdate() {
|
||||
var destination = POI == null ? Vector3.zero : POI.transform.position;
|
||||
destination.x = Mathf.Max(minXY.x, destination.x);
|
||||
destination.y = Mathf.Max(minXY.y, destination.y);
|
||||
destination = Vector3.Lerp(transform.position, destination, easing);
|
||||
destination.z = m_CamZ;
|
||||
transform.position = destination;
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/FollowCamera.cs.meta
Normal file
11
Platformer/Assets/Scripts/FollowCamera.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8721c9ad5410fa6de8001e8ec319bb0a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Platformer/Assets/Scripts/For HW05.meta
Normal file
8
Platformer/Assets/Scripts/For HW05.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1405d56a8c21e1264b55b54a962bd563
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
14
Platformer/Assets/Scripts/For HW05/Bus.cs
Normal file
14
Platformer/Assets/Scripts/For HW05/Bus.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Bus: Vehicle {
|
||||
|
||||
public Bus() {
|
||||
vehicleName = "Bus";
|
||||
}
|
||||
|
||||
public override void Beep() {
|
||||
base.Beep();
|
||||
Debug.Log($"{vehicleName}: beeeep");
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/For HW05/Bus.cs.meta
Normal file
11
Platformer/Assets/Scripts/For HW05/Bus.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 328e8ea01a57b39b0a073a8d8c46cf77
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
13
Platformer/Assets/Scripts/For HW05/Car.cs
Normal file
13
Platformer/Assets/Scripts/For HW05/Car.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Car: Vehicle {
|
||||
|
||||
public Car() {
|
||||
vehicleName = "Car";
|
||||
}
|
||||
|
||||
public override void Beep() {
|
||||
base.Beep();
|
||||
Debug.Log($"{vehicleName}: beep");
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/For HW05/Car.cs.meta
Normal file
11
Platformer/Assets/Scripts/For HW05/Car.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf33bec4a92af3ed6ba28c2390dc9dc5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
29
Platformer/Assets/Scripts/For HW05/Cat.cs
Normal file
29
Platformer/Assets/Scripts/For HW05/Cat.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Cat {
|
||||
|
||||
public string name { get; }
|
||||
public int age { get; }
|
||||
public int weight { get; set; }
|
||||
public int height { get; }
|
||||
public int length { get; }
|
||||
|
||||
public Cat() {
|
||||
}
|
||||
|
||||
public Cat(string name, int age, int weight, int height, int length) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.weight = weight;
|
||||
this.height = height;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public void meov() {
|
||||
Debug.Log(this);
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return $"Cat: \"name\": \"{name}\", \"age\": {age}, \"weight\": {weight}, \"height\": {height}, \"length\": {length}'";
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/For HW05/Cat.cs.meta
Normal file
11
Platformer/Assets/Scripts/For HW05/Cat.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5f59cbe9eb9251d4a3d99b065afcb9a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
14
Platformer/Assets/Scripts/For HW05/Tractor.cs
Normal file
14
Platformer/Assets/Scripts/For HW05/Tractor.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Tractor: Vehicle {
|
||||
|
||||
public Tractor() {
|
||||
vehicleName = "Tractor";
|
||||
}
|
||||
|
||||
public override void Beep() {
|
||||
base.Beep();
|
||||
Debug.Log($"{vehicleName}: trrr");
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/For HW05/Tractor.cs.meta
Normal file
11
Platformer/Assets/Scripts/For HW05/Tractor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 557a1b08d5299f343aa7c9571b2d79cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
11
Platformer/Assets/Scripts/For HW05/Vehicle.cs
Normal file
11
Platformer/Assets/Scripts/For HW05/Vehicle.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
|
||||
abstract public class Vehicle {
|
||||
|
||||
public string vehicleName { get; set; }
|
||||
|
||||
public virtual void Beep() {
|
||||
Debug.Log("Vehicle");
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/For HW05/Vehicle.cs.meta
Normal file
11
Platformer/Assets/Scripts/For HW05/Vehicle.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a356526260da9e9cbea681e569cd364
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
35
Platformer/Assets/Scripts/GameManager.cs
Normal file
35
Platformer/Assets/Scripts/GameManager.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameManager : MonoBehaviour {
|
||||
|
||||
#region Singleton
|
||||
public static GameManager Instance { get; private set; }
|
||||
#endregion
|
||||
|
||||
public Dictionary<GameObject, Health> healthContainer;
|
||||
public Dictionary<GameObject, Coin> coinContainer;
|
||||
public Dictionary<GameObject, BuffReceiver> buffReceiverContainer;
|
||||
public Dictionary<GameObject, ItemComponent> itemsContainer;
|
||||
[HideInInspector] public PlayerInventory inventory;
|
||||
public ItemBase ItemBase;
|
||||
|
||||
private void Awake() {
|
||||
Instance = this;
|
||||
healthContainer = new Dictionary<GameObject, Health>();
|
||||
coinContainer = new Dictionary<GameObject, Coin>();
|
||||
buffReceiverContainer = new Dictionary<GameObject, BuffReceiver>();
|
||||
itemsContainer = new Dictionary<GameObject, ItemComponent>();
|
||||
}
|
||||
|
||||
|
||||
//Вариант создания пула объектов
|
||||
// private void Start() {
|
||||
// var healthObjects = FindObjectsOfType<Health>();
|
||||
// foreach (var health in healthObjects) {
|
||||
// healthContainer.Add(health.gameObject, health);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
11
Platformer/Assets/Scripts/GameManager.cs.meta
Normal file
11
Platformer/Assets/Scripts/GameManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a189f932c0bec1ac4b39af02ac0e00ee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
20
Platformer/Assets/Scripts/GroundDetection.cs
Normal file
20
Platformer/Assets/Scripts/GroundDetection.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class GroundDetection : MonoBehaviour {
|
||||
|
||||
public bool isGrounded { get; private set; }
|
||||
|
||||
private void OnCollisionStay2D(Collision2D other) {
|
||||
|
||||
if (other.gameObject.CompareTag("Platform") || other.gameObject.CompareTag("Enemy")) {
|
||||
isGrounded = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCollisionExit2D(Collision2D other) {
|
||||
if (other.gameObject.CompareTag("Platform") || other.gameObject.CompareTag("Enemy")) {
|
||||
isGrounded = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/GroundDetection.cs.meta
Normal file
11
Platformer/Assets/Scripts/GroundDetection.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ed50ed2b45e162ca86dc271f8ca8ce0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
31
Platformer/Assets/Scripts/Health.cs
Normal file
31
Platformer/Assets/Scripts/Health.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Health : MonoBehaviour {
|
||||
|
||||
[SerializeField] private int health;
|
||||
[SerializeField] private Animator animator;
|
||||
private static readonly int TakeDamage = Animator.StringToHash("TakeDamage");
|
||||
|
||||
public int currentHealth { get; private set; }
|
||||
|
||||
private void Start() {
|
||||
currentHealth = health;
|
||||
GameManager.Instance.healthContainer.Add(gameObject, this);
|
||||
}
|
||||
|
||||
public void TakeHit(int damage) {
|
||||
currentHealth -= damage;
|
||||
animator.SetTrigger(TakeDamage);
|
||||
if (currentHealth <= 0) {
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetHealth(int bonusHealth) {
|
||||
currentHealth += bonusHealth;
|
||||
// if (currentHealth > health) {
|
||||
// currentHealth = health;
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/Health.cs.meta
Normal file
11
Platformer/Assets/Scripts/Health.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 548fbc35c05cbfcbca3f17d96372816f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
24
Platformer/Assets/Scripts/HealthBonus.cs
Normal file
24
Platformer/Assets/Scripts/HealthBonus.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class HealthBonus : MonoBehaviour {
|
||||
|
||||
[SerializeField] private int bonusHealth = 10;
|
||||
[SerializeField] private Animator animator;
|
||||
|
||||
private Health health;
|
||||
private static readonly int Pickup = Animator.StringToHash("Pickup");
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (GameManager.Instance.healthContainer.ContainsKey(other.gameObject)) {
|
||||
health = GameManager.Instance.healthContainer[other.gameObject];
|
||||
animator.SetTrigger(Pickup);
|
||||
}
|
||||
}
|
||||
|
||||
public void PickupHealthBonus() {
|
||||
if (health != null) {
|
||||
health.SetHealth(bonusHealth);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/HealthBonus.cs.meta
Normal file
11
Platformer/Assets/Scripts/HealthBonus.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6ecca912132fbbe5b06a07a50253d5d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
5
Platformer/Assets/Scripts/IObjectDestroyer.cs
Normal file
5
Platformer/Assets/Scripts/IObjectDestroyer.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using UnityEngine;
|
||||
|
||||
public interface IObjectDestroyer {
|
||||
void Destroy(GameObject gameObj);
|
||||
}
|
||||
3
Platformer/Assets/Scripts/IObjectDestroyer.cs.meta
Normal file
3
Platformer/Assets/Scripts/IObjectDestroyer.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9fc31bdc833c438f90755a8c10afcefb
|
||||
timeCreated: 1586561589
|
||||
41
Platformer/Assets/Scripts/InventoryUiController.cs
Normal file
41
Platformer/Assets/Scripts/InventoryUiController.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class InventoryUiController : MonoBehaviour {
|
||||
|
||||
[SerializeField] private Cell[] cells;
|
||||
[SerializeField] private int cellCount;
|
||||
[SerializeField] private Cell cellPrefab;
|
||||
[SerializeField] private Transform rootParent;
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Init() {
|
||||
cells = new Cell[cellCount];
|
||||
for (var i = 0; i < cellCount; i++) {
|
||||
cells[i] = Instantiate(cellPrefab, rootParent);
|
||||
cells[i].OnUpdateCell += UpdateInventory;
|
||||
}
|
||||
cellPrefab.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnEnable() {
|
||||
if (cells == null || cells.Length <= 0) {
|
||||
Init();
|
||||
}
|
||||
UpdateInventory();
|
||||
}
|
||||
|
||||
public void UpdateInventory() {
|
||||
var inventory = GameManager.Instance.inventory;
|
||||
foreach (var cell in cells) {
|
||||
cell.Init(null);
|
||||
}
|
||||
for (var i = 0; i < inventory.Items.Count; i++) {
|
||||
if (i < cells.Length) {
|
||||
cells[i].Init(inventory.Items[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/InventoryUiController.cs.meta
Normal file
11
Platformer/Assets/Scripts/InventoryUiController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a61afe30bb0940858de180f478c3276
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
78
Platformer/Assets/Scripts/ItemBase.cs
Normal file
78
Platformer/Assets/Scripts/ItemBase.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "New Item Database", menuName = "Databases/Items")]
|
||||
public class ItemBase : ScriptableObject {
|
||||
|
||||
[SerializeField, HideInInspector] private List<Item> items;
|
||||
[SerializeField] private Item currentItem;
|
||||
private int currentIndex;
|
||||
|
||||
public void CreateItem() {
|
||||
if (items == null) {
|
||||
items = new List<Item>();
|
||||
}
|
||||
var item = new Item();
|
||||
items.Add(item);
|
||||
currentItem = item;
|
||||
currentIndex = items.Count - 1;
|
||||
}
|
||||
|
||||
public void RemoveItem() {
|
||||
if (items == null || currentItem == null) {
|
||||
return;
|
||||
}
|
||||
items.Remove(currentItem);
|
||||
if (items.Count > 0) {
|
||||
currentItem = items[0];
|
||||
} else {
|
||||
CreateItem();
|
||||
}
|
||||
currentIndex = 0;
|
||||
}
|
||||
|
||||
public void NextItem() {
|
||||
if (currentIndex + 1 < items.Count) {
|
||||
currentIndex++;
|
||||
currentItem = items[currentIndex];
|
||||
}
|
||||
}
|
||||
|
||||
public void PrevItem() {
|
||||
if (currentIndex > 0) {
|
||||
currentIndex--;
|
||||
currentItem = items[currentIndex];
|
||||
}
|
||||
}
|
||||
|
||||
public Item GetItemOfId(int id) {
|
||||
return items.Find(t => t.Id == id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Item {
|
||||
|
||||
[SerializeField] private int id;
|
||||
public int Id => id;
|
||||
|
||||
[SerializeField] private string itemName;
|
||||
public string ItemName => itemName;
|
||||
|
||||
[SerializeField] private string description;
|
||||
public string Description => description;
|
||||
|
||||
[SerializeField] private Sprite icon;
|
||||
public Sprite Icon => icon;
|
||||
|
||||
[SerializeField] private BuffType type;
|
||||
public BuffType Type => type;
|
||||
|
||||
[SerializeField] private float value;
|
||||
public float Value => value;
|
||||
|
||||
public override string ToString() {
|
||||
return $"[id: {id}, itemName: {itemName}, description: {description}, type: {type.ToString()}, value {value}";
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/ItemBase.cs.meta
Normal file
11
Platformer/Assets/Scripts/ItemBase.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0acf5bfae8e4104a4939acbc25ac58ab
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
30
Platformer/Assets/Scripts/ItemComponent.cs
Normal file
30
Platformer/Assets/Scripts/ItemComponent.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class ItemComponent : MonoBehaviour, IObjectDestroyer {
|
||||
|
||||
[SerializeField] private ItemType type;
|
||||
[SerializeField] private SpriteRenderer spriteRenderer;
|
||||
[SerializeField] private Animator animator;
|
||||
|
||||
private Item _item;
|
||||
|
||||
public Item Item => _item;
|
||||
|
||||
private void Start() {
|
||||
_item = GameManager.Instance.ItemBase.GetItemOfId((int) type);
|
||||
spriteRenderer.sprite = _item.Icon;
|
||||
GameManager.Instance.itemsContainer.Add(gameObject, this);
|
||||
}
|
||||
|
||||
public void Destroy(GameObject gameObj) {
|
||||
animator.SetTrigger("StartDestroy");
|
||||
}
|
||||
|
||||
public void EndDestroy() {
|
||||
MonoBehaviour.Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public enum ItemType {
|
||||
ForcePotion = 0, DamagePotion = 1, ArmorPotion = 2
|
||||
}
|
||||
11
Platformer/Assets/Scripts/ItemComponent.cs.meta
Normal file
11
Platformer/Assets/Scripts/ItemComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: afbe89f45a203262a9dd4aa39eb39a84
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
39
Platformer/Assets/Scripts/MushroomJumper.cs
Normal file
39
Platformer/Assets/Scripts/MushroomJumper.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class MushroomJumper : MonoBehaviour {
|
||||
|
||||
[SerializeField] private Animator _animator;
|
||||
[SerializeField] private float forceMultiply = 2.0f;
|
||||
|
||||
private static readonly int Enter = Animator.StringToHash("Enter");
|
||||
private PlayerController playerController;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (other.gameObject.CompareTag("Player")) {
|
||||
_animator.SetTrigger(Enter);
|
||||
playerController = other.gameObject.GetComponent<PlayerController>();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerStay2D(Collider2D other) {
|
||||
if (other.gameObject.CompareTag("Player")) {
|
||||
if (playerController == null) {
|
||||
playerController = other.gameObject.GetComponent<PlayerController>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other) {
|
||||
if (other.gameObject.CompareTag("Player")) {
|
||||
playerController = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddForceJumpPlayer() {
|
||||
if (playerController != null) {
|
||||
playerController.Force *= forceMultiply;
|
||||
playerController.Jump();
|
||||
playerController.Force /= forceMultiply;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/MushroomJumper.cs.meta
Normal file
11
Platformer/Assets/Scripts/MushroomJumper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 77d619389556ba4468c7b52633565c85
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
215
Platformer/Assets/Scripts/PlayerController.cs
Normal file
215
Platformer/Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class PlayerController : MonoBehaviour {
|
||||
|
||||
[Header("Editable player setting")]
|
||||
[SerializeField] private float speed = 2.5f;
|
||||
[SerializeField] private float force = 4.0f;
|
||||
[SerializeField] private float shootForce = 5;
|
||||
[SerializeField] private float reloadTime = 5;
|
||||
[SerializeField] private int minimalHeight = -10;
|
||||
[SerializeField] private Vector3 direction;
|
||||
[SerializeField] private Arrow arrow;
|
||||
[SerializeField] private Transform arrowSpawnPoint;
|
||||
[SerializeField] private int arrowsCount = 3;
|
||||
[SerializeField] private Rigidbody2D m_Rigidbody2D;
|
||||
[SerializeField] private SpriteRenderer m_SpriteRenderer;
|
||||
[SerializeField] private GroundDetection m_GroundDetection;
|
||||
[SerializeField] private Animator m_Animator;
|
||||
[SerializeField] private BuffReceiver buffReceiver;
|
||||
[SerializeField] private Health _health;
|
||||
|
||||
public bool IsEnd { get; private set; }
|
||||
public float ReloadTimer { get; private set; }
|
||||
public float Force {
|
||||
get => force;
|
||||
set => force = value;
|
||||
}
|
||||
|
||||
private List<Arrow> arrowPool;
|
||||
private bool isJumping;
|
||||
private bool isReloaded;
|
||||
private Arrow arrowPrefab;
|
||||
|
||||
private float bonusForce;
|
||||
private float bonusDamage;
|
||||
private float bonusHealth;
|
||||
|
||||
private UICharacterController controller;
|
||||
|
||||
private static readonly int Speed = Animator.StringToHash("Speed");
|
||||
private static readonly int IsGrounded = Animator.StringToHash("isGrounded");
|
||||
private static readonly int StartJump = Animator.StringToHash("StartJump");
|
||||
private static readonly int StartFall = Animator.StringToHash("StartFall");
|
||||
private static readonly int Shoot = Animator.StringToHash("Shoot");
|
||||
|
||||
#region Singletone
|
||||
public static PlayerController Instance { get; set; }
|
||||
#endregion
|
||||
|
||||
public void InitUIController(UICharacterController uiController) {
|
||||
controller = uiController;
|
||||
controller.Jump.onClick.AddListener(CheckJump);
|
||||
controller.Fire.onClick.AddListener(CheckShoot);
|
||||
}
|
||||
|
||||
private void Awake() {
|
||||
Instance = this;
|
||||
IsEnd = false;
|
||||
ReloadTimer = 0;
|
||||
}
|
||||
|
||||
private void Start() {
|
||||
isReloaded = false;
|
||||
arrowPool = new List<Arrow>();
|
||||
for (var i = 0; i < arrowsCount; i++) {
|
||||
var arrowTemp = Instantiate(arrow, arrowSpawnPoint);
|
||||
arrowPool.Add(arrowTemp);
|
||||
arrowTemp.gameObject.SetActive(false);
|
||||
}
|
||||
buffReceiver.OnBuffChanged += ApplyBafs;
|
||||
}
|
||||
|
||||
private void ApplyBafs() {
|
||||
var forceBuff = buffReceiver.Buffs.Find(t => t.type == BuffType.Force);
|
||||
var damageBuff = buffReceiver.Buffs.Find(t => t.type == BuffType.Damage);
|
||||
var armorBuff = buffReceiver.Buffs.Find(t => t.type == BuffType.Armor);
|
||||
bonusForce = forceBuff?.additiveBonus ?? 0;
|
||||
bonusHealth = armorBuff?.additiveBonus ?? 0;
|
||||
_health.SetHealth((int) bonusHealth);
|
||||
bonusDamage = damageBuff?.additiveBonus ?? 0;
|
||||
}
|
||||
|
||||
private void Update() {
|
||||
if (isReloaded) {
|
||||
ReloadTimer += Time.deltaTime;
|
||||
return;
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
if (Input.GetKeyDown(KeyCode.Space)) {
|
||||
CheckJump();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void FixedUpdate() {
|
||||
if (!IsEnd) {
|
||||
Move();
|
||||
if (transform.position.y < minimalHeight) {
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
} else {
|
||||
if (Input.GetKey(KeyCode.R)) {
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// private void OnTriggerEnter2D(Collider2D other) {
|
||||
// if (other.gameObject.CompareTag("Finish")) {
|
||||
// if (!isEnd) {
|
||||
// isEnd = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
private void CheckShoot() {
|
||||
m_Animator.SetTrigger(Shoot);
|
||||
}
|
||||
|
||||
private void Move() {
|
||||
m_Animator.SetBool(IsGrounded, m_GroundDetection.isGrounded);
|
||||
if (!isJumping && !m_GroundDetection.isGrounded) {
|
||||
m_Animator.SetTrigger(StartFall);
|
||||
}
|
||||
isJumping = isJumping && !m_GroundDetection.isGrounded;
|
||||
direction = Vector3.zero;
|
||||
#if UNITY_EDITOR
|
||||
if (Input.GetKey(KeyCode.A)) {
|
||||
direction = Vector3.left;
|
||||
m_SpriteRenderer.flipX = true;
|
||||
}
|
||||
if (Input.GetKey(KeyCode.D)) {
|
||||
direction = Vector3.right;
|
||||
m_SpriteRenderer.flipX = false;
|
||||
}
|
||||
#endif
|
||||
if (controller.Left.IsPressed) {
|
||||
direction = Vector3.left;
|
||||
m_SpriteRenderer.flipX = true;
|
||||
}
|
||||
if (controller.Right.IsPressed) {
|
||||
direction = Vector3.right;
|
||||
m_SpriteRenderer.flipX = false;
|
||||
}
|
||||
direction *= speed;
|
||||
direction.y = m_Rigidbody2D.velocity.y;
|
||||
m_Animator.SetFloat(Speed, Mathf.Abs(direction.x));
|
||||
m_Rigidbody2D.velocity = direction;
|
||||
}
|
||||
|
||||
private Arrow GetArrowFromPool() {
|
||||
if (arrowPool.Count > 0) {
|
||||
var arrowTemp = arrowPool[0];
|
||||
arrowPool.Remove(arrowTemp);
|
||||
arrowTemp.gameObject.SetActive(true);
|
||||
arrowTemp.transform.parent = null;
|
||||
arrowTemp.transform.position = arrowSpawnPoint.transform.position;
|
||||
return arrowTemp;
|
||||
}
|
||||
return Instantiate(arrow, arrowSpawnPoint.position, Quaternion.identity);
|
||||
}
|
||||
|
||||
public void ReturnArrowToPool(Arrow arrowTemp) {
|
||||
if (!arrowPool.Contains(arrowTemp)) {
|
||||
arrowPool.Add(arrowTemp);
|
||||
}
|
||||
arrowTemp.transform.parent = arrowSpawnPoint;
|
||||
arrowTemp.transform.position = arrowSpawnPoint.transform.position;
|
||||
arrowTemp.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionY;
|
||||
arrowTemp.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void ShowArrowBeforeShoot() {
|
||||
var tmpPos = arrowSpawnPoint.position;
|
||||
var position = tmpPos;
|
||||
position = new Vector3(m_SpriteRenderer.flipX ? position.x - 0.75f: position.x, position.y, position.z);
|
||||
arrowSpawnPoint.position = position;
|
||||
arrowPrefab = GetArrowFromPool();
|
||||
arrowPrefab.SetImpulse(Vector2.right, 0, 0, this);
|
||||
arrowPrefab.transform.rotation = Quaternion.Euler(0, m_SpriteRenderer.flipX ? 180 : 0, 0);
|
||||
arrowSpawnPoint.position = tmpPos;
|
||||
}
|
||||
|
||||
public void ShootArrow() {
|
||||
arrowPrefab.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
|
||||
arrowPrefab.SetImpulse(Vector2.right, m_SpriteRenderer.flipX ? -force * shootForce : force * shootForce, (int) bonusDamage, this);
|
||||
isReloaded = true;
|
||||
StartCoroutine(nameof(Reload));
|
||||
}
|
||||
|
||||
private IEnumerator Reload() {
|
||||
isReloaded = true;
|
||||
yield return new WaitForSeconds(reloadTime);
|
||||
isReloaded = false;
|
||||
ReloadTimer = 0;
|
||||
}
|
||||
|
||||
public void CheckJump() {
|
||||
if (m_GroundDetection.isGrounded) {
|
||||
Jump();
|
||||
}
|
||||
}
|
||||
|
||||
public void Jump() {
|
||||
m_Rigidbody2D.velocity = Vector2.zero;
|
||||
m_Rigidbody2D.AddForce(Vector2.up * (force + bonusForce), ForceMode2D.Impulse);
|
||||
m_Animator.SetTrigger(StartJump);
|
||||
isJumping = true;
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/PlayerController.cs.meta
Normal file
11
Platformer/Assets/Scripts/PlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f5c33d83027d28880b87c7e8541f76de
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
28
Platformer/Assets/Scripts/PlayerInventory.cs
Normal file
28
Platformer/Assets/Scripts/PlayerInventory.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerInventory : MonoBehaviour {
|
||||
|
||||
public int CoinsCount { get; set; }
|
||||
public BuffReceiver BuffReceiver;
|
||||
|
||||
public List<Item> Items { get; private set; }
|
||||
|
||||
private void Start() {
|
||||
GameManager.Instance.inventory = this;
|
||||
Items = new List<Item>();
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (GameManager.Instance.coinContainer.ContainsKey(other.gameObject)) {
|
||||
var coin = GameManager.Instance.coinContainer[other.gameObject];
|
||||
coin.StartDestroy();
|
||||
}
|
||||
if (GameManager.Instance.itemsContainer.ContainsKey(other.gameObject)) {
|
||||
var itemComponent = GameManager.Instance.itemsContainer[other.gameObject];
|
||||
Items.Add(itemComponent.Item);
|
||||
itemComponent.Destroy(other.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/PlayerInventory.cs.meta
Normal file
11
Platformer/Assets/Scripts/PlayerInventory.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2b93532d8a067cc289c38e73c270ef0d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
18
Platformer/Assets/Scripts/PressButton.cs
Normal file
18
Platformer/Assets/Scripts/PressButton.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PressButton : MonoBehaviour {
|
||||
|
||||
private bool isPressed;
|
||||
|
||||
public bool IsPressed => isPressed;
|
||||
|
||||
public void OnPointerDown() {
|
||||
isPressed = true;
|
||||
}
|
||||
|
||||
public void OnPointerUp() {
|
||||
isPressed = false;
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/PressButton.cs.meta
Normal file
11
Platformer/Assets/Scripts/PressButton.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8473cd4a87345e7c59d221ad1064554a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
43
Platformer/Assets/Scripts/TriggerDamage.cs
Normal file
43
Platformer/Assets/Scripts/TriggerDamage.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TriggerDamage : MonoBehaviour {
|
||||
|
||||
[SerializeField] private int damage;
|
||||
[SerializeField] private bool isDestroyingAfterCollision;
|
||||
private IObjectDestroyer destroyer;
|
||||
private GameObject parent;
|
||||
|
||||
public int Damage {
|
||||
get => damage;
|
||||
set => damage = value;
|
||||
}
|
||||
|
||||
public GameObject Parent {
|
||||
get => parent;
|
||||
set => parent = value;
|
||||
}
|
||||
|
||||
public void Init(IObjectDestroyer destroyer) {
|
||||
this.destroyer = destroyer;
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (other.gameObject == parent) {
|
||||
return;
|
||||
}
|
||||
if (GameManager.Instance.healthContainer.ContainsKey(other.gameObject)) {
|
||||
var health = GameManager.Instance.healthContainer[other.gameObject];
|
||||
health.TakeHit(damage);
|
||||
}
|
||||
if (isDestroyingAfterCollision) {
|
||||
if (destroyer == null) {
|
||||
Destroy(gameObject);
|
||||
} else {
|
||||
destroyer.Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Platformer/Assets/Scripts/TriggerDamage.cs.meta
Normal file
11
Platformer/Assets/Scripts/TriggerDamage.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a078d069eb4d3205bf9df02eae16a6c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
20
Platformer/Assets/Scripts/UICharacterController.cs
Normal file
20
Platformer/Assets/Scripts/UICharacterController.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine;
|
||||
|
||||
public class UICharacterController : MonoBehaviour {
|
||||
|
||||
[SerializeField] private PressButton left;
|
||||
[SerializeField] private PressButton right;
|
||||
[SerializeField] private Button fire;
|
||||
[SerializeField] private Button jump;
|
||||
|
||||
public PressButton Left => left;
|
||||
public PressButton Right => right;
|
||||
public Button Fire => fire;
|
||||
public Button Jump => jump;
|
||||
|
||||
void Start() {
|
||||
PlayerController.Instance.InitUIController(this);
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/UICharacterController.cs.meta
Normal file
11
Platformer/Assets/Scripts/UICharacterController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 707209a17cabd2677b8df1a5e8d890f6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
154
Platformer/Assets/Scripts/UIControl.cs
Normal file
154
Platformer/Assets/Scripts/UIControl.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class UIControl : MonoBehaviour {
|
||||
|
||||
private static readonly Color BUTTON_NORMAL_COLOR = new Color(0.1450828f, 0.1094224f, 0.2154269f, 1f);
|
||||
private static readonly Color BUTTON_HIGHLIGHED_COLOR = new Color(0.6156863f, 0.2392157f, 0.8392157f, 1f);
|
||||
|
||||
[SerializeField] private GameObject player;
|
||||
|
||||
[Header("UI Controls")]
|
||||
[SerializeField] private Text coinsCounterView;
|
||||
[SerializeField] private Text healthCounterView;
|
||||
[SerializeField] private Text endTextView;
|
||||
[SerializeField] private Text reloadTime;
|
||||
[SerializeField] private GameObject buttonSoundSetting;
|
||||
[SerializeField] private GameObject panelMenu;
|
||||
[SerializeField] private GameObject inventoryPanel;
|
||||
[Header("UI Controls (Health settings)")]
|
||||
[SerializeField] private Image healthBar;
|
||||
[SerializeField] private float delta;
|
||||
|
||||
private Health _playerHealth;
|
||||
private PlayerInventory _playerInventory;
|
||||
private PlayerController _playerController;
|
||||
private Button _buttonSoundSetting;
|
||||
private Text _labelSoundSetting;
|
||||
private float _healthValue;
|
||||
private float _currentHealth;
|
||||
|
||||
private void Awake() {
|
||||
_playerHealth = player.gameObject.GetComponent<Health>();
|
||||
_playerInventory = player.gameObject.GetComponent<PlayerInventory>();
|
||||
_playerController = player.gameObject.GetComponent<PlayerController>();
|
||||
_buttonSoundSetting = buttonSoundSetting.GetComponent<Button>();
|
||||
_labelSoundSetting = buttonSoundSetting.GetComponentInChildren<Text>();
|
||||
}
|
||||
|
||||
private void Start() {
|
||||
_healthValue = _playerHealth.currentHealth / 100.0f;
|
||||
GetPlayerData();
|
||||
if (!PlayerPrefs.HasKey("Sound")) {
|
||||
PlayerPrefs.SetInt("Sound", 1);
|
||||
}
|
||||
if (PlayerPrefs.GetInt("Sound") == 0) {
|
||||
_labelSoundSetting.text = "Звук Выкл.";
|
||||
var colorBlock = _buttonSoundSetting.colors;
|
||||
colorBlock.normalColor = Color.red;
|
||||
colorBlock.highlightedColor = Color.red;
|
||||
colorBlock.pressedColor = Color.red;
|
||||
colorBlock.selectedColor = Color.red;
|
||||
_buttonSoundSetting.colors = colorBlock;
|
||||
} else {
|
||||
_labelSoundSetting.text = "Звук Вкл.";
|
||||
var colorBlock = _buttonSoundSetting.colors;
|
||||
colorBlock.normalColor = BUTTON_NORMAL_COLOR;
|
||||
colorBlock.highlightedColor = BUTTON_HIGHLIGHED_COLOR;
|
||||
colorBlock.pressedColor = BUTTON_NORMAL_COLOR;
|
||||
colorBlock.selectedColor = BUTTON_HIGHLIGHED_COLOR;
|
||||
_buttonSoundSetting.colors = colorBlock;
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update() {
|
||||
GetPlayerData();
|
||||
}
|
||||
|
||||
private void FixedUpdate() {
|
||||
if (Input.GetKey(KeyCode.I)) {
|
||||
OnClickInventory();
|
||||
}
|
||||
}
|
||||
|
||||
private void GetPlayerData() {
|
||||
var time = $"{(_playerController != null ? _playerController.ReloadTimer : -1):F2}";
|
||||
coinsCounterView.text = _playerInventory != null ? _playerInventory.CoinsCount.ToString() : coinsCounterView.text;
|
||||
healthCounterView.text = _playerHealth != null ? _playerHealth.currentHealth + "%" : "0%";
|
||||
reloadTime.text = "Reload time: " + time;
|
||||
if (_playerController != null && _playerController.IsEnd) {
|
||||
endTextView.enabled = true;
|
||||
}
|
||||
HealthBarAnimation();
|
||||
}
|
||||
|
||||
private void HealthBarAnimation() {
|
||||
_currentHealth = _playerHealth.currentHealth / 100.0f;
|
||||
if (_currentHealth > _healthValue) {
|
||||
_healthValue += delta;
|
||||
}
|
||||
if (_currentHealth < _healthValue) {
|
||||
_healthValue -= delta;
|
||||
}
|
||||
if (_currentHealth < delta) {
|
||||
_healthValue = _currentHealth;
|
||||
}
|
||||
healthBar.fillAmount = _healthValue;
|
||||
}
|
||||
|
||||
public void OnClickInventory() {
|
||||
if (Time.timeScale > 0) {
|
||||
Time.timeScale = 0;
|
||||
inventoryPanel.SetActive(true);
|
||||
} else {
|
||||
inventoryPanel.SetActive(false);
|
||||
Time.timeScale = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnClickPause() {
|
||||
if (Time.timeScale > 0) {
|
||||
Time.timeScale = 0;
|
||||
panelMenu.SetActive(true);
|
||||
} else {
|
||||
panelMenu.SetActive(false);
|
||||
Time.timeScale = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnClickExitMenu() {
|
||||
panelMenu.SetActive(false);
|
||||
Time.timeScale = 1;
|
||||
SceneManager.LoadScene(0);
|
||||
}
|
||||
|
||||
public void OnClickExit() {
|
||||
Application.Quit();
|
||||
}
|
||||
|
||||
public void OnClickSoundSettings() {
|
||||
if (PlayerPrefs.GetInt("Sound") == 1) {
|
||||
_labelSoundSetting.text = "Звук Выкл.";
|
||||
var colorBlock = _buttonSoundSetting.colors;
|
||||
colorBlock.normalColor = Color.red;
|
||||
colorBlock.highlightedColor = Color.red;
|
||||
colorBlock.pressedColor = Color.red;
|
||||
colorBlock.selectedColor = Color.red;
|
||||
_buttonSoundSetting.colors = colorBlock;
|
||||
PlayerPrefs.SetInt("Sound", 0);
|
||||
} else {
|
||||
_labelSoundSetting.text = "Звук Вкл.";
|
||||
var colorBlock = _buttonSoundSetting.colors;
|
||||
colorBlock.normalColor = BUTTON_NORMAL_COLOR;
|
||||
colorBlock.highlightedColor = BUTTON_HIGHLIGHED_COLOR;
|
||||
colorBlock.pressedColor = BUTTON_NORMAL_COLOR;
|
||||
colorBlock.selectedColor = BUTTON_HIGHLIGHED_COLOR;
|
||||
_buttonSoundSetting.colors = colorBlock;
|
||||
PlayerPrefs.SetInt("Sound", 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/UIControl.cs.meta
Normal file
11
Platformer/Assets/Scripts/UIControl.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13f40c4243738304dbfcc048282845dc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
44
Platformer/Assets/Scripts/UIMenu.cs
Normal file
44
Platformer/Assets/Scripts/UIMenu.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine;
|
||||
|
||||
public class UIMenu : MonoBehaviour {
|
||||
|
||||
[SerializeField] private InputField inputField;
|
||||
[SerializeField] private GameObject levelChoicer;
|
||||
[SerializeField] private GameObject mainMenu;
|
||||
|
||||
private void Start() {
|
||||
if (PlayerPrefs.HasKey("Player_Name")) {
|
||||
inputField.text = PlayerPrefs.GetString("Player_Name");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEndEditName() {
|
||||
PlayerPrefs.SetString("Player_Name", inputField.text);
|
||||
}
|
||||
|
||||
public void OnClickPlay() {
|
||||
SceneManager.LoadScene(1);
|
||||
}
|
||||
|
||||
public void OnClickExit() {
|
||||
Application.Quit();
|
||||
}
|
||||
|
||||
public void OnClickLevelSelect() {
|
||||
mainMenu.SetActive(false);
|
||||
levelChoicer.SetActive(true);
|
||||
}
|
||||
|
||||
public void OnClickBackToMenu() {
|
||||
levelChoicer.SetActive(false);
|
||||
mainMenu.SetActive(true);
|
||||
}
|
||||
|
||||
public void OnClickLevel(int levelId) {
|
||||
SceneManager.LoadScene(levelId);
|
||||
}
|
||||
|
||||
}
|
||||
11
Platformer/Assets/Scripts/UIMenu.cs.meta
Normal file
11
Platformer/Assets/Scripts/UIMenu.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 21688eee91f107875a597b1f1e595fdc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user