Search | Statistics | User Listing Forums
XNA Resources
XNA Resources ->  XNAResources.com Tutorial Forums -> Tile Engine Series -> View Thread

You are logged in as a guest. ( logon | register )

Isometric tile engine which allows tile modification in game?
Jump to page : 1
Now viewing page 1 [25 messages per page]
View previous thread :: View next thread
   XNAResources.com Tutorial Forums -> Tile Engine SeriesMessage format
 
jegnan
Posted 2012-01-23 1:02 AM (#397)
Subject: Isometric tile engine which allows tile modification in game?


New user

Posts: 2


(108.49.16.131)
I'm trying to make a farming + tower defense game based on the isometric tutorial. In order to do so, I need to add a feature to modify the tile's properties and graphics in game upon left clicking(eg: left click on
grass tile to turn it into a soil tile, place building on tile, etc). However, my knowledge of c#, xna, and mathematics isn't sufficient enough to achieve this with only myself. Could anyone give me some pointers or some code examples for it? I searched everywhere on the web but I couldn't find anything. It'd really help me big time. Thanks in advance.

Top of the page Bottom of the page
Kurt Jaegers
Posted 2012-01-23 2:50 PM (#398 - in reply to #397)
Subject: RE: Isometric tile engine which allows tile modification in game?


100100252525

(96.245.178.53)
Because the tiles are stored in an array-like construct in memory, all you need to do to change a tile is change the value of the item in the array. If you use the isometric picking example to determine what tile is being clicked on, all you need to do is set the appropriate tile (the topmost in the stack if you are using something along the lines of what I have in the tutorials) to change the image that gets displayed.
Top of the page Bottom of the page
jegnan
Posted 2012-01-26 2:12 PM (#399 - in reply to #397)
Subject: Re: Isometric tile engine which allows tile modification in game?


New user

Posts: 2


(108.49.16.131)
Firstly, thank you so much for taking the time out to answer our questions. Now, the way I am understanding your tutorial, it seems like the drawing aspects of your map creation are a 1-time-only, all-or-nothing kind of deal. You use a for-loop to draw the entire default map whose properties are pulled from tilemap.cs.

In order to update the changes to the map, I am wondering if we are required to write information into the tilmap.cs and if so, how do we do it? You mentioned that the information is stored in an array. How can we access this array and how can we write in it?
Top of the page Bottom of the page
Kurt Jaegers
Posted 2012-01-26 3:35 PM (#400 - in reply to #399)
Subject: Re: Isometric tile engine which allows tile modification in game?


100100252525

(96.245.178.53)
The initial setup of the map in the constructor of TileMap.cs is just there to provide some data visually to illustrate how the map is laid out. Normally there would be nothing there (other than maybe setting all of the base tiles to some default value like grass) and load maps from a pre-defined file format. I decided just to inline the sample map to focus the tutorial on the techniques used to draw the tiles instead of on loading maps.

The map itself is redrawn every frame and, in fact, the code in the Game1.Draw() method that does it really should be relocated in to TileMap.cs to let the tilemap draw itself. Again, the code is in the tutorial this way to keep things focused on what the tutorial is trying to accomplish.

Modifying a tile on the map from Game1 is as easy as myMap.Rows[y].Columns[x] = (Fill in the appropriate MapCell here). The next frame when the map is drawn, the topmost base tile for that tile will be updated to whatever you have set.

Again, a full blown tile engine would likely have helper methods in TileMap.cs to make this much simpler. For example, you could add the following to TileMap.cs:

public void ChangeMapCell(int x, int y, MapCell newCellInfo)
{
Rows[y].Columns[x].BaseTiles[Rows[y].Columns[x] = newCellInfo;
}

Then you could set a tile from Game1 by building a new map cell and then calling

myMap.ChangeTopBase(10, 10, myNewMapCell);

You could get much more indepth here, creating methods in MapCell that replaced the topmost tile in one of the stacks with a given tile, and passing that information along via a method in TileMap.cs.

It is important to understand that the tile "engines" created in the tutorials are not complete game engines. They are intended to illustrate particular points and techniques, and as such are bare bones and often the ancillary code surrounding the true focus of the tutorials does not conform to best practices. For example, packing everything into Game1 instead of building a full object model... That would be the right way to do it for a real game, but all of the extra code and text would distract from the focus - positioning and drawing tiles.
Top of the page Bottom of the page
parallelworlds
Posted 2012-03-23 7:31 PM (#429 - in reply to #397)
Subject: Re: Isometric tile engine which allows tile modification in game?


Member

Posts: 6


(108.49.35.83)
Hi kurt,

Thank you for your detailed answers. We have successfully integrated isometric picking into our project! However, I have encountered another problem that I am unable to resolve. I have begun to wonder whether this idea is viable or not.

As I explained earlier, this game is going to be a hybrid of farming and tower defense. Currently, I am trying to use your spriteanimation class to plant plants on the isometric tiles. The reason that I am using this is because the towers will have animation cycles later on and it gives me the convenient side effect of being able to "grow" the plants by changing their static image over time.

Currently, I have been able to use isometric picking to change tile IDs and plant a plant. However, the plant disappears as soon as I take my hand off the mouse button. Here are the relavent code snippets:

I am using a method similar to the way you spawn enemies in your Star Defense series to populate a list of plants:


planttexture = (Content.Load<Texture2D>(@"graphics\\objs\\plants0.09");
for (int p = 0; p < 19; p++
{
plants[plantcounter] = new Plant(planttexture);
plants[plantcounter].AddAnimation("seed", 128, 16, 64, 64, 1, 1.0f);
plants[plantcounter].CurrentAnimation = "seed";
plants[plantcounter].IsAnimating = true;
}


Updating them in a similar way:

protected void UpdatePlants(GameTime gameTime)
{
for (int p = 0; p < 19; p++
{
if (plants[plantcounter].IsAnimating == true)
plants[plantcounter].Update(gameTime);
}
}


And here is the draw Method using your isometric picking:

if (plantingmode == true && mousecurrent.LeftButton == ButtonState.Pressed)
{
Vector2 hilightLoc = Camera.ScreenToWorld(new Vector2(Mouse.GetState().X, Mouse.GetState().Y));
Point hilightPoint = myMap.WorldToMapCell(new Point((int)hilightLoc.X, (int)hilightLoc.Y));
if (hilightPoint.X < 0)
hilightPoint.X = 0;
if (hilightPoint.Y < 0)
hilightPoint.Y = 0;
if (myMap.Rows[hilightPoint.Y].Columns[hilightPoint.X].TileID == 2 |
myMap.Rows[hilightPoint.Y].Columns[hilightPoint.X].TileID == 6)
{
for (int p = 0; p < 19; p++
{
if (plants[plantcounter].IsAnimating)
{
plants[plantcounter].Position = new Vector2(hilightLoc.X, hilightLoc.Y);
plants[plantcounter].DrawOffset = new Vector2(-30, -20);
plants[plantcounter].Draw(spriteBatch, 0, 0);
healthstatus--;
}
}

}
else
{
spriteBatch.DrawString(font, "FAIL", centerpos, Color.Red);
}
}


If you could help in any way, even if its to briefly say whether this idea is viable or not, we would greatly appreciate it.
Top of the page Bottom of the page
Kurt Jaegers
Posted 2012-03-24 3:25 AM (#430 - in reply to #429)
Subject: Re: Isometric tile engine which allows tile modification in game?


100100252525

(96.245.178.53)
You will probably want to do your planting in Update instead of Draw... Any changes to your data based on user input should really happen there.

When a plant is "planted", you will want to add it to a list of current plants, storing the type, position, animation frame, etc in that list. Then, in the Draw method, run through this list of plants and draw them as appropriate.

As your code stands now, the plants will only be drawn if the mouse button is held down, and only at the location the mouse is currently sitting on. By separating the planting process from the drawing of the plants process, you can achieve what you are trying to do.

Also, one issue you may encounter is that your plants (and eventually towers) will appear on top of everything else in the world, including terrain that should block them. This may not be a huge issue, as you may not allow plants and towers to be placed in these areas. If you do, you'll need to take the draw depth of the top-most topper tile on the map space where the plant is planted into account when drawing the plant/tower. You will probably also want to draw the plants in top-to-bottom order on the screen to make sure further back plants don't overlap closer plants.
Top of the page Bottom of the page
Jump to page : 1
Now viewing page 1 [25 messages per page]
Jump to forum :
Search this forum
Printer friendly version
E-mail a link to this thread

(Delete all cookies set by this site)
Running MegaBBS ASP Forum Software
© 2002-2014 PD9 Software