Code refactor, added coins chests, key chest and door

This commit is contained in:
2023-06-22 13:58:20 +03:00
parent 30a51d96f7
commit b2cf8fde92
18 changed files with 359 additions and 63 deletions
-14
View File
@@ -1,14 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxesController : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
Destroy(gameObject);
}
}
}
+16
View File
@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chest : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collider)
{
var player=collider.GetComponent<Player>();
if (player != null)
{
player.AddCoin();
Destroy(gameObject);
}
}
}
+13
View File
@@ -0,0 +1,13 @@
using Assets.Scripts;
using UnityEngine;
public class Door : MonoBehaviour,IDoor
{
public void OpenDoor()
{
//PlayOpenAnimation
//Disable box collider
gameObject.GetComponent<BoxCollider2D>().enabled=false;
}
}
-15
View File
@@ -1,15 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorController : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
//if player have key, door will open
//else player can't pass
}
}
}
+25
View File
@@ -0,0 +1,25 @@
using Assets.Scripts;
using UnityEngine;
public class DoorInteract : MonoBehaviour
{
[SerializeField] private GameObject _doorGameObject;
private IDoor _door;
private void Awake()
{
_door =_doorGameObject.GetComponent<IDoor>();
}
private void OnTriggerEnter2D(Collider2D collider)
{
var player = collider.GetComponent<Player>();
if (player!=null)
{
if (player.IsHasKey())
{
_door.OpenDoor();
}
}
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c43502b5415adc044b631292348c83e8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
-1
View File
@@ -3,6 +3,5 @@
public interface IDoor
{
void OpenDoor();
void ToggleDoor();
}
}
+14
View File
@@ -0,0 +1,14 @@
using UnityEngine;
public class KeyChest : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collider)
{
var player = collider.GetComponent<Player>();
if (player != null)
{
player.SetKey();
Destroy(gameObject);
}
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cf81d521c74041643b3e8f0569c55a0f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
public class PlayerController : MonoBehaviour
public class Player : MonoBehaviour
{
[SerializeField]
private Animator animator;
@@ -17,17 +18,41 @@ public class PlayerController : MonoBehaviour
private BoxCollider2D _boxCollider;
private bool _isFall;
private bool _facingRight = true;
private bool _haveKey = false;
private int _totalCoins = 0;
private bool _hasKey = false;
void Start()
private void Start()
{
_body = GetComponent<Rigidbody2D>();
_boxCollider = GetComponent<BoxCollider2D>();
}
void Update()
public void AddCoin()
{
_totalCoins++;
print($"player have {_totalCoins} coins");
}
public void SetKey()
{
print($"player have key");
_hasKey =true;
}
public void RemoveKey()
{
_hasKey = false;
}
public bool IsHasKey()
{
return _hasKey;
}
private void Update()
{
float inputHorizontal = 0;
float inputVertical = 0;
var groundCheck = Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, Vector2.down, .1f, groundLayer);
@@ -35,7 +60,7 @@ public class PlayerController : MonoBehaviour
if (groundCheck || _isOnLadder)
{
_isFall = false;
inputHorizontal = Input.GetAxisRaw("Horizontal");
float inputHorizontal = Input.GetAxisRaw("Horizontal");
if (inputHorizontal > 0 && !_facingRight)
{
FlipCharacter();
@@ -77,6 +102,8 @@ public class PlayerController : MonoBehaviour
return Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, ladderLayer);
}
private void FixedUpdate()
{
if (_isOnLadder)
@@ -108,12 +135,5 @@ public class PlayerController : MonoBehaviour
_facingRight = !_facingRight;
}
private void OnTriggerEnter2D(Collider2D collision)
{
var door = collision.GetComponent<DoorController>();
if (door != null)
{
door.OpenDoor();
}
}
}