Are You A Wizard?

SO.  This is the project I’ve been working on for the last 4 months to a year, depending on where you start counting.  I’m still working on the name, but I think that’s a pretty good working title.  It is a game about being a wizard, and finding spells and stuff, but actually having no idea what they do and probably casting them anyways, even if they end up hurting you.  A wizardly idiot.  A widiot.  Hmn…

This is an alpha build, so it’s not feature-complete, nor is it really even all that close to it.  I just thought people should see my progress, and I did tell some people it would be here, so here it is.

The build may change over the next days and weeks as I try to roll out some more features / fix bugs.  Keep on the look-out.

The most reliable build to play is probably the downloadable one, because Unity is in the middle of a little web player crisis currently.  I’ve linked below a build on the Web Player and another on WebGL, but honestly that WebGL one is almost definitely not going to work.  (Unity does not really support WebGL yet; it is sort of something they had to throw in in haste.)  Chrome now hates the Unity Web Player, so they’ve made it fairly difficult to access, but maybe you’re using Firefox?

Click Here to Download for Windows 32-Bit!
Click Here to Download for Windows 64-Bit!
Click Here to Download for Mac!
Click Here to Download for Linux!
Click Here To Play on Unity Web Player!
Click Here To Play on WebGL!

The Controls (Xbox 360 controller recommended)

  • Move: WASD / Left analog stick/pad
  • Cast Equipped Spell: Space / A button
  • Stand Still: Left Ctrl/Cmd / Y button
  • Speed Up: Left Alt / B button
  • Pass a turn: Stand Still + Speed Up
  • Menu: M key / Start button
  • Confirm: Space/Enter / A button
  • Cancel: Esc / B button
  • Inventory shortcut: I key / LB button
  • Spell List shortcut: P key / RB button
  • Scroll Log Window: Page Up/Page Down / Right analog stick

Change Log

4th Update:

  • Added Town scene, where you can return to buy and sell items and stat upgrades
  • Added money item pick-ups
  • Added Portal Stone item to return to town with
  • Added Purify spell effect type

3rd Update: Fixed several bugs and usability frustrations

2nd Update:

  • Fixed glitchy character animations (Unity 5 upgrade issue)
  • Spell generation curve added
  • Spell scrolls can now be memorized by spending “ether”
  • Rooms generated can now be much smaller
  • Enemies now have a sweet fade-out animation after death

1st Update: Fixed a crash that was occurring commonly when renaming spells.

Schizomaniac

So I participated in the Global Game Jam last January (23-25), but I forgot to post what my team made.  “Forgot”.  This was my first game jam, and I learned a lot about what it means to cooperate with strangers.  I also made some less than stellar choices about use of my time in pursuing my own vision of what the theme ought to mean.  I wouldn’t say I “steamrolled” the team, but I certainly didn’t consult them, so I apologize, guys.  In the end, the part we made together was fun, and the part I tried to tack on at the last minute was confusing and dumb.

Anyways, here’s the link to the game at the jam, with its ever-so-appropriate name:
http://globalgamejam.org/2015/games/schizomaniac

And here’s the game in-browser, if you have the Unity plug-in.

PRO-TIP:  Press ‘L’ to skip to the good part.  Otherwise, suffer through my “story elements”.

PRO-TIP 2: A game controller will give you an easier time finding the buttons, but otherwise they are: alt to switch characters | ctrl to speak? | space to activate things (only for story things)

 

I’ve been working feverishly on a game recently, so maybe I’ll throw a preview up here soon.

CT

alias_method_chain and ActiveRecord

I just encountered an interesting scenario that I felt deserved some discussion:

I needed to make a middleware function for getting an ActiveRecord association, and for whatever reason it occurred to me that alias_method_chain was going to be the right way to do it.  While this instinct turned out to be correct, it didn’t occur to me at the time the fact that ActiveRecord sometimes does tricky things involving method_missing and not actually defining its method names.  So when I then had to make a middleware function for getting/setting an ActiveRecord column, I turned to alias_method_chain once more.  Well, when I tried doing that, I received “NameError: undefined method” for the name of the column that I was trying to alias.  What gives?  Why did one of these work and not the other one?

As I mentioned, ActiveRecord overrides the method_missing method to “define” some of its method names during runtime, but although column names are in this list, associations are not.  Therefore, when you define “belongs_to” or “has_many” in your model, it is creating multiple methods at build time to assist you, named with the specific name you passed it.  Column names, however, are not technically defined in the model or anywhere else in code, but by simply what the database returns when asked for these records.  So when you call the name of a column as a method on an ActiveRecord object, it first recognizes that no such method exists, and it then redirects the request to see if any column can be fetched (or has been fetched) from the database with this name.  (Note that this means you can finagle Rails into recognizing anything you select as a “column”, such as aggregations or other calculated values.)  Alias_method and Alias_method_chain require that the method you are trying to alias exists in the first place, so this simply will not work for this case.

If you really think about it, however… Why try to alias something that doesn’t exist in the first place?  Obviously, you can simply write the method yourself and bypass all of the method_missing magic, as long as you know what to reproduce.  The below is how you would reproduce the accessors for the attribute “name” (with no actual changes functionally):

def name
  read_attribute(:name)
end 

def name=(value)
  write_attribute(:name, value)
end

From there, it should be easy to see how to make the changes you want.

-CT

Easy Alternative to accepts_nested_attributes_for a has_many :through Relationship

I just found this out myself; I have no idea why this was kept secret from me for so long.  If you need an interface for linking multiple pre-existing model_ones to each of your model_twos, your first thought might be accepts_nested_attributes_for.  While that is a great and versatile tool, it is definitely not easy for every situation, of which I think this is one.  You would probably get stuck trying to figure out the best way to remove old links, recognize existing ones, and add new ones, all in as simple an interface as possible for the user.  For instance, wouldn’t this multi-select box be nice?

That was what I wanted.  What I found out (again?) was that you could do exactly that by writing simply:

<%= f.select :platform_ids, Platform.all.map{|p| [p.name, p.id]}, {}, :multiple => true %>

(Assuming that Game has_many :platforms, :through => :platform_relationships. Remember to put :platform_ids in your attr_accessible!)

So there you are.  Rails really is magic.  It seems that any has_many association recognizes an array of ids as a valid way to list and alter its child relationships in bulk, though the benefit for :through associations is much more meaningful.  The syntax is the collection’s name in singular + “_ids”, and the documentation claims the following:

collection_singular_ids=ids
Replace the collection with the objects identified by the primary keys in ids. This method loads the models and calls collection=.

A few notes:

  • Any children orphaned by this operation which are defined as :dependent => :destroy will be destroyed!  This is probably helpful clean-up, but definitely be aware of it.
  • For a :through association, the join model will be deleted, not destroyed (no destroy callbacks), and this appears to be independent of :dependent => :destroy.  This may not have adverse effects in most situations, and in fact is probably a good thing as the relationship model is automatically groomed for you.
  • If any of the child assignments doesn’t pass validation, the whole operation will fail.
  • The assignment operation cannot go through multiple join models, though you can get the current array of ids.  This makes sense in the end, because the second or third join models are probably not simple join models for which we have all the information necessary to create from scratch.

Enjoy!
-Comatose

Unity Engine Lab Project 04 – 2D Mario Clone

Yet another tutorial lab project from Walker Boys Studio.

This one took me longer to complete and put up here, for various reasons.  There is one more lab available, but I don’t think I will take them up on it.  This one left kind of a bad taste in my mouth, since all of the groundwork for the game was already laid (and laid poorly) by them when I started the project.  Further, I took issue with many of the ways they went about things in this project, but deviating from it resulted in a lot more changes to the groundwork (I deviated anyways).  In the end, it was an interesting project, and I definitely learned some important things, but I think I’m going to head out on my own now and start making something I want to make.

Not that the end product for this one was bad!  I think it came out pretty well, even if I was too lazy to make a second level for it.  (Maybe I’ll get around to that some time.)  I expanded on several things from the project, making some animations more fluid or some interactions more reliable and complex.  It’s not perfect still, but enjoy:

Click Here to Play It!

Unity Engine Lab Project 03 – Timer

Another in the series of tutorial lab projects from Walker Boys Studio.

This was an interesting lab focusing on the development of tools likely to come in handy later.  Specifically, it culminated in the development of the app here, which is just an extremely versatile timer/stopwatch that handles many functions common to games, presented using an animation sprite sheet interpreted onto a few planes.  I don’t know, it’s pretty cool to me.  I started kind of wanting to hurl bricks at the screen during the tutorial videos, though, and probably for a variety of reasons.

Anyways, Click Here to play it!

 

 

Unity Engine Lab Project 02 – Space Shooter

The next in a series of tutorial lab projects from Walker Boys Studio.

The description of the project made it sound like an asteroids clone, and I started getting ahead of the teacher until, before I knew it, I had made a full asteroids control scheme for it.  It turned out not to be what the project asked for, but I didn’t feel like wasting it, so there you go.  It was pretty fun to develop, actually, but it really just made me want to move on to personal projects.  Must resist for just a little longer…

Click here to play, and have fun!

Unity Engine Lab Project 01 – Click and Click

I’ve been toying around with the Unity 3D engine recently, thinking about starting up a game project with it.  This is my first lab project, as directed via the online tutorial resource Walker Boys Studio:

Click here to play the game.

 

This will open up in your browser in a new tab, and probably will require a Unity web plugin download. Obviously it’s a fairly simple game, but I thought I’d track my progress anyways.  Enjoy!

 

DRY it up!

Recently I’ve been adding features to someone’s old VB6 application, and I came upon something that was (or felt, at least) devastating to my task.  So much of the code was repeated several times across different functions within the same object–and not all just from cruft–so that making each change required would actually involve making nearly the same change in at least two different places.  I say “nearly the same” because, even more miserably, the same code was interlaced with unrelated code in these different places, making it necessary to carefully examine my additions in both places separately. The lack of comments didn’t help, but I’ve come to expect that by now and am even guilty of it myself quite often.  The duplicated and triplicated code, however, was distressing to me to the point that my physical health was affected: I had to take a break and clear my head just to avoid going crazy!  The timeline for the project is not such that I could consolidate the entire thing, so I find myself forced to just allow it, but not without protest…  which brings me to the purpose of this post:

DRY up your code, people!

DRY, if you haven’t heard it before, stands for Don’t Repeat Yourself.  It means that every concept or algorithm or process in your code should only be present in one distinct place, so that your code base becomes more like a set of useful tools–and more object-oriented!–and less like an inextricable, tangled web.  Think of how much time you have wasted copying and pasting things, and how often your copypasta even has to be edited slightly before it works.  Surely you had a nagging feeling in the back of your head telling you that you could try a little harder and end up with less code, and maybe a more powerful function.  Maybe you even thought for a second that it might be more aesthetically pleasing?  “Beautiful”, even?  Is that just me?

Now, as I mentioned earlier, cruft is to be expected, and sometimes things slip through the cracks.  Often simple deadlines are also to blame.  Allow me to beg you, for your entire team’s sake:  Extra time spent now on simply DRYing up your code will save you (or someone else, thanks) from huge headaches later!  Compounding WET (Write Everything Twice!) code will get out of hand very quickly, and soon enough you won’t know where to start or how long you can expect it to take.

Allow me, as a math major at heart, to make a mathematical illustration in defense of DRY code:
How many multiplication functions are there?  “Gotcha!” you might say, “There is more than one multiplication function, depending on what we’re multiplying!  Or were you trying to say that, for instance, rational multiplication is the same process as cross product vector multiplication?”  Obviously not; for vectors the term ‘multiplication’ is ambiguous in any case.  But the cross product multiplication process does contain instances of rational multiplication, and I should hope that you are using your previously defined function in those places rather than reinventing the wheel (even if, in many cases, a multiplication function would be trivial).  Moreover, rational multiplication is even more akin to other forms of multiplication, such as matrix multiplication, polynomial multiplication, etc.  Would you think to write these all into the same function (as much as possible for your given programming environment) when, at a later time, such functionality suddenly becomes relevant?
Imagine that now you have to find the cross product of vectors with polynomial coordinates, along with providing support for your new type of multiplication in every other place where you’ve used multiplication in the code?  Would your code already be prepared to handle that?  Is your code robust and DRY enough?  Will you make it so, or have you decided again to copy and paste or rewrite code over and over?  Will you keep trying to save time by avoiding the issue… and end up losing time in the end?

Maybe that got out of hand; I suppose this is just a rant.  Maybe it didn’t even end up being that informative!  Sorry, I’m still a bit emotional about it.

Until next time,
CT