I'm currently building a 2D platformer using Monogame and I'm having a bit of an issue. The way I have designed my game is that I use an array to draw out the map of tiles, which all have collision, like this:
protected override void LoadContent()
{
map.Generate(new int[,] {
// 0 = no tile drawn
// 3 = tile is drawn
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{3,3,3,3,3,3,3,3,3,3,},
{3,3,3,3,3,3,3,3,3,3,},
}, 57); // size
}
However I'm trying to think of a way that allows me to load another array that of course would have tiles placed differently once the player reaches a certain point, for example, using the array in my question, if the player spawns on the left and reaches the last tile on the right.
What would be the simplest way of trying to accomplish this?
Here's a good reference, supplied by Microsoft. This will allow you to examine and breakdown a working version of a 2D platformer.
https://msdn.microsoft.com/en-us/library/dd254916%28v=xnagamestudio.31%29.aspx
In brief, this is what I would expect to implement, regarding a 2D platformer and several levels (not scrolling).
Define the total screens, block size, total blocks to draw on the screen.
Create an array of screen block data.
Set the game screen number to your start screen.
Use that number to reference into the screen block data array and draw the blocks on screen.
The above data could be generated as a binary file and loaded in at start-up, or for ease created as an array of screne block data within your game class.
I'm sure the tutorial I've linked will guide you in the development of your game.
Related
I want to make RPG game using Unity2d with the tile feature to draw the game map.
I created a new class inherited from UnityEngine.Tilemaps.Tile and overrided void GetTileData.
In void GetTileData I determine the sprite to show for each tile according to the tile's neighbors.
See the image below. The source image is input from the inspector. The input is only one image like this below. I don't want to make massive input images caus that totally mess up.
But then I have a problem. When in the game I have to extract certain blocks from the source image and combine them into a new sprite to show onto the map as a tile sprite.
Just want to know, if I have the 4 rectangles known, and want to combine them into a sprite like the image above, how can I do that?
There is no easy way to do what you want in code. Simply use a tilemap with smaller base tiles.
I had an idea in my game of having 3 layers to the map, the first being the ground, the second being the roads/grass/etc, and the third being impassable objects such as walls/buildings/lakes/rivers/trees. I have it so the player is centered at the middle of the screen and the layers of the map move in the inverse direction that the player wants to go. I was thinking I would have the different layers that way if the playerModel overlaps whereever something is drawn on the impassableLayer, then the playerModel would shift back. However, as I am new to xna, I don't know how to get the game to recognize that the playerModel is overlapping the impassable objects on the impassableLayer. The impassableLayer obviously only has objects drawn on it, and is empty anywhere there isn't an object. Therefore I can't just say:
if (playerModel.X > impassableLayer.X)
{
impassableLayer.X++;
}
As this would always be true.
Is there a way to tell if an object is overlapping a layer?
What you are looking for is collision detection I belive. You want to keep objects from passing through each other. It isnt as simple as the idea you had. True collision detection will take alot of work, but their are tons of tutorials for it.
Youtube tutorial on per pixel collision
Per-pixel collision on MSDN
Bounding Box Collision
You can always to a search here on the site or google, there are lots of resources for this out there.
I'm looking for some idea's on how to create the 2d layout (walk path) of a game.
The tiles will be 32x32 pixels aswell as the character moving on the tiles.
I've thought of two ways currently, havn't tried any of them out yet.
The game will be shown in a window of about 400x300 or 512x356 something.. not much bigger than that where the game map itself could be bigger then 1000x1000. So only a portion of the map will be visible most times.
So here's my 2 options as far as I've thought of them.
[1]: Create the entire map at the start (which might increase the loading times but it will make it more easy to move the map around) including each object, player, enemies etc etc.
[2]: Create only the map seen and 1 tile around it (which is not visible) so I can make that one visible with a story board and remove the row / column which is out of bounds and create a column again.
Which means, If I have a room of 5x5 tiles, and I load 7x7 tiles. If I move right, I move all the tiles left and keep the player in the middle of the screen. Then I remove the far left set of tiles and create a new set of tiles on the right side, which will hopefully be done before the storyboard has finished moving.
In the second method all I have to consider is objects that are larger then 32x32. How could I best react on those.. However, this is a problem I'd like to address later.. Right now I prefer to know people's opinions and possible better methods.
I do apoligize if this question is no apropriate for stackoverflow.
Just be careful with the "load" word. Loading, means taking the data from the disk/drive and putting it memory. For that, you should load all the current world/map at once, before the game start. Trust me, you don't want to get yourself in dynamic loading stuff while the map is running.
But you only need to load the definition of your map, you don't need to create the instance of the object at that point. So you can create the instance from the map definition as the player walks... but are you really that constraint in memory? I can understand that for a game like Minecraft or Terraria where the map contains virtually millions of tiles... But for 32x32, you can consider making them all when you load the definition.
Of course, whatever the solution, you should only draw/compute/collide/update the tiles that are visible on screen.
I have to make a program on particle systems in C# and I'm using Visual studio 2010. I must not use any libraries like OpenGl, ... I can use just Graphisc library from C#. I tried to read some tutorials and lectures but I still don't know how to implement it.
Can anybody help me understand how I should decompose my problem? Or direct me maybe on some useful tutorials? It´s new for me and I'm kinda stuck.
My task:
Program a simple generator luminous particles in the shape of a geyser from a point generator, view the particles as different colored dots moving across a single track on a black background in parallel projection.
Look up BackgroundWorker and RenderTargetBitmap it is probably best to do this in WPF
Psuedo code
backgroundWorker()
{
while(isRunning)
{
update()
draw()
}
}
update()
{
for each all particles
{
update gravity and/or relativity to other particles
}
}
draw()
{
clear bitmap
for each all particles
{
draw your particle
}
set it to your container
}
This is based upon a game loop
Though not a complete answer, here is how I would break down the problem if it were my task:
Find a way to render a single particle (perhaps as a dot or a ball). I recommend starting with WPF.
Find a way to "move" that particle programmatically (i.e. by rendering it in different places over time).
Create a Particle class that can store its current state (position, velocity, acceleration) and can be moved (i.e. SetPosition, SetVelocity...)
Create a World class the represents a 3-dimensional space and keeps track of all the particles in it. The World will know the laws of physics and will be responsible for moving particles around given their current state.
Create a bunch of Particles that have the same initial starting position but random initial acceleration vectors. Add those Particles to the World. The World will automatically update their positions from then on.
Apply what you learned in steps 1 and 2 to have your World graphically represent all of its Particles.
Edit: Step 7 would be to "run step 6 in a loop". The loop code provided by noHDD is a very good basic framework: continuously update the state of the world and then draw the results.
I've been producing this 2d tile-based game engine to be used in several projects.
I have a class called "ScreenObject" which is mainly composed of a
Dictionary<Point, Tile>
The Point key is to show where to render the Tile on the screen, and the Tile contains one or more textures to be drawn at that point. This ScreenObject is where the tiles will be modified, deleted, added, etc..
My original method of drawing the tiles in the testing I've done was to iterate through the ScreenObject and draw each quad at each location separately. From what I've read, this is a massive waste of resources. It wasn't horribly slow in the testing, but after I've completed the animation classes and effect classes, I'm sure it would be extremely slow.
And one last thing, if you wouldn't mind..
As I said before, the Tile class can contain multiple textures to be drawn at the Point location on the screen.
I recognize possibly two options for me here. Either add a quad at that location for each texture to be drawn, or, somehow.. use a multiple texture for the same quad (if it's possible). Even if each tile contained one texture only, that would be 64 quads to be drawn on the screen. Most of the tiles will contain 2-5 textures, so the number of total quads would increase dramatically with this method. Would it be feasible to add a quad for each new texture, or am I ignoring a better way to do this?
I'd suspect using a Dictionary would be slower than just using a straight array. If your world consists of 512x512 tiles then you allocate an array 512x512 (262144) in length. YTou can get any given tile in that array by using "array[x + (y * 512)]".
You know how many tiles there are so store an array where each either points to the tile at that position or has an index to the tile in a list (you will likely save memory this way as you can probably keep all your tiles in an array less than 65536, or maybe even 256, in size and thus store the index as a 16-bit.
You then find the area of your array you want to render. To do this optimally you want to avoid switching textures as much as possible. So first thing I'd check to see how big your tiles are I'd then try and combine as many of the texture into 1 big texture. Then set your UVs to sample a sub portion of this large texture. This way you should be able to limit the number of textures in use with a few big textures. Of course you will probably find that a given tile set (lets say rocky ground, for example) will use the same groups of textures. There may also be some blending across to grass somewhere so it may well be worth holding the grass textures in BOTH big texture to avoid doing so many texture swaps. ie sacrifice video memory for speed.
You then iterate over the visible portion of the array and draw all the tiles using texture 1 and then all tiles using texture 2 and so on.
I would recommend to use single VAO object composed from triangles + indices. Calculate position on client side and just update it on each frame (streaming).
Use texture atlas to store everything in single texture (to avoid switching states). You can use texture packer tool.
Render in one shot (if you have depth buffer enabled). Otherwise render first objects that opaque and then render everything that should be blended.