tirsdag den 25. september 2012

Crafting and Combat

I remember less than two months ago, I posted a video of me building a house in Phantobra - The Game. This was done by spawning the materials and then build the house. I was proud to have come so far. Now... I've met the next milestone- Building a house without any kind of cheats or commands.
For a long time I've postponed the making of a crafting system, so I ended up making one of those bets with a friend, where you give each other a task, and failing means you have to give the other part something. So my task was to implement crafting in the game, which I did.

What's new?

Both combat and crafting has been implemented in the game. The complete list of updates since last post:
- Added Crafting to the game. Currently there is 10 craftable items.
- Added new items to the game. Currently there is 18 items in the game.
- Combat has been implemented. You can now kill (and loot) rabbits.
- Two loot items has been added to the rabbit. Animal fat and Rabbit Hide.
- Sand tiles and the sand item has been added to the game.
- New background(s) for both day and night time.
- Tiles now crack when mined to visually show where you are currently mining.
- Terrain has been updated- This is still at an experimental stage, as you can easily see in the video.
- Random caves and holes has been added in the terrain. This is also very experimental.
- Optimized on RAM usage. Reduced the memory required for each tile by approximately 20%.

Crafting

Ever since I got mining implemented into the game I've wanted to add a crafting system. I thought it would be a lot of boring coding, so I kept doing other more exciting stuff. Now where the basics are complete I can see I was a bit wrong. The thing that took the most time was figuring out how my crafting system should work. On top of that I had to make it so that it was easy to understand, which I think I did accomplish. At the moment, you can craft any craftable item, as long as you have the required material. Since crafting is a major part of this type of game, I will expand on the crafting system later. Further restrictions, such as work benches, forges, etc. will be requirements for some items later. I've also though about adding recipes for some of the more unique and rare items in the game, where you need to find the recipe before you can craft the item.
Currently all recipes are hardcoded into the game. Actually what I do is keep all the recipes in one Method, within a switch case. I throw in the ItemID of the desired item, and it returns a list of Vector2 containing the ID and the amount of each required item in the recipe. Currently the code to retrieve the recipes are running every loop as long as the inventory is open. This is not optimal, but I needed to be sure that if I remove an item while the inventory is open that It wouldn't allow me to craft items that I didn't have the ingredients for. This is not that hard to fix. All I need to do is run the CraftingUpdate everytime I open my inventory, or change something in it while it is open.
I really should do that soon...
The UI for crafting is simple and straight forward, at least in my opinion. I haven't decided on the colors yet, but for now I'll stick to what I've got. I am planning on making the colorscheme for the UI customisable anyways.

Items and Loot

As a consequence of both combat and crafting I've added a few more items to the game. This includes "Wooden Sticks, Animal Fat, Rabbit hide and sand. Except for sand, all of these were added because of either combat or crafting. I did put a lot of thought into what items I wanted to add to the game, especially the items needed to create torches. In this style of game torches are a very important aspect of the game, and their availability greatly impacts the game. Personally I believe that torches should be relatively easy to come by, but still require a bit of effort on the players part.
Since I don't want my game to be an exact copy of Terraria, I don't want to do the "get gel from slimes" thing. So I've decided to have more than one source of "fuel" for the torches. The first one being animal fat. Using one unit of animal fat, and one stick currently yields two torches. Animal fat currently only drops from rabbits, which is a harmless mob. My plan is to have a low to medium drop chance (somewhere between 5-20%) for every mob you kill that can be considered an animal. This will (hopefully) give a steady flow of "fuel" for your torches. I also want the player to be able to mine for his fuel, rather than kill animals. So being able to find coal while digging around was an obvious choice. Coal is not yet implemented in the game, but it will be added at a later time.
The rabbit hide has currently no use in the game. My first idea with it was to use it to craft animal fat by removing it from the raw newly skinned hide, but I decided against that. Instead I decided that a rabbit could drop both things. Currently there is a 25% drop chance on rabbit hide, and 20% on animal fat.
I also added the sand item and sand tile. There really isn't much to say about it. It was like a natural thing to add it sooner or later. It reacts to gravity, and falls down if something beneath it is removed. The physics for the sand is very basic. If a column of sand drops it drops just as a I shaped tetris block would do in a game of tetris (except for the grid based movement thing). Currently I am happy with the simplicity of it, so I might keep it that way... I haven't decided yet.

Combat

So eventually combat had to be implemented, and now was the time. I don't think I mentioned it in my earlier posts, but I've actually done some of the code for combat a while back now. I got stuck though, since the rotation of the weapon has some heavy mathematics around it. My math skills aren't sufficient for a lot of the more advanced stuff, so I do a lot of reading, and get help and find examples online, and basically I get something together by doing a mix of that.
So currently Swords, Axes and pickaxes are usable as weapons in the game. Already back before I even did anything related to graphics in this game, I fleshed out the tool/weapon classes very well, so after finishing the combat system it was all a matter of combining it with the calculated stats of the weapons and tools. Damage and mining efficiency are calculated from item type, material and other effects you can add to items. Speed is based on material and weapon/tool type. All three of these weapons are all swinging weapons, which means the swing in an arch from top to bottom in front of you. These are all working and are implemented.
How this works is, I rotate the weapon, and the origin of the weapon (Origin = hilt position). I tweaked the numbers so the sword arched nicely in front of the player. After that I added the swing animations to the sprite and polished the numbers a bit, and voila, Emma can now swing her sword. After I did that the next problem came up- I needed to have some decent collision detection so that I could kill some rabbits. Now when XNA rotates the sword visually, you don't have any boundingbox out of the box, that you can do collision detection on. So after reading up on the subject I found another solution. On your weapon you add a few points, in my case 3 points, and check if they intersect with the rabbit, and if so, you deal damage. Basically what I do is add 3 points, in line with the start position of the tool, and rotate it around the same origin (sword hilt). This way the points follow the sword and you have a decent collision detection for your combat.
I still need to implement stabbing weapons, and a digging weapon. The digging weapon (shovel) will rotate just like the swords and axes, but going from down to up instead.
I belive the stabbing collision will be simple enough, as there is no rotation involved there. I belive using a single point for collision detection would be viable here.

Terrain and Mining crack

I have been playing around with the terrain quite a bit. There are underground caves, and randomized terrain. Everything is highly experimental at this stage, and although I've spend a lot of time playing around with it, and reading up on it, I don't have much to share about it currently. I use a variation of the Perlin Noise algorithm, which I haven't written by myself, but pieced together online. As you will see in the video, the results are promising, but definately needs improvement.
I've also added a small feature- The mining crack. When mining a tile a crack will now appear, indicating your progress. The further along you are, the more the tile will crack. If I remember correctly the crack is a 5 frame animation, and was implented very easily. Basically I just compare the tiles current hp to it's max hp, and based off on the percentage I draw the crack. So every 20% increases the size of the crack.

Backgrounds

So I've added some simple backgrounds, the most noticable is probably the night sky. It's very simple, but it does add a lot of mood to the scene. I do have a lot of plans with the backgrounds. I think I mentioned last time that the moon can be used to symbolize events, like a red moon in terraria causes zombies to invade. Their idea is very good, but sadly they don't utilize the potential of this idea very well, at least in my opinion. I can think of quite a few more events you can related to the moon. Even without the moon there is a lot of things you can do with the background. Imagine that sometimes a rare constellation will be visible on the night sky. This could be a subtle hint to the player that something is going on. Maybe a rare flower, needed for alchemy only spawns whenever there's a certain constellation on the night sky, or a certain kind of mob or animal only shows up during a specifik constellation. Adding lots of small events, gives the player options that can benefit them, but it will not be game breaking if you are unable to react to them.
Now I do know that adding tons of events like this can quickly become a lot of work, so my longterm goal is to see if I can find a fast, and yet solid way of handling these events. Something like everytime I want to add an event, it's all a matter of adding a few lines of code and store it in some event collection. As long as there are some solid, yet flexible, rules for these events, this should be possible.

I think that covered most of it. I didn't get around the RAM optimization at all, but really there's not much to it. I realized some time ago that my tiles had a lot of information in them, that I could possibly store elsewhere. Information like whether or not a certain tile type reacts to gravity. Instead of storing that information in the tile, I would just go look it up somewhere. I was hesitant to do that since then I'd have to jump through ifs or switch cases, for every single tile on the screen. So I went online and got some very nice advice. I create a list of tiles with static information (Not a static class). One for each TileType. Then all I need in the tile to access all the static information is the index number for the list. An example would be: Dirt is placed in the 2nd. spot in the list, thus it would be on location 1 in the zero based index. I than keep a short with the value one on the dirt tile, and use that as an argument everytime I want to retrieve information from the list. This way I save a lot of information from being stored in the memory, and I avoid having to juggle with switch cases hundres of times each cycle.

Phew.. This turned out to be quite a wall of text this time. It's getting late and there is school tomorrow, so I'll wrap it up.
Enjoy the video, and don't hesitate to comment, rate or subscribe, if you feel like it.
 
Phantobra - The Game - Part 5: Crafting, Killing, Construction and ...




mandag den 3. september 2012

Sprites and Animations

I've finally taken the first step towards my programmer education. My current education is an IT Technician (IT Supporter) - But there's not much programming in that one. So I decided to study computer science. So over the next two years I'm reading up on some classes, to reach the requirements to get in.
What this means is, that there will effectively be less time to work on this game, but as it turns out, the need to plan my time better seems to give more productivity, so this might not be all bad for this game.
 
What's new?
I finally managed to put some life into this game. This update includes:
- The player rectangle has been retired, and replaced by a new player sprite.
- Rabbits have been added to the game.
- A full cycle moon has been added to the game (Before it was just a big yellow square).
- Clouds have been added to the game.
- Some minor bug fixes.
 
The player sprite
I finally got around to making a (almost) fully animated sprite of the player. Since this first version looks more like a female than a male, my daughter decided to name the sprite "Emma"- So until further notice, this will be the name of the player sprite.
I think I've mentioned before that all the graphics in the game are made solely by me- And it's no secret that I am not very good at drawing at all. I am... satisfied for now, with my current sprite.
After I finished making the basic frame in Paint.Net I found an awesome beta version of a program called Spriter (Made by BrashMonkey) that required me to chop up my sprite into smaller pieces. This left me with separate pictures of the hair, head, torso, legs, hands and feet. This allowed me to animate my character in a very simple way, by moving these images/parts around as desired.
This gave me the idea to create separate animations for each part, and then draw them on top of each other in the game. This allows me to change the animation, or appearance of each body part individually. So in the near future I will be adding various clothing and hairstyles, which can then easily be changes while playing. I think this is the first step towards changing equipment visually.
I still need to add the Stab and swing animation to the character.
 
Rabbits
So after some moderate success with the player sprite I decided to add rabbits to the world.
Before I started to draw them I went online and looked at various pictures of rabbits, and after that I decided to give it a go. What was really difficult was to keep the rabbit in each frame, about the same size as the rest, while at the same time showing the rabbit stretching (which it does when it jumps).
I consider the rabbits my biggest success this update. I made the entire animation strip, without testing, I then went on to program the rabbits basic AI, compiled and tested it. And voila - Hole in one. It just worked. I stared at those small bunnies jumping around my screen for like 5 minutes, and I was speechless. For me- This was the experience of the month, no doubt about it.
I did end up having to add a bit extra code to the rabbits physics method to keep them falling through the ground occasionally.
 
The Moon
For quite a while, my moon has just been a big yellow square. So last night I had some spare time on my hands, so I decided to make a moon. This time I decided to do this without any help. An hour later or so I had a full moon cycle strip, just ready to be implemented. I colored the moon light grey, which gives the opportunity to tint it with any color I want in game. This allows me to create various moon related events like "Blue moon", "Blood moon", "Hunters Moon", etc.
The moon will have an impact on what happens during the night, based on the current state of the moon, and based on the rare events where the moon will be colored.
 
Clouds
Finally I managed to create some clouds to implement. Currently all clouds moves east (right). I do have a wind variable to play around with, but weather and such will not be implemented for some time.
 
So with all that out of the way, here's the video for this update. It's short, since all the new things does not take that long to show- Enjoy :) 
Phantobra - The Game - Part 4: Rabbits, Clouds and a Moon