Hi, been developing a Tower Defense-game in JME3 over the past weeks, getting pretty far and are at the point where its all coming together. However, I think I may need a pointer on the physics-part. The setup is this (so far)
A world (a TerrainQuad, w. roads, mountains etc., made physical)
A base (a Box-shape, with an added GhostControl, to check for incoming enemies, made physical to stop it from falling through the terrain)
A player (a CharacterControl, able to walk around the map, made physical to stop it from falling through terrain)
Towers (a Box-shape, with an added GhostControl, to check for incoming enemies - fires CannonBalls at enemies when they are in range, made physical to stop it from falling through terrain - and to stop enemies walking through them). Using picking to place them on the terrain.
Enemies (CharacterControl, using setWalkDirection in each update loop, to find the base (no path-finding-algorithm implemented yet. ), made physical to stop them falling through terrain and from walking through towers)
NB: I like the tutorials, and the fact that they encourage the reader to figure out the solution themselves - so I dont need an entire solution - just hints of where to look etc.
Q1: What parts of the above setup would need to be physical in a simplistic setting (right now, its just about getting the game-framework going, eg, A base, mobs reach it they disappear, tower who shoot at mobs, mobs disappear if health = 0, mobs walk towards base etc.)?
It seems as though my base-ghost-control detects a mob in range, and tries to remove it:
which removes the first mob (not immediately, but it removes it) - the second enemy takes a bit longer to get removed and from then on, no enemies are removed from the scene.
Q2: Is this due to poor programming on my part or am I missing something ?
I’m using a TimerTask (implementing Callable) to create mobs (through application.enqueue(Callable c)) - which seems to work fine - although, I’d like to be able to spawn enemies at a semi-random intervals (so it wouldn’t be every 1 seconds, it would be @ 0, then @ 0.3, then @ 1.4 …
Q3: Any api that would let me achieve this semi-randomg spawning ?
I’m also using TimerTask to fire at enemies (when they collide with the TowerGhostControl, a TimerTask is created and scheduled every second) - then when they exit the ScheduledFuture is cancelled. Works, but not quite how I’d like it to. Right now, the projectiles themselves are physical - which could cause a problem as I’d like the endgame scenario to include a large number of towers and enemies.
Q4: Any hints on how to implement a better firing?
I read that Normen recommended the following to another tower-defenes-programmer:
You’d have Spatials for the enemies and wall parts etc, use picking to check for collisions with the terrain and other objects, use simple location info to determine where to shoot etc etc.. I suggest you do the tutorials and read the primers in the wiki (basically work your way through the first part of this page). After doing that, most of the questions on how to do that should be easy to solve and you will also have learned a bag of tricks that will definitely come handy in the later development stages of your game.
Q5: am I misreading something in the quote or did I actually implement it somewhat how he described?
I got a better shooting implemented, although its till too heavy on the physics. First tower looks and feels smooth, two towers feel a bit choppy, and 3+ is unplayable. I must be doing something wrong, since I can see in the jMETests-project that the BrickTower + CannonBall-tests has 100+ bricks + cannonballs and it runs smooth as silk.
I’m looking for a tower-shooting-logic, that could handle 100+ towers each shooting 5 times per second, which would allow me to check if the projectile hit anything.
Afaik you dont need a Geometry in your Controls, when extending AbstractControl you get access to this.spatial which is the spatial you attach the control to.
At the moment your progam is working hard … not smart… you need to think a little less linearly and you can make some big savings. The following are all examples that may or may not be the best way to do something for you - see if you can think of better ways:
For example the towers don’t move (other than spinning to aim) when fired…so why have them colliding with the terrain? Just place them in a fixed location. Check the area is clear, if it is then drop them in place.
For shooting do you really need accurate physics? In fact that can make life harder as you need to predict the targets movement to shoot in the right place. Just have the towers fire lasers/homing rockets/whatever. (Maybe some towers have physics and some not).
For route finding (I know you didn’t mention this yet) don’t try and calculate it for each enemy all the time. Instead divide the board into cells and update each cell with the direction to the exit. Each enemy just reads its local cell and knows which way to go.
Place each tower and when you do “block” that cell in the board and recalculate the route for all the others. Now enemies automatically avoid the tower and you don’t need collision detection there either. You also don’t need collision with the floor. Just move the enemies with a constant y and with x/y direction decided based on their speed and the direction stored in the cell. This also lets you check for complete blocks when placing towers…if it would “isolate” a cell then don’t allow it to be placed.
Generally tower defence enemies don’t block each other - since they tend to come in swarms. If yours do then be prepared for lots of jostling around in busy levels.