---
# System prepended metadata

title: Lab 2 Documentation

---

# Lab 2 Documentation


## Task

- Create a new type of pickup with a new score rule. Give good variable names or document it properly so that the code is readable. 
- It has to subscribe to the onHit event and must inherit Consumable.cs
- Edit Consumable.cs enum type to have one more type of pickup  
- The ItemManager must update score accordingly with this pickup type 

## Documentation


New object would have the sprite : 
- Created a new object which is denoted as type 4. Object will have the sprite of a dog.
```csharp=
...
    public enum items
    {
        // Creates the different types of pickup items. Useful for creating different types of items in the game world.
        GREENJEWEL = 0,
        YELLOWJEWEL = 1,
        GARBAGEJEWEL = 2,
        COIN = 3,
        /*
         * new object would have the sprite : Sprite will be a dog
         * - Created a new object which is denoted as type 4. Object will have the sprite of a dog.
         */
        DOG = 4

    }
...
```

The new pickup is called dog because the sprite is a dog. The following is the newly created Object

```csharp=
/*
 * Created a new object called Dog which is denoted by a dog sprite in the game.
 * - Currently It subscribes to an onhit event as seen in line 19
 * - inherits Consumable.
 * - This is similar to the coin.
 */

public class Dog : Consumable
{
    // Coin Properties
    public int amount = 1;
    protected GameObject dog;

    public Dog(items type, int cost, float happiness = 0 ) : base(type, 0, happiness)
    {
        ItemManager.onHit += hit;
    }

    public void create(GameObject dog)
    {
        dog.GetComponent<BasicObjectScript>().type = type;
        dog.GetComponent<BasicObjectScript>().index = 0;
        this.dog = dog;
    }
    public void hit(items type, int index)
    {
        if (type == this.type)
        {
            dog.SetActive(false);
            amount-- ;
        }
    }
}

```

I have also updated the scoring rule for the dog. This can be seen in the following.

```csharp=
...
        /*
         * New Scoring system for the new object.
         * - With the DOG object now players would need to get the coin first before they take the dog.
         *   Concept-wise imagine buying a dog with the collected jewels which is traded for the coin.
         * - Player gets rewards with the preset dog health if they got the coin first
         * - else player gets punished.
         */
        else if (type == Consumable.items.DOG)
        {
            if (coin.amount == 0 && score >= 0)
            {
                score += (int)dog.health * 5;
            }
            else score += Mathf.FloorToInt(-1 * 10 * speed * dog.health);
        }

...
```

New End condition is also added
```csharp=
...
        if (score >= 50 && dog.amount == 0 && coin.amount ==0)
        {
            //... then set the text property of our winText object to "You win!"
            winText.text = "You win!";
            winText.enabled = true;
            // stop time
            Time.timeScale = 0;
            Application.Quit();
        }
        //if there's nothing else to eat
        else if (coin.amount == 0 && greenJewel.amount == 0 && yellowJewel.amount == 0 && garbageJewel.amount == 0 && dog.amount == 0)
        {
            winText.text = "You Lose!";
            winText.enabled = true;
            Time.timeScale = 0;
        }
...
```

## Youtube link:
https://youtu.be/VWwrwoCzul4