jump to navigation

Let’s Make a Game: Target Acquired April 10, 2014

Posted by eric22222 in Game Development, General.

Our character can run around and shoot now, so how about we add something to shoot at? First, some quick programmer art:


This spinny diamond thing is going to be just floating in the air, soaking up our projectiles. Since he won’t have any special stuff going on, we’re just going to give him one state animating through each of the frames I made.

Now, if we were staying on this side of the code, we’d add a bunch of functions to search for colliding carrots, store the index, and all that jazz, but we’re going to take a detour behind the scenes and just build it all from the ground up.

The improvement to code access is my favorite part of SGDK2 over version 1. We could spend some time making all these functions on the front end, but we’d have to recreate them for EVERY ENEMY. With a just a little bit of code-writing, we can have every enemy follow the same rules. That way, when we decide to add a new feature to enemy interaction, we can spend 5 minutes updating a few lines, rather than 5 days updating hundreds of enemies.

I’m having the front end make one function call: Update. This function will be called by every enemy definition once per frame. It’s going to start simple, but will probably be a tangled mess before I even realize it.

Each enemy will have a value called initialized. This is a boolean (true or false) that tells me if I’ve run the update function for this sprite yet. If initialized is true, I’ll proceed with normal update functions. If it’s false, then I’m going to take a moment to set things up. I’ll set the sprite’s health counter, maybe assign him a certain amount of treasure to drop, that sort of thing. We don’t need to reset the maximum health every frame; by only doing this once per sprite, I’ll cut down on the amount of time spent running our function, and hopefully make everything run smoother.

Now that I’ve got the basics in place, I notice that it wouldn’t take much to add some neat features. I’ve got a function called “ApplyDamage.” Right now it just adds to a running total for damage, but I’ll have it update a health bar or something, too. But it would be really easy to have an “ApplyHeal” function, too. Maybe some enemies could heal their buddies, or have some slow regeneration.

Eric, why not just use ApplyDamage with a negative number for healing? Why have a different function at all? My thought is that although that would be less code, it could get me in a lot of trouble if I forget about that “feature.” Suppose I add in an item that gives the player a defensive boost, reducing all damage by 3. Ecstatic, the player leaps for joy… right into a pit of lava. Oh no! But what’s this? The usual 1-damage-per-second lava is actually HEALING the player! Now, I hope I would realize my mistake long before that kind of bug got introduced, but I feel like it’s smarter to keep these functions distinct. So ApplyDamage and ApplyHeal will not act on any negative numbers.

// Increments enemy's total damage by dmg
// Returns 1 if enemy is killed as a result
public int ApplyDamage(int dmg) {
___if (dmg < 0)
______dmg = 0;
___totalDamage += dmg;
___// TODO: CreateDamageIndicator();
___// TODO: UpdateHealthBar();
___regenTimer = regenFrequency;
___if (totalDamage >= HP) {
______// TODO: Kill();
______return 1;
___return 0;

And that’s pretty much it! This is really simple right now, and I can’t imagine it’s going to stay that way. You can see I have a few functions still to implement. We’ll look at some of that next time.




No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: