In this lecture, we'll start looking at the implementation of the basic gameplay and the feed the teddies game. There's lots of code required to implement that basic gameplay. I've broken this into two separate lectures. As you can see, we have five prefabs here in our project for the burger, explosion, French fries, teddy bear and TeddyBear projectile to support gameplay. We have a bunch of scripts in our gameplay folder, in our scripts folder that also helps support gameplay. Let's go take a look at the key scripts we use to make our gameplay work. Our burger class is a child class of our int event invoker class. We have some prefabs that we save for shooting French fries and creating an explosion when the burger blows up. We keep track of a collider dimensions so that we can clamp our burger. We cached some values so that we can efficiently check for clamping. We set our health of the burger to 100, and we have some firings support. This makes it so our burger is an automatic French fry firing weapon. To do that, we have a can shoot flag that we start off as true. We have a cool down timer that will let us control the firing rate. This constant just helps us position the French fries a little bit above the burger when we shoot. In the start method, we do a bunch of stuff to support efficient clamping. We do some more stuff to support efficient clamping. Here's where we interact with the event manager. First, internally, we add a new health changed event object to our unity events dictionary keyed the health changed event name. Then we tell the event manager that we are an invoker of the health changed event. We also add a new game over event object to our unity events dictionary keyed by the game over event. Then we tell the event manager that we are an invoker of the game over event. The burger class is the only class in our game that actually invokes two different unity events with an int parameter. The burger class is the reason that our int event invoker needed a dictionary rather than just having a fields for particular events that we're going to be invoked. This is why we needed a dictionary of unity events in our int event invoker. Then we add a cooldown timer and set the duration based on some configuration data. We say we want to listen for the cooldown timer being finished. I re-factored our timer to invoke a timer finished event, so that instead of just checking if the timer is finished on every frame of the game, we just listen for the timer finished event. When the timer finished event is invoked, we do something that I'll show you soon. In update, I move horizontally. I move vertically. I stay clipped in the screen. Then we handle some of the firing stuff. If I can't currently shoot, which means I'm currently cooling down, but I don't have any input on my fire 1 axis, then I'm going to stop the cool down timer and set can shoot to true. What this support is, if the player is automatically firing, and then they released the fire key, then they can immediately press it again and shoot it again. This is support for spam firing. If you tap the key quickly, you can fire more quickly than the actual automatic firing rate for the burger. This is our automatic firing rate stuff. If I currently can shoot, so I'm not cooling down right now, and I'm getting input on the fire 1 axis, then I start running my cooldown timer. I changed the flag to false. Even if I keep holding down the fire key, I won't be generating a stream of French fries because this Boolean expression will evaluate to false. Then I create a French fries at my current position. Then I add that little bit that I showed you up above. Then I instantiate the French fries, and I play a sound effect and we'll look at audio in a later lecture. If the burger collides with another object, and that other object is a teddy bear, that's a bad thing. We'll instantiate an explosion at the teddy bear's location, will destroy the teddy bear and will take damage based on whatever damage teddy bears inflict on the burger. We'll look at the take damaged method soon too. I also have an On Trigger Enter 2D method. The burger prefab actually has both a box collider we use for collisions and a box collider that we use as a trigger. If I'm colliding with French fries, then I create an explosion where the French fries are and I destroy the French fries. Otherwise, if I'm colliding with a teddy bear projectile, this is bad, this will damage us, so I instantiate an explosion at the teddy bear projectile location. I destroy the teddy bear projectile and I take damage this time based on the teddy bear projectile damage. That's why I wrote a take damage method because there are two places where I take damage and I only wanted to write the code for taking damage once. The clamping is functionality we've seen before. When the cool downtime or finishes, I reset my can shoot flag to true because I'm done cooling down and now I'm able to shoot again. Here's that take damage method. First, I set health to the maximum of zero or health minus damage. This clamps are held to never go below zero. That's why it's max. It keeps our health from going negative while still taking damage here from this argument. Now that I've changed my health, I invoke the health changed event from my dictionary of unity events. There's invoking it, passing the new value of health as the argument when I invoke the event, and then I play sound effect. I also need to check now that I've taken damage, if my health is zero, remember it can't ever go below zero because of this line of code, but it might be zero and once my burger health is zero, the game is over. I play a sound effect and I invoke the game over event from my dictionary of unity events. Now you might wonder why I provided a zero here because I don't really need to provide an integer when the game is over. But remember, the only events that I have in my unity events dictionary, are unity event with one int parameter. To simplify my invokers and my event manager, I'm always only invoking events with a single int parameter. In this particular case, I don't really care about that int parameter, but I need to provide it anyway. The French fries classes, also a child class of int event invoker. Also with the prefab explosion.In the start method, I get the projectile moving and I need to add the French fries as a point added event invoker. I add a new point added event object to my dictionary of unity events for the points added event, and I tell the event manager that I'm an invoker of the points added event. When the French fries become invisible, which can only happen at the top of the screen if we've shot the French fries straight up and they haven't hit anything on the way out of the game, then here's where I tell the event manager to remove myself as an invoker of that event. Remember that so that we don't have the dictionary holding a bunch of dead objects that used to be invokers but are no longer part of the game. Once I've done that, I destroy the French fries. I have a trigger collider attached to French fries. If the French fries hit a teddy bear, this is exactly what we're trying to do. We're trying to hit teddy bears with French fries so that we can feed them. I invoke the points out of that event with a value of whatever destroying teddy bears is worth, I create an explosion where the teddy bear is. I destroy the teddy bear. I created an explosion where the French fries are. I destroy the French fries. I will point out, if you don't include this line of code, you can actually have French fries destroying teddy bears, and then they just tunneled through the teddy bears and go kill other teddy bears. While that's fun, is not the functionality I wanted in this game, so I destroy the French fries when they hit a teddy bear. I also check to see if the French fries have hit a teddy bear projectile. If they have, I create an explosion at the teddy bear projectile and destroy the teddy bear projectile and created an explosion at me and destroy me, the French fries. This is actually an interesting gameplay thing because we can shoot French fries at teddy bear projectiles to protect ourselves from the damage that the teddy bear projectiles would inflict on us if they actually collide with us. To recap, in this lecture, you learned how we can implement the burger and teddy bear functionality. You saw how objects interact with each other through the event manager rather than having to know about each other. Next time, we'll look at how we can implement the rest of the basic gameplay.