Ep. 103: Shooting Mobs - Game Programming

The Cherno · Intermediate ·12y ago

Key Takeaways

Creates a new mob class that inherits from the mob class and adds the ability to shoot at the player

Full Transcript

hey what's up guys My Name Is AO and welcome to episode 103 of game programming I know it's been a while since the last episode but here we are hopefully this going to be a little little bit more frequent um from here on out but basically what what we're going to do today is take a look at how mobs are going to be able to shoot at other mobs okay so instead of um cuz really they have absolutely no form form of attack right now and I thought that'd be good to uh kind of give them one just because um well it's probably desirable um and so what we're basically going to be able to do here is make a brand new mob that has the ability to shoot at the player okay um it's not going to have any kind of AI okay it's only just going to be able to find the player and shoot him it's not going to walk towards the player it's just going to be a stationary mob that will shoot at the player whenever just whenever okay so let's let's just take a look at that and see what we can do so over here under our mob folder inside our mob package of course we'll create a new class we will call it um um so basically this is just this is just going to be a brand new mob that has the ability to shoot so I'll call this shooter okay really creative um we'll make sure that of course it does inherit the mob class since it is a mob um we'll add the unimplemented methods here which have to be update and render of course and um and what we're basically going to do is copy more or less um well we don't even need to copy anything okay we could make the mob move and we might do a bit of that in a it but we don't really have to because that's not the goal of it so really what we want to be able to make it do is shoot but first thing we should do um is just basically render it so let's go ahead and go screen. random mob at of course uh X you know Y and uh the mob being this thing okay um and the thing is though the thing that people don't realize is that this is actually how easy it is to make a new mob okay one thing we should do is set X and Y okay in the Constructor so we might make a new uh Constructor here called public shooter um Will Set uh in X and in y into the um into the construct into the parameters for the Constructor here and uh we'll of course just assign this doxal to X and this yal to Y in fact we might just multiply that by uh 326 are we 16 size styles I think we are it's been a long time um or just multiply it by um or shift it left sorry by four that'll be the same as multiplying by 16 great that's actually what we did I think in pretty much every other mob so dummy had that as well as you can see um we should also set the Sprite so we'll set the Sprite to be dummy okay not going to do anything fancy um it's just going to look the same as a dummy Sprite that's it now what it's going to do though shoot so this is actually you see this code here there's like no code here almost at all but all this code that's all you need to create a brand new mob okay and in update is where you can call methods like move like shoot which is what we're going to do in a minute um and control the behavior of your mob okay and the render you can control any visual aspects so visual and logic okay and then this is like initialization code so where we want to put the shooter and what Sprite we want to give it all that stuff so um now let's go ahead and actually add it into the level of course we've created the mob type we've created the class we've created the kind of template for it the template but what we want to do now is add it into the level so our current level is our spawn level right that's the actual level that we walk around in um we've added Chaser mobs we added star mobs let's just get rid of I will comment out star and Chaser um and dummy okay honestly like we're dealing with I'm I'm I mean I mean I'm convinced that you guys can see that these mobs all work so there's no need to having a million mobs well not a million but seven in this in this case running around and distracting us from uh from what we want to test out so I'll just come them out um so we'll add a new shooter and we'll just pop it in I think Dy like 2055 that was a pretty good location okay and let's import shoot shoter okay so now we've added into into the level we've added one shooter of course into the level and here in um in our shooter class you can see that's all it is so if we just launch this really quickly you'll see that what we should get is right over here we have just one mob and it's just doing nothing right it can't do anything because there's nothing in the update method if we were to put something along the lines of let's just say move up um so move 0 - one here just into the update method um then it would just move up and that's all it would do you can see that it doesn't even have any animation nothing it's just a mob okay um so yeah I mean I'm pretty sure you guys already knew that but just in case you didn't that's how all of that works so into the update method now let's just make it shoot at the player kind of like a turret cuz it's not moving around so we'll make it kind of like a turret at this point so what do we need to shoot at the player okay I actually saw a comment or a forum post on my website the other day um and by the other day I mean like a month probably about 2 months ago go uh that said that it would be very very hard to implement this kind of stuff cuz I think someone else on the Forum asked how one would do this and the other guy was like it's really hard uh it's really not okay it's honestly the whole thing's going to take us three lines of code and you could actually probably condense it into one line of code but um basically we need to get the player cuz we want to shoot at the player so we need to get the player we need to somehow be able to get the player's position since we want to shoot at the player so let's go ahead and get the player from the level because we have a wonderful method in level in n level class called get client player and what that will do is just get the local player because obviously this is going to be a multiplayer game um and this get client player will just get the first okay the very first player in the players array list because the very first play in the players players array array list is the player that well we guess we're playing locally okay so it's not some multiplayer player it's the player we controlling um great Okay so once we've done that we need to somehow calculate a direction okay because what this will what what this P variable that we've created which is an object of course of the player class um we've set it equal to get the client player okay from the level which is just getting I guess the player that we're controlling um not some other you know multiplayer player and then what we want to do now is figure out um the direction that we want to that we want to get our shooter mob to shoot in okay because um if we just uh bring up paint.net here I've actually already drawn a diagram look at that um I was rehearsing this episode because that's just how good I am um okay so uh what we've got here is basically how we're going to work it out so we can use the aan 2 method here um to actually uh get I guess the angle okay in radians because what we what we know let's just say that we are um this mob over here right or rather let's cuz it's red let's just make let's just make it that is us I was going to make it the enemy but doesn't matter that's us and then this here is the enemy or no rather red is the enemy blue is us okay and what we want to do is we want to shoot we we want to get the enemy to shoot towards us right so basically we want the bullets here or the projectiles to follow uh give me a good color to follow this kind of line yeah this black line here um so to work that out we need two thing we need a bunch of things okay and first thing is uh the X and basically all we need actually to be honest is the X and Y are coordinates or the I I guess the position of both of these mobs okay the player is also a mob by the way it's just happens to be controlled by us um right okay we just we just need that that information okay then once we get that we can work out DX okay Delta X so what that is is it's basically just x - x doesn't matter which one cuz we're going to uh well I guess it doesn't really matter which ones comes first I don't think um if you really want to be good though You' probably ABS it so absolute it so there's always positive because you want a positive result here that's the idea um so uh we basically just go ahead and say x - x and that is what DX equals right and then Dy is just going to be d y = y - y okay and by y - y I mean this y - this way okay so that's what we want to do then what we get is two variables DX and Dy and then once we've got those two uh we just basically feed them in into this method and it will do some pretty fancy stuff basically work out the inverse of tan okay um and then that will give us of course an angle which we can then use um to figure out the I guess the angle or the direction of the projectile that we want to fire towards okay so that's how that that's the mass behind it okay so um I'm going to stretch this out over several lines of code just because uh probably be easier for you guys this way so I think positions in now s is doubled so um go ahead and say PX which is going to be the player's X position that's obviously just going to be play player. getx or P.G X py is going to be play to get y it's pretty uh obvious uh X is just going to be get X Y is just going to be get Y and that's just going to get our Y in fact I'm pretty sure we don't even need that because I think we've got access to it anyway yeah we do okay uh and then that's it okay that's all we need to work out our Direction here okay so our Direction then is going to equal as we kind of spelled out here uh math do math.atan2 um and then we just simply feed in I guess DX and Dy let's work out DX and Dy separately so DX is just just going to be uh PX - x okay and Dy is just going to be pyus y all right that's it um so this of course you could just substitute this for and you wouldn't need this variable at all and you would get rid of two lines of code here because you do it for X and Y so what I mean is you could just do this and then you wouldn't need that um but again fact yeah let's do that I'm pretty sure you guys can can tell what is what here and this is get y okay there you go Okay so we've got DX and Dy now we simply go make sure you do Dy first by the way otherwise you'll get ridiculous results um right that's it okay we now know our direction of fire so now what we have to do is actually shoot the projectile luckily for us that's very easy to do because we made a shoot method in the mob class it's right over here and all that does is it simply shoots a projectile with the given angle and it adds it into the level of course let's get rid of this uh commented code as well we don't need it okay wonderful so now let's shoot it of course at our Exposition that's kind of where where we want the projectile to start from um just our X and Y cuz we we want to be able to shoot it well I guess the shooter mob needs to be able to shoot it from itself x y and then the direction and that's it okay this is in the update method it's just going to constantly do that 60 times a second okay there's no fire rate uh restriction nothing okay so let's go ahead and and try that out you can see there's a line of fire to us right now okay and as I move look at that now it's it's and as you can see by the way like the Collision for that is just perfect as well so everything is working as if it was us firing around projectile like that okay now one thing you probably notice is that um the the the start of this is uh not in the center and that's that's not our problem that's not our actual problem that's just the fact that um there's no rendering offset so what you probably want to do and not just rendering offset but what you probably want to do is um one of two things so you'll not that dummy uh does not have any kind of offset at all okay whereas all the other mobs that we've made such as the star mob for example actually has an offset of -16 okay so what you could do is offset that render rendering thing so that it actually gets rendered kind of from the center rather than from the top left corner or what you could do if you don't want to do that so that's one that's one way right just minus 16 for both X and Y or you could add 16 to this and it will give you the same result I'll show you but um yeah it's up to you what you prefer to do so you can see that's all that's originating from the center here um and if I do the same for get rid of that and just call minus 16 twice here you can see that we get the same result okay it's originating in what is roughly the center okay and you can tweak that however You' like okay wonderful so what about if he wants to move well we've kind of covered that before so in dummy let's just make him move randomly right so I'm just going to copy this entire logic here um and paste it into here okay and then there's a bunch of stuff here that we should change so for example uh we need a variable here called time so private in time equals z we also need two integers uh xaal 0 and y equal Z all right and I think that's oh we need animated Sprite as well um so we need all this stuff and we'll just pop that up here and we should be set let me just get rid of that okay so let's just try that now what our mob should be able to do is walk randomly and you can see he's just completely firing at us and there's actually no animation because we never rendered the Sprite so I think we just need this one line of code here from dummy into shooter and we should be um animated and walking properly there you go see so he's he's just he's just constantly firing at us and you can see he's always firing exactly at us look at that he's moving why not he's firing exctly at us eventually he'll get out of range or something but yeah okay see so that is how simple it is it's literally like a few lines of code couple lines of code here just this that's it that's everything to make him shoot so do with that what you will um next episode though what we're going to do is talk about multiple targeting so we're kind of going to cover targeting so what if we don't just want every single mob in the game or this shoo a mob to shoot at the player what if we want that mob to shoot at whoever's closest or uh at a at a random mob within a range right and as you can see this guy is just shooting at us constantly no matter where we are so if we're here he can't reach us but he's still shooting us there's no like range or anything of course there is a range to the projectiles uh that's why they're kind of despawning here um but look at this he'll even shoot at us through walls and everything but um yeah so we're going to talk about multiple targeting and how we can deal with that in the next episode but I hope you guys enjoyed this episode of game programming um if you want to support this series and make sure that I basically I guess have the time to make more of these episodes in other words if you guys enjoy the series and you want to help out I do have a patreon a patreon page it's just patreon.com Theo here as you can see um and go to that basically you can just become a patron by pledging a certain amount of money like a dollar or you know 50 cents whatever that is uh per video and then what you'll be able to do is just basically every time I release one of these videos um I guess you'll get you you'll basically just get charged and then I will get that money thus give giv me more time to make more videos very very simple um the other thing I want to obviously mention here is that I do have a forums uh so in other words if you guys have some problems with your game or um something's going on or you you want to collaborate with someone here you want to uh showcase some of your work whatever you want to off topic talk about your cat your bunny I don't know um you can do that uh the game programming subreddit of course uh subreddit not subreddit sub Forum I guess um the game programming Forum refers to this series you can see it's really active I mean today is the 1st of March and there's already been uh two posts in just this forum um so and i' I've haven't even made a video in like a month uh so this is really this is really um active which is good and you can see that all of these things basically have replies okay out of this out of like the tons that I'm displaying here only two don't have any replies okay and it's only been like a couple days it's been like 3 days so not even 3 days two days so um that is that's that okay um make sure you go to that make sure you register here you can register it's free of course and um and that's it okay I'll see you guys in the next episode which will be multiple targeting [Music] goodbye

Original Description

Support me here ► http://www.patreon.com/thecherno Twitter ► http://www.twitter.com/thecherno Website ► http://www.thecherno.com Facebook ► http://www.facebook.com/thecherno ------------------------------------------------------------------------------------------------- In this video: - How to make mobs shoot at the player! Credits: - Thanks to Trond Lohne for pledging $5 per video! ------------------------------------------------------------------------------------------------- Website: http://www.thecherno.com Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/thecherno Subreddit: http://www.reddit.com/r/thecherno/ Steam Group: http://www.steamcommunity.com/groups/thecherno ------------------------------------------------------------------------------------------------- Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →