Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JessicaVakili committed Oct 17, 2018
0 parents commit 506a864
Show file tree
Hide file tree
Showing 33 changed files with 1,429 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
Binary file added 9781484237717.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter 3/.DS_Store
Binary file not shown.
81 changes: 81 additions & 0 deletions Chapter 3/MovementController.cs
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);
}
}
}
33 changes: 33 additions & 0 deletions Chapter 4/RoundCameraPos.cs
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;
}
}
7 changes: 7 additions & 0 deletions Chapter 5/Character.cs
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;
}
6 changes: 6 additions & 0 deletions Chapter 5/Consumable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using UnityEngine;

public class Consumable : MonoBehaviour {

public Item item;
}
21 changes: 21 additions & 0 deletions Chapter 5/Item.cs
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;
}
41 changes: 41 additions & 0 deletions Chapter 5/Player.cs
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);
}
}
17 changes: 17 additions & 0 deletions Chapter 6/Character.cs
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;
}
30 changes: 30 additions & 0 deletions Chapter 6/HealthBar.cs
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.

Copy link
@legallyfree

legallyfree May 10, 2019

if (character != null) maxHitPoints = character.maxHitPoints;
avoid a null reference

}

void Update()
{
if (character != null)
{
meterImage.fillAmount = hitPoints.value / maxHitPoints;
hpText.text = "HP:" + (meterImage.fillAmount * 100);
}
}
}
7 changes: 7 additions & 0 deletions Chapter 6/HitPoints.cs
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;
}
62 changes: 62 additions & 0 deletions Chapter 6/Inventory.cs
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;
}
}
60 changes: 60 additions & 0 deletions Chapter 6/Player.cs
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;
}
}
6 changes: 6 additions & 0 deletions Chapter 6/Slot.cs
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;
}
Loading

0 comments on commit 506a864

Please sign in to comment.