Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Monday, March 29, 2010

Big Update is finished.

I have finally finished the overhaul I was talking about couple of days ago. And I think I managed to create some good stuff, albeit messy and hard to understand. 


Part 1: The Problem


The problem I had was the arrays. At first I wanted to be able to store them in a class, or a structure or even inside a text file that I would load. I rejected the first two, because making a class directly for a single array would be contrary to Object Oriented Programming and text files were useless since placing them outside my program was just as bad as bluntly dropping them in the middle of my main form, which was what I wanted to change. 


At first, I had arrays. My procedures would activate themselves with the arrays I needed by parameter and would fix the matrix accordingly. The problem with this approach, is that I need to know exactly what arrays to load when executing my procedures. This is chaotic because of transitions, going from map to map, as my game is not linear. I know not if the player will take a turn right or left. How can I decide what tile array to load in my functions ? This could have been hell...


Part 2  The Solution


 Instead I made a Terrain class. This class would have several parameters to hold the various arrays such as the tile and sprites arrays. As well as track which map is to its right, left, bottom or top. So instead of giving my sub several arrays. I would give him an instance of the clsTerrain class that represented a map. For example BasicRoad. I can them access its parameter inside the matrix such as BasicRoad.Spritemap or BasicRoad.TilesMap.  I would have a block of code on top that job would be to instantiate each map and give them the geographical location in relation to the other maps. 


The clsTerrain class:

So now that all our maps are there, we need to make our player able to switch maps. This is called a transition, it is triggered by stepping in a tile and pressing the according button. If you are at the top tile and you press the top arrow you get to the map that is right on top of the current one.But then we need to identify which tile is a transition, I could just leave it like if you are standing a tile in the border and you press the according button you get thrown to the next map. But then what would I do with doors and such ? I chose to make transition recognition  as early as I can. For this I had a couple of choices: I could just programmatically make them by telling him which tile to mark as such by hand. That is tedious and stupid. Instead I choose to add a third array and in there marking the transitions myself once and let him do the work. But how can a tile remember that it is a transition ? By adding a property, so I created a Tile class that inherits from picturebox and I added a couple of properties like IsTransition, a boolean value that marks a tile as one used as transition.

The clsTile class



This being done, I only need to configure the movement sub of each direction to respond if they are on a transition.  So the procedure is now ( in two parts ):

The moveRIGHT subroutine

Pretty big procedure. Theres 4 possible choices:
  • You are NOT on the border and NOT on a transition tile: You walk normally
  • You are are ON the border and ON a transition tile: Switch to next map
  • You are ON the border and NOT on a transition: Walk as long as the direction you're going is not out of bounds
  • You are NOT on the border and ON a transition: This is a "Door-transition", this will teleport you somewhere else. Not yet implemented
There IS a lot cleaner way to do it, but this is just a project to "make it work". The really big thinking process will be done in the final version of the project. Until then I just hack my way through my game and I will optimize later on. This is to keep me from falling out of morale. Since this is my first project that has a slightly bigger scale than normal ( for me ), I think that If I was to take 3 weeks to think out a single perfect function I would drop the project after 3 days. Now I can see my progress clearly, establish goals and accomplish them with relative ease, giving me the satisfaction needed to go forward.

Part 3: What's next ?
Right now, I have no idea. I'd like to take some time to create some more tiles to make rather more complex maps to bring me to the next features. The next logical course of action would be to make "Doors transitions", basically when I am walking on a door transition I get "inside" the building. Which is a map by itself. To do that I first need to create some kind of house tiles and that means a lot of work since all I did yet was some crappy grass and patches of road. And I was lucky with those, I just happened to arrive at a really good result on my 2nd try. I tried to make some mountainside tile a few days ago and it was really crappy. So lots of studies and practice incoming. I'd welcome any kind of suggestion, I understand that since this is not my final product I could just go to some website, rip some sprites and use them like some of my colleagues are doing. But what would be the point ??? I'm going to end up doing all the work anyway, might as well do it now and gloat the power of my magnificent tiles for a while. I would agree with the person that tells me that since they're nowhere near done yet that my tiles are "un-gloatable" but its a work in progress ! At least I can be happy in the fact that I don't need to steal all my shit. Anyway. I should also start thinking about the music and the game-atmosphere like the story and characters, in short the non-VB stuff. I'll be sure to keep you posted with updates. I still have a couple posts about problems that needs some explaining. I would like to explain in details how the transitions work because I think my system is pretty damn well though ( without wanting me to give myself too much credit ). 

I'm pretty happy with my progress so far. In ONE month I went from a corpse-less form with nothing at all on it, to a corpseless form with nothing at all on it BUT at least now I can make characters pop on the screen and move around. OK OK, I admit. Some people could do that in a day and do it better. I am not one of those people, I am studying in my second year. This is some pretty heavy stuff for the amount we learned in class. And I plan on making a LOT more with increasing complexity. I am talking of course about the battle system, and inventory and menus and all. I have yet to know what and how I will do this, but it will be grand I think. 

Until then ! See you on top of the next article.

Friday, March 19, 2010

I love the smell of burning CPU in the evening, smells almost like... Victory

Oh yes ladies and gentlemen. I have now a playable version of my game, its not much. But it is the content of all I've been doing these past 2 weeks. You can browse the code here and download an executable from this page.

And I DID solve my Un-Escapable loop problem, I'm going to make a port on this one. 


Milestone 2


After hitting more walls in my previous version of the program I decided to create another form where I would put only the code I need. To make it cleaner and more manageable. It is currently at its third iteration. But while I was looking at my code ( which was muuuch easier to read in a clean environment not chuck full of properties and classes anywhere) I though of the following logistic: I have two matrices, two arrays and 3 methods to help me put those on the form. If I followed my plan to make another matrix for the game hero, I'd have even more. So why not make it only 2 methods, one to fill the matrices, and one to show them. Simple since they do pretty much the same. And to that conclustion I said, why not have a single matrix ? Why would I need 2 layers when I could easily do it with one, and actually make this a lot more simpler and easy. The final result is that I have a single matrix of pictureboxes and two arrays with two methods to fill and stamp it. 


This is the code I used. you can see this all the simpler. 


The loop problem was caused by this: from the moment I decided to use an matrix of pictureboxes instead of images I changed from a type to an object. Do when typing 


Dim tileMatrix(15, 15) as Picturebox


What it did was to create 256 pictureboxes that are actually the SAME picturebox. Because there is no New()   I will create a subsequent post explaining this particular error but for now do understand that the problem was that every picturebox in my matrix was the same picturebox. It was a single one with 256 references through the matrix. 

Thursday, March 11, 2010

Milestone achieved !

The Proof
Well I finally have something to show as a proof of concept:

Isn't that awesome ? Megaman standing on a map full of road and grass ! I made the image extra large so you all could see it. The simplicity of this piece of program is more complex than you can image. I will deploy a few images to explain what code I used to piece this up.

First I used both the "ArrayMap" and the "GridMatrix" technique from earlier posts. I have two arrays - BasicRoad and SpriteMap, that contains the values of which tile and sprite to apply in which position of the matrix ( as seen on the left ).

Then I created a method to take the values in the BasicRoad array to decide which image to place in the tiles matrix.
Afterwards I took the images from the matrix and put them inside a picturebox-like class and placed the tile on the form.
Next I had to do the same with the sprites, but unfortunately I had a problem. My idea was to place the sprite in a picturebox and put that picturebox on top of the tile he's resting on. But there is no opacity property on pictureboxes so wherever I put the sprite he was hiding the tile under him. Making an ugly hole in the map. to solve this problem I used the backgroundImage property and took the image of the tile he's resting on and applied it as background.

I created two classed derived from picturebox: GridTile and SpriteTile so when I scrolled through the form I could use their type to get only the tiles or only the sprites.
 
You can see that I am passing quite a few parameters to these classes, instead of giving them their values after I created them. I made a New() so I could give them their values directly when created. Much cleaner that way. You can see where I use the me.backgroundImage property and used the coordinates from the matrix. 

I'd like to stress that this is NOT a representation of the final project. This is from the vbProject that I made to explore the mechanism that I will use in my game. I still haven't done a single line of code regarding the game itself. The sprite I used are not mine and property of their respective owner. But I can take credit for the tiles since I made them, and they are awesome.

Thursday, March 4, 2010

The "GridMatrix" technique.

The Basics
As you can see from my last posts, I will be using matrices a lot. First off what is a matrix ? It is an array of data arranged in a special fashion with each other. A single matrix may have several dimension
A matrix can have any number of dimension but it is impossibly hard to imagine more than 3. But there are applications for them still. 

A matrix is created thus:

Dim myMatrix(4, 4) as Integer

This will have created a matrix with two dimensions, each containing 5 elements ( starts at 0 ). Easy isn't it ?

The Method I use
We will then look at this piece of code here:

This is the code to make a simple 5 x 5 matrix and write the column number to the console. Note than I did not generate values inside the matrix, it is still empty. I allowed my program to follow the flow of the matrix and reflect the geometry of the matrix itself. 

I use a sweeping technique, I choose each line one by one with the first For Next loop. This will allow me to "plug" myself in a single line. After this I sweep through the columns using another For Next. Then moving unto the next line and repeating. 

Couple interesting points here.  myMatrix.getLowerBound(0) will take the lowest position of the first dimension ( in this case the dimension 0 ) and will go up from there until he encounters the upper boundary of the dimension. If I wanted to make a 3-dimension matrix I would have written 

Dim myMatrix(4, 4, 4) as integer

The output is then: 


Wednesday, March 3, 2010

Design UPDATE !

Yes, an update already. I've been messing around with my IDE and decided to give a go to the design of the game. What I did here was to generate the grid of the game window. As you know in earlier RPG's, the world was a series of tiles aligned in a matrix pattern ( Meaning for example: a 16x16 grid, showing 256 tiles total ) and your character and anything in the game world occupied a single tile. I made a few samples to give me a better idea of what I wanted my game grid to look like.


I had a choice between placing each individual tile on the form, or programatically add them after the form loads. The advantage in adding them after the form has loaded is that your form can be easily modified. As well as giving a greater number of design tweaks. I tried both method thought, placing them manually and automatically. And here are my results:


The one on the left is the automatic, right now is manual.

I chose the colors at random just to give me an idea on the output. Not a lot of difference you might say ? Well when you look at my design form, you can see it is a lot less cluttered. 

Automatic generation yields a much cleaner form designer.


The one on the left is the auto-generated window, the one on the left is the one I added every single tile by hand ( with the help of CTRL+V of course ). You can see the absence of picture boxes on the one on the left because there is currently no tile alive yet. They have yet to be created. Also, the code for autogeneration is MUCH smaller than the manually added at start. 

Let me explain why: When placing controls on the form, even thought you cannot see the related code, some is still created. For a single picture-box, is looks like that
      Me.PictureBox195.Location = New System.Drawing.Point(288, 372)
        Me.PictureBox195.Name = "PictureBox195"
        Me.PictureBox195.Size = New System.Drawing.Size(32, 32)
        Me.PictureBox195.TabIndex = 213
        Me.PictureBox195.TabStop = False
Multiply that by 256 times, and you get quite a big file to load already at start. Meanwhile, when adding them automatically when the form loads, they are added one by one with much cleaner code thus making your source code smaller and lighter. The code I used for both forms is thus:

The first one is for the auto-generate, and the second one is manual. You can see on the second image that the only code running is the color-chooser, thats be cause there IS NO code needed on the main() procedure for showing all the picture boxes. But when you compare the first to the second, one simple method is much cleaner than 265 chunks of designer data.


All in all, auto-generation is a much better choice for the purpose of making game because the grid is not static. As we move to the edge of the screen I have to be able to change the map very quickly and efficiently .