-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 506a864
Showing
33 changed files
with
1,429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class MovementController : MonoBehaviour | ||
{ | ||
public float movementSpeed = 3.0f; | ||
Vector2 movement = new Vector2(); | ||
|
||
// 1 | ||
Animator animator; | ||
|
||
// 2 | ||
string animationState = "AnimationState"; | ||
Rigidbody2D rb2D; | ||
|
||
// 3 | ||
enum CharStates | ||
{ | ||
walkEast = 1, | ||
walkSouth = 2, | ||
walkWest = 3, | ||
walkNorth = 4, | ||
|
||
idleEast = 5 | ||
} | ||
|
||
private void Start() | ||
{ | ||
// 4 | ||
animator = GetComponent<Animator>(); | ||
rb2D = GetComponent<Rigidbody2D>(); | ||
} | ||
|
||
private void Update() | ||
{ | ||
// 5 | ||
UpdateState(); | ||
} | ||
|
||
void FixedUpdate() | ||
{ | ||
// 6 | ||
MoveCharacter(); | ||
} | ||
|
||
private void MoveCharacter() | ||
{ | ||
movement.x = Input.GetAxisRaw("Horizontal"); | ||
movement.y = Input.GetAxisRaw("Vertical"); | ||
|
||
// 7 | ||
movement.Normalize(); | ||
rb2D.velocity = movement * movementSpeed; | ||
} | ||
|
||
private void UpdateState() | ||
{ | ||
// 8 | ||
if (movement.x > 0) | ||
{ | ||
animator.SetInteger(animationState, (int)CharStates.walkEast); | ||
} | ||
else if (movement.x < 0) | ||
{ | ||
animator.SetInteger(animationState, (int)CharStates.walkWest); | ||
} | ||
else if (movement.y > 0) | ||
{ | ||
animator.SetInteger(animationState, (int)CharStates.walkNorth); | ||
} | ||
else if (movement.y < 0) | ||
{ | ||
animator.SetInteger(animationState, (int)CharStates.walkSouth); | ||
} | ||
else | ||
{ | ||
animator.SetInteger(animationState, (int)CharStates.idleSouth); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using UnityEngine; | ||
using Cinemachine; | ||
|
||
public class RoundCameraPos : CinemachineExtension | ||
{ | ||
public float PixelsPerUnit = 32; | ||
|
||
|
||
// Called by Cinemachine after the Confiner is done with its processing pipeline | ||
|
||
protected override void PostPipelineStageCallback( | ||
CinemachineVirtualCameraBase vcam, | ||
CinemachineCore.Stage stage, ref CameraState state, float deltaTime) | ||
{ | ||
// Check to see what stage of post-processing we're in | ||
if (stage == CinemachineCore.Stage.Body) | ||
{ | ||
// Get the VC's final position | ||
Vector3 finalPos = state.FinalPosition; | ||
|
||
// Call the method we wrote to round the position | ||
Vector3 newPos = new Vector3(Round(finalPos.x), Round(finalPos.y), finalPos.z); | ||
// Set the VC's new position to the difference between the old | ||
// position and the new rounded position that we just calculated | ||
state.PositionCorrection += newPos - finalPos; | ||
} | ||
} | ||
|
||
float Round(float x) | ||
{ | ||
return Mathf.Round(x * PixelsPerUnit) / PixelsPerUnit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using UnityEngine; | ||
|
||
public abstract class Character : MonoBehaviour { | ||
|
||
public int hitPoints; | ||
public int maxHitPoints; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using UnityEngine; | ||
|
||
public class Consumable : MonoBehaviour { | ||
|
||
public Item item; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
[CreateAssetMenu(menuName = "Item")] | ||
public class Item : ScriptableObject { | ||
|
||
public string objectName; | ||
public Sprite sprite; | ||
|
||
public int quantity; | ||
|
||
public bool stackable; | ||
|
||
public enum ItemType | ||
{ | ||
COIN, | ||
HEALTH | ||
} | ||
|
||
public ItemType itemType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using UnityEngine; | ||
|
||
public class Player : Character | ||
{ | ||
public void Start() | ||
{ | ||
|
||
} | ||
|
||
void OnTriggerEnter2D(Collider2D collision) | ||
{ | ||
if (collision.gameObject.CompareTag("CanBePickedUp")) | ||
{ | ||
Item hitObject = collision.gameObject.GetComponent<Consumable>().item; | ||
|
||
if (hitObject != null) | ||
{ | ||
print("Hit: " + hitObject.objectName); | ||
|
||
switch (hitObject.itemType) | ||
{ | ||
case Item.ItemType.COIN: | ||
break; | ||
case Item.ItemType.HEALTH: | ||
AdjustHitPoints(hitObject.quantity); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
collision.gameObject.SetActive(false); | ||
} | ||
} | ||
} | ||
|
||
public void AdjustHitPoints(int amount) | ||
{ | ||
hitPoints = hitPoints + amount; | ||
print("Adjusted hitpoints by: " + amount + ". New value: " + hitPoints); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using UnityEngine; | ||
|
||
public abstract class Character : MonoBehaviour { | ||
|
||
public HitPoints hitPoints; | ||
|
||
public float startingHitPoints; | ||
public float maxHitPoints; | ||
|
||
public enum CharacterCategory | ||
{ | ||
PLAYER, | ||
ENEMY | ||
} | ||
|
||
public CharacterCategory characterCategory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
public class HealthBar : MonoBehaviour | ||
{ | ||
public HitPoints hitPoints; | ||
|
||
[HideInInspector] | ||
public Character character; | ||
|
||
public Image meterImage; | ||
|
||
public Text hpText; | ||
|
||
float maxHitPoints; | ||
|
||
void Start() | ||
{ | ||
maxHitPoints = character.maxHitPoints; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
void Update() | ||
{ | ||
if (character != null) | ||
{ | ||
meterImage.fillAmount = hitPoints.value / maxHitPoints; | ||
hpText.text = "HP:" + (meterImage.fillAmount * 100); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using UnityEngine; | ||
|
||
[CreateAssetMenu(menuName = "HitPoints")] | ||
public class HitPoints : ScriptableObject | ||
{ | ||
public float value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
public class Inventory : MonoBehaviour | ||
{ | ||
public GameObject slotPrefab; | ||
|
||
public const int numSlots = 5; | ||
|
||
Image[] itemImages = new Image[numSlots]; | ||
Item[] items = new Item[numSlots]; | ||
GameObject[] slots = new GameObject[numSlots]; | ||
|
||
public void Start() | ||
{ | ||
CreateSlots(); | ||
} | ||
|
||
public void CreateSlots() | ||
{ | ||
if (slotPrefab != null) | ||
{ | ||
for (int i = 0; i < numSlots; i++) | ||
{ | ||
GameObject newSlot = Instantiate(slotPrefab); | ||
newSlot.name = "ItemSlot_" + i; | ||
newSlot.transform.SetParent(gameObject.transform.GetChild(0).transform); | ||
slots[i] = newSlot; | ||
itemImages[i] = newSlot.transform.GetChild(1).GetComponent<Image>(); | ||
} | ||
} | ||
} | ||
|
||
public bool AddItem(Item itemToAdd) | ||
{ | ||
for (int i = 0; i < items.Length; i++) | ||
{ | ||
if (items[i] != null && items[i].itemType == itemToAdd.itemType && itemToAdd.stackable == true) | ||
{ | ||
// Adding to existing slot | ||
items[i].quantity = items[i].quantity + 1; | ||
Slot slotScript = slots[i].GetComponent<Slot>(); | ||
Text quantityText = slotScript.qtyText; | ||
quantityText.enabled = true; | ||
quantityText.text = items[i].quantity.ToString(); | ||
return true; | ||
} | ||
|
||
if (items[i] == null) | ||
{ | ||
// Adding to empty slot | ||
// Copy item and add to inventory. Copying so we dont modify original Scriptable Object | ||
items[i] = Instantiate(itemToAdd); | ||
items[i].quantity = 1; | ||
itemImages[i].sprite = itemToAdd.sprite; | ||
itemImages[i].enabled = true; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using UnityEngine; | ||
|
||
public class Player : Character | ||
{ | ||
public Inventory inventoryPrefab; | ||
Inventory inventory; | ||
|
||
public HealthBar healthBarPrefab; | ||
HealthBar healthBar; | ||
|
||
public void Start() | ||
{ | ||
hitPoints.value = startingHitPoints; | ||
inventory = Instantiate(inventoryPrefab); | ||
healthBar = Instantiate(healthBarPrefab); | ||
healthBar.character = this; | ||
} | ||
|
||
void OnTriggerEnter2D(Collider2D collision) | ||
{ | ||
if (collision.gameObject.CompareTag("CanBePickedUp")) | ||
{ | ||
Item hitObject = collision.gameObject.GetComponent<Consumable>().item; | ||
|
||
if (hitObject != null) | ||
{ | ||
bool shouldDisappear = false; | ||
|
||
switch (hitObject.itemType) | ||
{ | ||
case Item.ItemType.COIN: | ||
shouldDisappear = inventory.AddItem(hitObject); | ||
break; | ||
case Item.ItemType.HEALTH: | ||
shouldDisappear = AdjustHitPoints(hitObject.quantity); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
if (shouldDisappear) | ||
{ | ||
collision.gameObject.SetActive(false); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public bool AdjustHitPoints(int amount) | ||
{ | ||
if (hitPoints.value < maxHitPoints) | ||
{ | ||
hitPoints.value = hitPoints.value + amount; | ||
print("Adjusted HP by: " + amount + ". New value: " + hitPoints.value); | ||
return true; | ||
} | ||
print("didnt adjust hitpoints"); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
public class Slot : MonoBehaviour { | ||
public Text qtyText; | ||
} |
Oops, something went wrong.
if (character != null) maxHitPoints = character.maxHitPoints;
avoid a null reference