Units on an Isometric Grid?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
never
Prole
Posts: 10
Joined: Sun Apr 11, 2010 4:20 am

Units on an Isometric Grid?

Post by never »

Hi I want to start by thanking Thursdaybloom, and Kikito for the help so far. I'm new to LUA and the love engine, and I'm creating a small zombie shootem up on an isometric grid. Currently I'm trying to get a few things working and am wondering if anyone would mind lending a hand.

1. The way I am trying to add the units to the grid is by coding them as if they were another tile, but above the first "plane" of tiles. It seemed practical at first. I'm wondering if this is the way I should be doing it, or If I should take a different route.

2. I'm also trying to make the tiles clickable and for the unit to move to the tile that the user clicks.

3. I have the game set up so that if you press the "P" button it will add the human unit to tile [2][2] on the grid, the issue is that when the unit gets added to the field the tile beneath it changes color to the "Highlight tile" that I have created instead of staying the way it was. I already understand WHY it's doing that but I'm not sure how to fix it.

4. If anyone wouldnt mind helping me with making the main unit mobile, and coding the grid to make it clickable I would really appreciate it.

Here is the code for the main plane of tiles and for the units that will spawn above it.

Code: Select all

 function love.draw()	
	
	-- first drawing the background image since it apparently has to generate before any other graphics otherwise the get overlapped.
	love.graphics.draw(background)
	
	-- main plane of tiles
	for x = 1,grid_size do
		for y = 1,grid_size do
			if grid[x][y] == 1 then
				love.graphics.draw(ground,
					grid_x + ((y-x) * (block_width / 2)),
					grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size/2)))

			else -- grid[x][y] == 2 then
				love.graphics.draw(highlight,
					grid_x + ((y-x) * (block_width / 2)),
					grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size/2)))
			end

			if grid[x][y] == 3 then
				love.graphics.draw(hlgreen,
					grid_x + ((y-x) * (block_width / 2)),
					grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size/2)))
			end

		end
	end

	-- units on top
	for x = 1,math.ceil(grid_size/2) do
		for y = 1,math.ceil(grid_size/2) do
			if grid[x][y] == 4 then
				love.graphics.draw(manfront,
					grid_x + ((y-x) * (block_width / 2)),
					grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth)
	
			end
			if grid[x][y] == 5 then
				love.graphics.draw(zombiefront,
					grid_x + ((y-x) * (block_width / 2)),
					grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth)
	
			end
		end
	end
	
	love.graphics.draw(pbutton, 710, 4)
	love.graphics.draw(qbutton, 780, 4)
	love.graphics.draw(lbutton, 710, 32)
	love.graphics.draw(health100, 8, 480)

end
Screenshot 1 is before the "L" button is pushed to spawn the zombie on the desired tile. As you can see 2 tiles are pre-programmed to automatically be a certain color.
Image

Screenshot 2 is after it is pushed. You can see he spawns perfectly but the tile beneath changes.
Image
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Units on an Isometric Grid?

Post by bartbes »

Change

Code: Select all

if grid[x][y] == 3 then
to

Code: Select all

if grid[x][y] >= 3 then
that should probably fix the highlighting.

For the clicking, you obviously made it harder by this angle, but I think you should still be able to do it, what you want to do would be math.floor((x-gridstartx)/squaresize), that should give you the x position, and the same should work for the y position.
User avatar
Thursdaybloom
Citizen
Posts: 81
Joined: Mon Feb 15, 2010 3:43 am
Location: Australia

Re: Units on an Isometric Grid?

Post by Thursdaybloom »

Can you make a .love file of this project and upload it for us to tinker with?
never
Prole
Posts: 10
Joined: Sun Apr 11, 2010 4:20 am

Re: Units on an Isometric Grid?

Post by never »

Thanks for the help, and yes Ill upload the love file.

Also there is an audio resource that gets loaded, its an 8BIT song from http://8bitcollective.com I'm not sure that I needed permission to use it or not.

Heres the file, the attatchment wouldnt allow over 2mb :halloween: http://drop.io/ryne_isoproject/asset/isozombie-love

-- Double Post --
The change you gave me fixed the tile from turning gray but now it turns the tile green where ever you spawn a unit ^^

I think the issue may be the fact that I'm treating the units like another layer of blocks, when maybe there is another way?
Last edited by bartbes on Mon Apr 12, 2010 5:37 pm, edited 1 time in total.
Reason: Double post...
Geti
Party member
Posts: 112
Joined: Tue Oct 20, 2009 6:38 am

Re: Units on an Isometric Grid?

Post by Geti »

It's possible to do what I've been doing in proto-rts and putting instances of a unit inside a tile.occupants table. if you know what you're doing in your draw function you can just iterate through the tile table and first draw the tile, then draw any extra stuff over it.
For example, your highlights might actually be drawn over the tile based on a tile.status variable.

Code: Select all

--all of this is conjectural pseudocode
function love.draw()
 local draw = love.graphics.draw
 for x,v in ipairs(tiles) do for y,tile in ipairs(v) do
  draw(tile.sprite, x*tilewidth, y* tileheight)
  draw(statustable[tile.status], x*tilewidth, y* tileheight)
  for k,occ in ipairs(tile.occupants) do
   draw(occ.sprite, x*tilewidth, y* tileheight)
  end
 end end
end
you would of course need to put the proper offsets into the coordinate values of draw, but still, the example is there.
this does require your tiles to be more than just a number in an array, and requires a lot more iteration (especially in your draw function), but a tile "class" is much more versatile anyway.
you can still keep the units in another grid aswell for their methods and the like (I keep mine in a "units" table) but i think the easiest thing for getting offsets correct in your case would just be implementing the whole occupancy thing.

the reason I'm keeping this as generic as possible is that proto-rts does tile operations in the most backwards way possible, so I don't want to pull all the mistakes I made into your project.
never
Prole
Posts: 10
Joined: Sun Apr 11, 2010 4:20 am

Re: Units on an Isometric Grid?

Post by never »

I understand some of it, but I'm confused on a few things. I sent you a private message :)
Geti
Party member
Posts: 112
Joined: Tue Oct 20, 2009 6:38 am

Re: Units on an Isometric Grid?

Post by Geti »

quick clarification of the proposed new system, I'm busy with phone calls at the moment so I don't want to get bogged down.

iterate your "grid" table.
in each grid[x][y] there is a tile "object" (actually a table) which at the very least has two indexed values, status (a number) and occupants (a table)
when a unit or other object is on a tile, it gets inserted into the tile's occupants table.
when drawing the grid, the iterator would first draw the tile, then the status highlight if necessary, then the occupant.

that's really all there is to it. the unit object can be as simple or as complex as you like. it could be as simple as a sprite index (which you can then look up in a sprite table, for example if unitsprites was a table full of sprites and the second value in it was the zombie sprite, each zombie would have zombie.sprite = 2, which you could draw by drawing unitsprites[zombie.sprite]), some hp (a number), and have all their controls handled by the computer. I'd probably have a controller object too, at the very least, which would be a table full of boolean values.
in this example, my createUnit() function would look like this

Code: Select all

--more pseudocode, but it should work in theory
function createUnit(s)
 local unit = {}
 unit.sprite = s
 unit.hp = 100
 unit.control = {}
 unit.control.up = false
 unit.control.down = false
 unit.control.left = false
 unit.control.right = false
 unit.control.attack = false
end
or something to that effect.

I have to go, I hope this has helped, though it seems a bit rambly in retrospect..
never
Prole
Posts: 10
Joined: Sun Apr 11, 2010 4:20 am

Re: Units on an Isometric Grid?

Post by never »

That actually clarified a lot, thank you again :megagrin:
Post Reply

Who is online

Users browsing this forum: DTmg and 3 guests