2D Game Dev Journey — Day 22: Smart Enemy and Pick-Up Magnet

Welcome to 2D Game Dev Journey — Day 22: Smart Enemy and Pick-up Magnet. Today I go over how I implemented an enemy that can shoot in multiple directions, as well as a way for our player to attract nearby pick-ups.

Starting Point & Setup

Today we will be starting right where we left off in 2D Game Dev Journey — Day 21: Enemies That Dodge.

To get started for today, I went ahead and already created the art asset I will be using for my smart enemy.

Smart Enemy Implementation

What makes this enemy smart is the fact that it can know which direction the player is from it and if in line, it will shoot a projectile at our player. To accomplish this, I again utilized the concepts of inheritance and polymorphism (which I go over in my previous article a bit more) to get the following result:

In the end there, you can see the smart enemy shoots a projectile towards the left instead of down, which results in the player losing in this instance. This enemy type will likely need some balancing in the future, but for now, this is fine.

Pick-up Magnet

To implement this mechanic, I had to make the pick-ups start moving towards the player whenever they had the “C” key pressed down. To accomplish this, I leveraged a pretty common software design pattern called the Observer Pattern and went about implementing it through delegates. When it comes to the observer pattern and delegates, I like to think about it in the following way:

Let’s say there’s a magazine publisher out there, and it offers a subscription service for all magazine releases. Those that sign up for this subscription service can be notified about many things, and really is just up to what the magazine publisher wants to notify the subscribers on. When a subscriber is notified of an event, they can then take whatever proper action they need to take to handle that event. This is basically what I did when coding. Below is how I set up the delegates in my Player class.

Delegates living in my Player class

Take note of how the variable pointing to my delegate is static. This is on purpose. Reason being that in my power-up class, I can now reference and “subscribe” to the delegate in my Player class, and then tell what actions my power-up should take based on the “notifications” they are receiving.

Subscribing to the events in my Power-up class

With a bit more code to tell when the events to fire and implementing proper actions in the subscriber class, I got the following result:

In the future, I will likely add a small effect to sell it a bit more that something is happening when the player hits the “C” key, but I am happy with this for now.


Thanks for checking in and reading today’s article. Tomorrow I will be creating an aggressive enemy type, as well as enemy shields. If I have made any mistakes, please let me know so I can correct them for future readers. If you would like a more in-depth tutorial about anything I went over today, let me know and I will take the time to write something up.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store