Jump to content
  • Advertisement
ethancodes

Algorithm examples of how pickups are handled

Recommended Posts

I'm wondering if anyone has any examples or tips/ideas on how to handle pickups in a game. The game is an arkanoid style game. I'm going to have at least 5 different pick up types, and they are going to be in a queue, where only one can be active at a time. Once one pick up is expended, the next one should automatically start up. Some of the pick ups have an immediate effect, such as ball speed. Others will activate when the pickup is hit, but doesn't actually do anything until the ball hits the paddle. Those type of pick ups have a limited number of shots instead of a time limit. What I'm trying to figure out is what kind of structure I should have for a system like this? I'm just curious how these things are handled in other games, especially if anyone has any examples that would be great. Thank you!

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

My guess would be that most games add pickup stats in their "getStatusXY()" function which then calls something like "getPickupBonus()" which loops through all pickups and calculates the bonus for this specific situation and status and returns a flat number or percentage to add to the actual status value.

Pick out 10 different games and they will all do it a different way.

You have to find a way for your specific case.

 

I would suggest you to make a myPickups array and save your aquired pickups in there.

Then whenever you call your getStatXY() function (e.g. getSpeed()) you add a function in there to check for pickups.

This checkForPickups() function then should loop through all pickups and add all stat changes.

Then you can be sure to always get the correct stat and won't miss any line in code where the stat might be without the pickup.

You could then add a parameter like "withoutPickups = false" which can be set to true and then only returns the stat without the pickup.

 

For example:

16 hours ago, ethancodes said:

Some of the pick ups have an immediate effect, such as ball speed.

When you get the pickup, you add it to the myPickups array. Then you get the speed from function getSpeed() which checks the myPickup array for speed modifying pickups. This way you don't have to worry about pickups when you just want to have the current speed.

16 hours ago, ethancodes said:

Others will activate when the pickup is hit, but doesn't actually do anything until the ball hits the paddle.

When you hit the pickup, you add it to the myPickups array. When hitting the paddle you check the myPickups array if this pickup is in there.

 

This way you could have more than one pickup at the same time. Of course you can also change the myPickups array to one Pickup called activePickup which is referring to the currently active pickup. When the time for this pickup is up, you change activePickup to the next pickup.

If that is not possible for you, you might want to give some code examples or a more detailed explanation on your pickup code.

Edited by SeelenGeier

Share this post


Link to post
Share on other sites

This gives me some ideas and I'm slowly starting to piece this whole thing together. I'm considering using the observer pattern by having a PickUpManager that listens to each of the pick ups. When the pick up gets collected, it destroys itself and sends the info to it's listeners, in this case it would most likely be just the PickUpManager. The PickUpManager will then know which PickUp was destroyed, and can create an instance of that PickUp in it's List<BasePickUp>. When it adds a pickup to the list, it sets a bool hasPickUp to true. Update function will check that bool every frame. If it is false, the game just continues, if it is true, it checks the first item in the list, finds out what kind of pickup it is, and calls a method based on that pick up to activate it. If it is a fast ball pickup, the ball goes faster for a limited time, then goes back to normal speed after the timed coroutine is finished. At this point, the object in the list is destroyed and the next in line steps up, if there is one. If there isn't one, we set hasPickUp to false and the game continues. Where I'm having a hard time is with the multishot pickups. The object in the list could hold information on how many shots there are. But is there a way for me to check if the ball hit the paddle from the PickUpManager? Can I do something that translates to "If multishot pick up is active, watch for ball collision with paddle"? If so, how would I do that? I think if I can figure out this part, I can finally piece this thing together and have a pretty solid base to build off of.

Share this post


Link to post
Share on other sites

First of all, please format your text when posting, that was hard to read ^^

Then I would suggest you use PickupList.count() instead of a flag that you set and unset manually.

I don't know how your code is structured so I can't give a specific advice for the collision.

I would try to implement an event list where you can register callbacks.

 

Something like this:

When the player receives the pickup, you set an event called onPaddleHit which calls the multishot function.

every time the ball hits the paddle, you check if this event has been registered and then fire the multishot function if it is.

Share this post


Link to post
Share on other sites

I'm trying to figure out the Observer pattern. I haven't used it before but most examples look pretty straight forward. However, some parts of my setup are making it a bit more difficult. I believe I've got the observable item set up correctly, that would be on the BasePickUp class, that way all pick ups that implement that are observable. That is this code here:

public event EventHandler PickUpCollected; //Event to raise if pick up is collected

	protected void OnPickUpCollected (EventArgs e) //Notify observers if event is raised
	{
		if (PickUpCollected != null) 
		{
			PickUpCollected(this, e);
		}
	}

And I believe what I'm wanting to do is to set up the observer part on the PickUpManager, so that it is notified any time a PickUp is collected. The things I'm not sure about are:

1. The basic structure of the observer, since the pickups will be spawned and are not something that is just hard coded to be subscribed to.

2. how to let the PickUpManager know which pickup was collected so that it may react accordingly. 

I found this site here: https://www.codeproject.com/Articles/1084848/Implementing-Observer-Pattern-with-Events-Csharp and it does a pretty good idea of explaining the simple version, but I'm still very confused. 

Share this post


Link to post
Share on other sites
Posted (edited)
54 minutes ago, ethancodes said:

I found this site here: https://www.codeproject.com/Articles/1084848/Implementing-Observer-Pattern-with-Events-Csharp and it does a pretty good idea of explaining the simple version, but I'm still very confused. 

It is an event with a trigger. For example:

Say there is a soccer ball in the middle of the scene. There is a player and a AI. We want the AI to chase the player when he has the ball.

So we make the players collision function a Event. Both the player and the AI are observers of this event. So now if the player collides with the ball we can parent the ball to the player, and the AI is also told the player has the ball, allowing the AI to start chasing the player.

 

Think of it has having a IF function that checks to see if that event happened.

On 6/5/2018 at 10:43 PM, ethancodes said:

I'm wondering if anyone has any examples or tips/ideas on how to handle pickups in a game.

Using a Observer pattern gives you more flexibility but isn't needed for powerups.

You could also just make a powerup class with it's own execute function that applies the effect to the player it collides with.

Edited by Scouting Ninja

Share this post


Link to post
Share on other sites

I'm not sure what you mean exactly by that last line. "applies the effect to the player it collides with."  I created a PickUpManager and I'm planning on have it track the pick ups in the queue, as well as apply all effects the pick ups do. So the pick ups themselves will basically just be a ball bouncing around until it hits the main ball or the paddle, in which case an instance of the pick up gets added to a list. The update function on the PickUpManager will then find that and activate it accordingly.

I'm trying to figure out how to let hte PickUpManager know about the collision between the pickup and ball or paddle. It needs to know that the collision happened and what type of pick up it was. I thought the observer pattern would be a good way to do this, but I'm open to different options.

Share this post


Link to post
Share on other sites
10 minutes ago, ethancodes said:

So the pick ups themselves will basically just be a ball bouncing around until it hits the main ball or the paddle, in which case an instance of the pick up gets added to a list.

This ball needs some way to collide and to know what it collided with. This means adding collisions to it; just use the Unity 2D collisions. When the two collides you can let the ball implement the code to the object it collided with.

The code would look something like this:

using UnityEngine;
using System.Collections;


public class DoubleSpeed : MonoBehaviour
{
    void OnCollisionEnter (Collision col)
    {
        if(col.gameObject.name == "Player1")
        {
            col.gameObject.velcoity*2f;//double speed
          	Destroy(self.gameObject);//Destroy powerup
        }
    }
}

Any variable your player has that is public can be used this way. The Collision system in Unity follows the Observer pattern.

Share this post


Link to post
Share on other sites

This was initially how I had the simpler pick ups set up. However, the other pick ups I'm working on do things like "when the main ball comes back and hits the paddle, if a spread shot pick up is active, shoot several balls out from the paddle. And this will be active for a certain number of shots. So I need a way to manage this, as well as managing those extra balls because all the balls will be destroyed on impact (except the last one). Also, my pick ups line up in a queue. So if I have a fast ball active but then I grab a spread shot, I don't want fast ball and spread shot both active. Spread shot would be on stand by in the queue.

Share this post


Link to post
Share on other sites
Posted (edited)
9 minutes ago, ethancodes said:

Also, my pick ups line up in a queue.

Still very similar. Just make a pickup class then make a public queue:

using UnityEngine;
using System.Collections;


public class DoubleSpeed : MonoBehaviour
{
    void OnCollisionEnter (Collision col)
    {
        if(col.gameObject.name == "Player1")
        {
	col.getComponent<PowerUpManager>().ListOfPowerUps.Add(new DoubleSpeed());
	Destroy(self.gameObject);//Destroy powerup
        }
    }
}

Then in your player class you need some way to execute power ups.

Edited by Scouting Ninja

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!