Remember I broke this lecture into two parts. So this is the start of part two for the lecture I started last time. Okay, but at this point, I didn't really have anything to launch my chickens at, and these are ranged weapons. I mean, you gotta shoot 'em at something. And of course, teddy bears are the something that we should shoot them at. So the next thing I wanted to do, was to actually start spawning teddy bears. To do that, here in the update method, I implemented the timer stuff that we have seen numerous times already. Now a couple of comments about what I was thinking about and doing as I went through this code. First of all, I added a new constructor to the teddy bear class. I didn't have one that I could pass in the sprite, the texture 2D and the velocity. I had one that I could I pass in a content manager and a sprite name and a velocity, but that isn't what I wanted to do. So I added a new constuctor. When I did that, I felt horribly guilty, because if you go look at that code, there are four constructors that have lots of duplicated code in them. And as we learned over the previous lessons in this module, there's a better way. And in fact, it was in the die class that we discovered that there's a better way to consolidate that code, even when we're dealing with constructors. We should have the constructors call each other using this. I didn't do that, I feel horribly guilty. You should, you know, feel bad as well, as you read that code. Those constructors should be cleaned up, absolutely, I just didn't do it. The other thing that I did was, I turned off teddy bear bouncing. I didn't want the teddy bears to be bouncing around in the screen anymore, I just wanted them to be spawned. Okay, so because when I did the draw method I drew all the game entities. And when I did the update method, I update all the game entities. Now if I F5, I actually get teddy bears spawned. And I'm very excited and I shoot rubber chickens at them, and of course nothing happens because I haven't coded that yet. And I'm sad again because, here are these teddies, just begging to be hit with rubber chickens, and I can't do it. Very sad. But we have completed the spawning teddy bears step. We also need to, because teddy bears don't bounce anymore, we need to deactivate the teddy bears when they exit the window. So check for teddy bear leaving window. All in common, so now, if the collision rectangle for the top of the teddy bear is greater than window height, that means that teddy bear's below the window. And so I just de-activate it, and we'll clean out that teddy's later. So, unlike the chicken, where we wanted to just keep that chicken object around to save memory, this teddy bear, there's no reason to save it around. We'll just deactivate it and, and clean it up. So, we've done the next step where I deactivated teddy bears outside the window, and cleaning out dead teddies is exactly the way we cleaned out dead teddies before. So, you can in fact, the way you can know that a teddy bear has actually been cleaned out, is we will let one. This will be testing both the deactivate the teddy, and removing inactive teddies. So there we have removed that teddy bear. I have to believe, right, so, the teddy bears are actually dying because of their timer. So that doesn't actually test going out the bottom of the window. So we should go change how long a teddy bear lives, so that it can get all the way to the bottom of the window. So let's make teddy bears live you know, 15 seconds. It's very unlikely they'll actually survive that long, especially once I can shoot rubber chickens. And let me, you can actually add break points while the game is running if you're quick enough about it. And so I've added it so that this teddy bear here, once he's left the window he gets removed. So, we've tested that. We've made sure that both the teddy bear going outside of the window deactivates it, and that dead teddies are cleaned out. So let's change that teddy bear life back to five. Again, we regularly change our code as we're testing, just to temporarily test out certain things. So we've cleaned out the dead teddies. And you'll notice I've been doing this in small steps. But those small steps are sort of in larger chunks, if you will. So the first chunk was getting rubber chickens working. And the second chunk was getting teddy bears working. And now, the third chunk, this is not a game development term by the way, this is just the way I'm thinking of it. [LAUGH] So, the third thing is the interactions between teddy bears and chickens. So this is the collision stuff. So we'll go back to game one. We'll go back into the update method. And this is absolutely the biggest piece of code, as you can see here. So, this is, first of all, let me uncomment it. So these are nested loops. I made a comment about nested loops, though you hadn't seen any yet. And I talked about, you know, processing a two dimensional game board like tic-tac-toe or something. And by the way, the code that was in the tic-tac-toe programming assignment now you should absolutely be able to understand those nested four loops that I gave to you, because we hadn't talked about those yet. These are, in fact, nested four loops nested loops anyway, although the inner one is a four h loop and the outer one is a four loop. I actually had em both as for each loops, and then we'll see that I create a new chicken. And when I added the chicken to my chickens collection right here, my code blew up just as I warned you it would, because I was changing the collection inside for each loop. So I changed this to a four loop, because I might add a chicken. So these are nested loops. For every chicken, we're going to take the first chicken and compare it to every bear. Then we're going to take the second chicken, and compare it to every bear. And then we're going to take the third chicken, and compare it to every bear. You can see why this collision detection thing can get pretty computationally intense if you have a lot of game objects. Okay, this is standard stuff. Both things are active and the collision rectangles intersect So we detected a collision, and I want to apply damage to the bear, and I want to explode the bear as appropriate. So I added a health food, I added a health food. I added a health field to the bear. Bear starts at 100 health, and I also added a take damage method to the bear. Which reduces the bear's health by the damage amount, and if the health is less than or equal to zero, I clamp the health to 0. because we don't want negative health, even for dead teddy bears, and I deactivated the bear. So he'll get cleaned out of that teddy bears list in the game one update method. Let me go back here. And so what I want though is, I want the teddy bear to take multiple hits to die. So I make em take damage and I check to see if he's inactive. And if he's inactive, then I'm going to start playing an explosion. This time it's going to be great. both the teddy bears and the projectiles will blow up. Rubber chickens will blow up when we have that collision. Okay. So, let me comment this out and show you this cool bug that I ran into when I tried testing with the above in place. So, I put this stuff in place, but I hadn't implemented this yet, because, you know, you write a little code and then you test it. And so, it took me a few minutes to figure out what was going on here, because even though the chickens only inflict 50 damage, and the teddy bears have 100 health, I was killing these with one shot. And that is absolutely incorrect. Here's the issue. Because I hadn't implemented this code yet, I wasn't making the chicken inactive. So on one update, the chicken hits the teddy bear, the teddy bear takes damage, so now he's down to 50 help. He's still active though, and I loop around to the next update, a 60th of a second later. And because the chicken is still active, this evaluates to true. So, I hit the bear, same chicken. Same chicken damages the bear again because, oh, this is horrible. The rubber chicken is passing through the teddy bear, ugh, every update. And so that's why, I was getting one shot kills of bears even though I had attempted to implement two hits per death. [LAUGH] Two hits per death. How's that for a slogan? So once I put this back in. So now I've made it so the chicken will become inactive on this collision. Chickens always die on a collision, there's no two hit chicken death. So we explode it. And we're going to add a new chicken down at the bottom of the column. So, I thought about moving this particular chicken down to the bottom again, but it made less sense to me. When a chicken leaves the window, it makes sense to me to drag it back. Once you explode a chicken, it really doesn't make sense to me to move that exploded chicken around. You certainly could do it, exactly the way we did it when a chicken left the window, but I added a new one in this case. And I made sure here that the new one that I was adding was in the same column, same x location. Here's another one of those calls to get chicken start y. This is where I added it. When I got here I said, holy smokes. I need to write a method for that, and chicken damage like that. So now we will see that teddy bears take two hits to die. One, two. Oh my gosh. We still aren't quite done, as you can see, because the chicken, we get explosions, but then the chicken keeps flying up. We'll figure out what's going on there momentarily. But if we go back to the Powerpoint, we can see that this collision stuff had a lot of special processing. We had nested loops, which we hadn't seen before, though we'd talked about. We had multiple hits to kill a teddy bear, which was buggy at first until we completely implemented that. We have both teddies and chickens exploding, so you got the gratuitous extra explosions. There was that cool bug. And that may seem weird talking about a cool bug, but they're mysteries right? You go solve these mysteries and that's part of the fun of programming. And then there was some special chicken processing, the part where I made the chicken inactive, but also added a new chicken to make sure I always had a chicken in one of those columns. Just a couple more things. So the chicken kept moving forward, even after we started the explosion at the middle of the chicken, because we aren't cleaning out dead chickens. So I have come to this part in the code where we do clean out dead chickens, this is just like cleaning out dead teddy bears. And so now, we will see that when I shoot a teddy bear with a chicken, the chicken does not keep going up. I'm sorry, I just got a little distracted there because I was shooting rubber chickens at teddy bears. So, we've now cleaned out the dead rubber chickens, and there's just one more thing for us to do. For efficiency, you can't even see this. But for efficiency, we have those explosions that we cleaned out before, when we had explosions, and we should do that again. Once an explosion has played, it becomes inactive. And so we should get it out of the list. I did make a change that I forgot to mention moments ago. When I create an explosion, if I can find it. When I create an explosion, I changed the explosion class. Early on, when we were using explosions, remember, we had an explosion, and we'd create it, and then hold it around, and then we'd tell it to play at a particular location. This is kind of a different way of thinking about explosions, where you just create the explosion, and play it right away, and then when it's done, it's done. And, sort of we know enough now for that to be an intuitive and doable thing for us. So, once we've cleaned out the dead explosions, we are all done with this game. So, what did we do today? We used the rubber chicken class and some of the other classes we had hanging around. And we built a small game up step by step following the steps I used to built this small game. And talking about the concepts we've learned so far in the course as we did it. So that's the end of Module 11. We're all done with, well, we're all done learning the new ideas behind class design and implementation. And so next time, we'll actually start learning how to add music and sound effects to our x and a games.