jump to navigation

Let’s Make a Game: Lock and Load April 5, 2014

Posted by eric22222 in Game Development, General.
trackback

We’ve got ground to stand on, and a character to do the standing. Now let’s give him a way to fight off whatever it is he’s fighting off.

I like the way a lot of games implement bows to require some strategy. Generally, you have to actually draw back the bow for a second before you can release the arrow, so you have to make the call on whether or not you have time to make the shot. Plus, you have the option of releasing early for a less powerful, short range attack.

I feel like Minecraft does this well. Fighting with a bow has lots of little nuances that are rewarding to learn and, more importantly, are fun. You can hold the arrow drawn for as long as you want, but you move slowly while taking aim; sometimes you have to make the call between slowly working through dangerous caves with your bow ready, or sacrificing the battle-readiness for speed.

The bow and arrow idea (or bow and carrot. Whatever.) is going to be a little tricky to implement. First off, I need to decide how the player will control it. I could go with mouse controls, where you point to aim, click and hold to draw the bow, and release to fire. It’s intuitive, and the implementation wouldn’t be too hard. However, mouse-centric controls means no gamepad support… We’ll try and make the game usable with keyboard/gamepad first, then add mouse support later if that seems like a fun option.

The bow will be handled by a different sprite. Luckily we don’t have to do a lot of work for the graphics. I wrote a bit in part 3 about SGDK’s frame rotation. I’ve got 5 frames drawn for different stages of drawing the bow. Each of those has been auto-rotated into 5-degree intervals. As a result, our original 5 drawn images has become 360 individual frames.

bowRules

To keep track of the bow’s state, I’m creating 3 custom parameters: “currentRotation” is equivalent to the sprite’s state mod 72. If you haven’t encountered modulo (notated by “%”) before, here’s a quick crash course: 11 % 4 is the remainder of 11 / 4, which is 3 in this case. The sprite could be facing one of 72 directions, and this parameter will keep track of it. The next parameter I’ve got is “power” which is the current state divided by 72, then rounded down. This just keeps track of how far back the bow is drawn. Finally, I’ve got “drawnCounter” which keeps track of how long we’ve held the button for the bow. 

My ruleset is a terrible mess. The top half boils down to rotating the bow with up and down. The next section (starting with “If pressing fire”) increases the drawnCounter while the fire button is held. If the counter reaches a certain threshold and the bow isn’t already fully drawn, it adds 72 to the state, essentially keeping the rotation the same, but increasing the drawn…ness.

The last branch handles the actual release of the bow. First I set two global variables, one for the direction and one for the power. Sprites can’t pass data easily between each other because they don’t really know how to reference each other. After we store that data, we actually create a carrot sprite at this location.

Our carrot sprite is a lot simpler. SGDK has some built-in functions to handle rotating sprites. I’ve set up an “isNew” parameter for the carrot. All sprite parameters start at 0. I have a function set up to check if isNew is equal to 0. If it is, it sets the carrot’s speed and direction equal to our global variables, then sets isNew to 1.

fireBow

The rest of the carrot functions just handle updating the y velocity for gravity, then adjusting the state to match its direction.

And hey! That didn’t turn out too bad!

Next time we’ll whip up some quick enemies so we can have something to shoot.

Advertisements

Comments»

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: