Friday, 15 May 2015

Lines and Music, Mostly

It's been a little quiet on the blogging front lately, mainly because I've not really done too much on the game. Most of the basics have been in place for a while now, what's left is tweaking around the edges - prettying things up, making things more intuitive and so on. Yesterday, I added some lines connecting the player to any people they were influencing. God, was that a nightmare...

I haven't done a screenshot for a while, so here's one:

Advanced shaders and photorealism at work here. Possibly.

As you can see, there are now roads. They provide a slight speed increase if you move along them. I did this by making the roads an object (oRoad), then adding the following to the player's step code:

// On Road Increase Speed

if distance_to_object (oRoad) <1
{
    playerSpeed = 6;
}
else
{
    playerSpeed = 4;
}

All this does is check if the distance between the player and the road is less than 1 and, if it is, increase the player's speed to 6. If not, the player's speed is 4.

Then there's the slight graphical change. Nothing majorly interesting there, I just tiled a 64x64 background which was basically colourised noise. I reused the same tile for the roads, just recoloured it grey.

Drawing a Line

Last change you can see is the lines from the player to any people they are currently influencing. As I said, this was a bit of a nightmare. I was trying to get that to work for literally hours. I don't have the original code I was using to start with, but my approach was to try inserting a draw_line command in the object's step code, in the place that dealt with influencing.

Placing draw commands in step code is apparently a no-no. There, I may have just saved you some hours of googling and testing...

Instead, draw commands go in a draw event, rather than a step event. Basically, you add a draw event to the object, in the same way you'd add a create or step event. As an example, in the neutral person object's draw event, I put the following code:

draw_sprite(oPersonNeutral, image_index, x, y);

// Influenced by Player
if distance_to_object (oPlayer) < influenceDistance
{
    if playerLight > 0
    {
        draw_set_alpha (0.5);
        draw_set_colour ( c_white );
        draw_line_width ( x, y, (oPlayer.x), (oPlayer.y), 3);
        draw_set_alpha (1);
    }
}

Now when you add a draw event, GameMaker uses that instead of the sprite. So if I add an event to draw a line, the object basically becomes a line. To get around that, you add the draw_sprite command that you see at the top. That basically redraws the sprite (the image-index part is for referencing a sub image of that sprite - not used in my example).

Below that line,  are the conditions - essentially, if the player is within influencing distance and the player has some light resource left, then the line is drawn. Most of that stuff is probably self explanatory except for maybe the last two lines:

draw_line_width ( x, y, (oPlayer.x), (oPlayer.y), 3);

This draws a line between two points, with a specified thickness - 3, in this case. x and y refer to the objects x and y position, but I found that you can add ".x" and ".y" to an object name to refer to that object's x or y co-ordinates. Here, I've used oPlayer.x and oPlayer.y to refer to the player object's co-ordinates, so the line is drawn from the owning object's position to the player's position, with a thickness of 3.

Let There Be Looping, Low-Priority Rock!

You can't actually see the last change, but I've added some music. It's still a work in progress, I want it to be a little more abstract and machine-y, but it's added and it works, so I can always change it later. It was pretty easy to add. First, save your music file in wav, mp3 or ogg format, then right-click on Sounds in GameMaker (in the left tree thing), select Create New Sound. From there, you can load your file with a bunch of options to compress it or not.

Now the sound is loaded, you then have to get it actually playing. As I am only using one file, it's really easy. In my invisible object, oGlobal, I put the following:

// Play music
audio_play_sound(sndMusicTheme, 1, true);


sndMusicTheme is the name of the sound that I specified when loading it into GameMaker - don't confuse this with the filename. The 1 refers to a sound's priority, which can be anything from 1-100. As I don't have any other sounds at the moment, I could basically put anything, but once I add sound effects then I'll want those to be a higher priority, so I've set it to 1 (lowest). The last argument is for whether or not the sound should loop or not - True means yes, it should. False, pretty obviously, means it shouldn't.

And that's a wrap for this post.

No comments:

Post a Comment