Tuesday 25 June 2013

Updates and Things to Come

Hey Interwebs,

Sorry for the gigantic delay. I've been busy with so many things in the last several months that the blog got a bit neglected. Rest assured, I have been working, I just haven't done a good job talking about it.

I suppose I should start with the biggest news. I have a website now. You can check it out here. It's not the most amazing thing ever and there's no backend to speak of, but since all I needed was something to sit there and show off my resume and prototypes, it gets the job done. The most dynamic thing I would be working with is this blog anyway, so all I need to do is link to it. I may try to integrate the blog into the site proper later but it's pretty far down on my list of things.

Next: STRATAGEM UPDATE

I have some rudimentary jQuery communication working and the front end is now returned by the server, giving the 1st contact the 1st player and the 2nd contact the 2nd player. Next order of business is to get the turns encoded and sent to the server then handle the responses on the front end. This involves building an animation system, so it's been just enough work to keep me interested in other projects. However, my hair is becoming problematic again (for those new to this blog, I pledged not to cut my hair again until stratagem saw some form of release), so my motivation to get back on it is steadily rising.

One of my other big projects last semester was developing a Flashpunk game for NEPTUNE Canada that could be used to educate people about the project, a massive underwater cabled observatory that allows hundreds of instruments to stream their readings live to NEPTUNE's archives and the Internet at large. We, my teammates, Tom and Colton, and I, developed a skeletal prototype for the game. You can play it on my website here.

We also had a summer game jam a couple weeks ago, this time with an emphasis on developing games for the new UVGD project, Verb the Noun, a minigame collection in FlashPunk. I brought my contribution, Repeat the Level, to a pretty much playable state, although the game doesn't really end yet and the assets are only standins. The idea is that the level will repeat over and over with the obstacles in the same order each time, however, the assets will get simpler and simpler until some of the obstacles are indistinguishable from each other. Players must remember the sequence to reach the end. I'll get back to this one pretty soon. For now you can play the prototype here.

Lastly, I would be remiss if I didn't mention Jeode. What is Jeode you ask. Well the short version is that it's a templating system for creating programs, games in my case, that I have been developing for my own use. The project is in Ruby and you can find out more here, but I plan to have a full post about it in a week or so, once I've taken care of a couple other bits of work I need to attend to. Suffices to say that I'm excited about it, and I can't seem to put it down. And that's a very good thing.

And that's all for now.

Keep calm, and wait for more news about Jeode.

Saturday 2 March 2013

Tsukino-con Followup, Javascript, and a Useful Code Segment

Howdy interwebs!

Sorry this is a bit late, but 1 week is actually better than I normally do.

So the Strategem front end now has most of the functions I wanted to get in for the extremely basic prototype.  It distinguishes which units are yours and which are not, you can save orders in terms of angles.  The on screen pathfinding still has a bug when it switches cases, but as of now it isn't worth hunting down.  On the bright side, aside from the bug, the pathfinding system works just like I wanted.  

The architecture is in a somewhat unmanageable state as all the work has been done in a single javascript file, which is now up to 600 lines.  Yikes. There is a discernible model-view-controller split so the next version should be able to put them in different files and either assemble them with the builder, or just reference them on the server.  Web architecture is still rather alien to me, although javascript itself is less so now. Prior to this work all I had done with the language was a few scripts running in a django system, (that's a python server system, not a slave system rising up against the oppressive admin that stole its wife.)  and some short scripts for a security course.  

Javascript architecture lives in the strange domain of the web script, where there isn't a clear main and scope is an eternal mystery.  Depending on how you look at it, scripts are either run in a space with several global variables, or in a class with several member variables that can be accessed throughout.  Either way, I found that the singleton pattern to be both incredibly useful and easy to set up.  

var Singleton = new function Singleton(){
   //Add variables here
}

I realize it's debatable wether this is genuinely a singleton, or just a global object in which I can put several other variables and can be reliably retrieved with a simple "Game.x", but in the end, with everything being the same script, it had the same effect.  The example I was using online also had the usual getInstance() method, but the Game. syntax was shorter and worked well enough. 

This doesn't detract from my larger point, that in javascript, since document is floating around anyway, having game next to it doesn't seem out of place, making javascript an unusual place where global variables are not a mortal sin.

You know, it dawns on me that I very rarely post code on this site.  I should fix that, and I know just how.  Behold:

function getAngle(xo, yo, xd, yd)
{
    var x = xd - xo;
    var y = yd - yo;
    var angle = 0;

    if(x == 0)
    {
     if(y > 0){ angle = 270;}
     else if(y < 0){ angle = 90; }
    }
    else if (y == 0)
    {
     if(x > 0){ angle = 0;}
     else if(x < 0){angle = 180; }
    }
    else
    {
     //var tmp_angle;
     var tmp_angle = Math.atan(Math.abs(y)/Math.abs(x)) * 180 /      Math.PI;

     var rem = tmp_angle % 45;
     if(Math.floor(rem/22) < 1)
     {
            tmp_angle -= rem;
     }else{
            tmp_angle += (45-rem);
     }

     //var switch_var;
     switch_var = 0;
     if(x>0){ switch_var+=1;}
     if(y>0){ switch_var+=2;}
     switch(switch_var)
     {
      case 0:          //quadrant 2
           angle = 90 + tmp_angle;
           break;
      case 1:          //quadrant 1
           angle = 0 +  tmp_angle;
           break;
      case 2:          //quadrant 3
           angle = 180 +  tmp_angle;
           break;
      case 3:          //quadrant 4
           angle = 270 + tmp_angle;
           break;
     }
    }
    return angle;
    
}

This is a simple function I wrote for Jeubble to make the bubble shooter rotate to face the mouse, butwhen testers found the controls too touchy, I added in this section:

var rem = tmp_angle % 45;
if(Math.floor(rem/22) < 1)
{
     tmp_angle -= rem;
}else{
     tmp_angle += (45-rem);
}

Basically it separates the angle into 45 degree increments.  The original Jeubble version used 15 degree increments, but Stratagem uses 45 degrees to differentiate between the 8 possible1 space moves from a given square.  I also recently used this function in the NEPTUNE game to deal with the different directions available from hex and takes th increment as a paramater instead.  It's probably the best version of this, but it's on my other computer and I don't want to dig through github right now.  Anyway, when you find you've used the same code 3 times, its time to save it somewhere.

Let's break it down.

This first part assigns the x and y components and initializes the output angle to zero.  Nothing fancy yet so I didn't copy it.  But the next part is where things get interesting.


    if(x == 0)
    {
     if(y > 0){ angle = 270;}
     else if(y < 0){ angle = 90; }
    }
    else if (y == 0)
    {
     if(x > 0){ angle = 0;}
     else if(x < 0){angle = 180; }
    }
    else
    {
     //var tmp_angle;
     var tmp_angle = Math.atan(Math.abs(y)/Math.abs(x)) * 180 /      Math.PI;


Typical use of a tan function has issues about where it works.  Firstly, at 0,90,180, or 270,  there is often some sort of asymptotic bug.  Game Maker has probably the funniest version of this where rather than crashing, the angle just grows, so the object will slowly rotate forever.  Signs also don't match in most cases, because screen coordinates are reflected in the x axis (positive y is down).  Lastly, atan is only defined over a 180 degree section between 2 asymptotes.  I've found this varies from implementation to implementation, but the one thing you can always count on is the first quadrant, 0 to 90 degrees, being accurate.

To deal with these issues, my function starts by catching all 4 asymptotes.  First it checks if one of the coordinates is 0, then checks if the other is above or below zero.  The double zero case kind of slips harmlessly through the cracks.  It takes the first if because x==0, but it doesn't take either of the followups, and instead jumps to the end of the function without changing the initial value of angle.  So in the end, the method returns 0, which while not technically correct, isn't detrimental to an place I've used this program.

The else section takes the arctan of the ratio of the 2 absolute coordinates, giving an angle 1-89 degrees above the positive x axis.  The next step is the truncation bit we talked about earlier:


var rem = tmp_angle % 45;
if(Math.floor(rem/22) < 1)
{
     tmp_angle -= rem;
}else{
     tmp_angle += (45-rem);
}


It takes the remainder of the angle retrieved in the else and rounds it to the nearest multiple of 45.  So now we have an angle of 0, 45 or 90.  Obviously there are 5 other possibile angles which aren't represented, so we need to take our answer out of the 1st quadrant and translate it to the correct one.


     //var switch_var;
     switch_var = 0;
     if(x>0){ switch_var+=1;}
     if(y>0){ switch_var+=2;}
     switch(switch_var)
     {
      case 0:          //quadrant 2
           angle = 90 + tmp_angle;
           break;
      case 1:          //quadrant 1
           angle = 0 +  tmp_angle;
           break;
      case 2:          //quadrant 3
           angle = 180 +  tmp_angle;
           break;
      case 3:          //quadrant 4
           angle = 270 + tmp_angle;
           break;
     }
    }
    return angle;

Ah switch case manipulation, how I love it.  Switch cases are in the same camp as for loops in the school of constructs created to quickly perform a simple common operation that can be subtly reworked if you know what you're doing, and made to perform a more complex, but fundamentally similar operation.  This isn't too much of a hack, but it does demonstrate creative use of the switch variable. 

First let me stress that this is done for screen coordinates rather than normal coordinates, so positive y is down.  So we once again start by checking the x and y coordinates against 0.  We can think of our 4 quadrants as hypercube in 2 dimensions.  I'm using x as the bit 0 value and y for the bit 1.  In an integer, these correspond to adding 1 and adding 2 respectively.  This creates a number between 0 and 3 that will correspond to the quadrant.  In each quadrant, we assign angle to be the tmp_angle is between angle and the x-axis.

And then we're done.  So the function isn't amazing or super insightful or even as general as it could be,  but it is useful and it has prompted me to start a pseudocode list for UVic Gamdev of useful functions like this one that can be dropped into any program and modified to fit the language.

Well I hope you find this useful random reader, and don't be afraid to tell me how I could make it better, or how much it sucks, or how it was the missing piece that made your most difficult problem suddenly go away.  I'm not realistically expecting that last one, but hey, a guy can dream.

Saturday 23 February 2013

The Sleeper Has Awakened

Happy month of February Interwebs,

Time for another whirlwind rundown.

So the christmas break happened.  I was going to make a cool game to show off to all those new faces. That game was not the one I set out to make, which fell victim to a combination of Game Maker being ill suited to complex tasks, and me being too lazy and easily distracted to be terribly productive while on vacation.  I also played a lot of Catan.  I'll give that project another chance later in flash or objective C or something.

A game did indeed get made though.  On the off chance that crazy number worshipers, history channel executives, and the Mayans were right against all conceivable odds, I figured I might as well make a game for the occasion and die as I intended to live.  That game was a tiny platformer in which you run around a minimally designed, meteor and lava ridden hell scape to reach the rocket ship that will take you to salvation amongst the stars.  I made it in about 7 hours and it came out pretty well actually managing to have a difficulty curve and a reasonable margin for player error without becoming easy.  I'll have it up on this blog in before the month is over (hopefully).

As for the game a month challenge, that goes better.  I'm taking some artistic liberties and measuring my success by wether I have 12 functioning prototypes by the end of the year, freeing myself up to develop projects over multiple months in parallel. But January's prototype, "Heart the Beats" is functional and was my project from the Global Game Jam.  It is also part of my contribution to UVic Gamedev's "Verb the Noun" project, a club minigame collection which I am also helping to build the infrastructure for.  Verb the Noun is all in Flash using the Flashpunk libraries.  If you think you're ready to move past Game Maker, Flashpunk is an ideal second step and since you are free (and even encouraged) to alter the engine for your own purposes, it's a great way to experiment and understand how a game engine works, and what it needs to do.  Whenever we get VtN online, I will be sure to post a link.  Hopefully Beats will get some polishing before then.

February itself is kind of a toss up.  All through this month, I have been working on an educational game for NEPTUNE Canada as a software engineering design project (it's a course at UVic and that's all you really need to know).  It is almost playable, but I don't see it making it for the end of the month.  If it isn't ready by March, I'll be in serious academic trouble, so it will definitely be ready by then.  The more likely candidate is a primitive version of Stratagem.

Yes I have at last blown the dust off the project and started coding again.  This time the primary concern is the front end, the HTML5/javascript project that I have studied, bu not seriously pursued up till this point. I just committed the first version of the game loop and while so far it's just the render function, it is using a bare framework of the game data to which objects can be added and removed, so the skeleton is there.  I've been spurred into production again by the Tsukino Con Game Jam, and since only the first day has passed, there will be more to come. I'm trying to make the front end rather than start a new project and with a little luck, I can get it to work with the backend by the end of the weekend.

There, you are now up to speed.  And with that, I'm going to sleep, because it's 2:30 in the morning and I have lot to do tomorrow.