Oh Dear, another question! Look ahead collision detection.
Hello again, I'm ever so sorry about this, I seem to be throw out lots of questions!
Am really stumped on this one, have had a look through the forum and google searching but can't find what I need.
Collision detection, I'm okay with the basics, but have a scenario I've encountered and need some advice on how to sort this;
Apologies if this is old news, I know I've mentioned this in previous threads but it helps give the context - I'm working on a grid based defense game, slightly in the ilk of Plants Vs Zombies. I'm creating so enemies approach in waves from the right of the screen along one of six horizontal paths, and weapons can be placed at any point in a 10 X 6 square grid to intercept the enemies. I'm trying to implement this weapon that detects if there is an enemy on the grid somewhere to it's right, and if so it releases a projectile horizontally towards said enemy. It sounds so easy in theory, but I've hit some snags.
My weapon is spawned from a prototype, and the player can position weapons on any of the game squares in the grid. Only one can be spawned on an individual square at one time, but the player could spawn one instance of the weapon on every square in the 10 x 6 grid, so 60 up to 60 of these weapons could be deployed at any one time.
My first thought was that when an instance of the weapon is spawned, I could also spawn an invisible rectangular actor, offset to the right of the weapon, that stretched from the weapon off to the right, to the edge of the screen, and then I could use that to check for collisions with approaching enemies (I've been referring to it as the range-finder), and that detection could be used to trigger the weapon into spawning a projectile. This would work for detecting and firing, and as I want the weapon to only look for enemies approaching from the right - ignoring enemies that have already passed it position to the left, I thought it was perfect. However, I don't know how to destroy the range-finder in the event that the weapon itself gets destroyed. Both weapon and range-finder are spawned from prototypes, I don't have an attribute that links them together and am not sure how to manage it. I've been trying to figure out some kind of indexing system so that every weapon and range-finder gets allocated a unique number matching number, but it's making my head hurt!
My second idea was to create 6 invisible actors stretching the length of each horizontal path to act as detectors, and then any weapon placed on that path could have a rule checking for a collision between enemy and detector and thus triggering the projectile. The problem with this is that the detector covers the whole of the horizontal path, and the weapons can only fire to the right. If the player deploys a weapon and there is an enemy already in a position further to the left of the weapon, what I want to happen is that the weapon should ignore it - as per my previous idea - however this method doesn't factor that in, as soon as a collision is detected the weapon starts firing regardless - if the detected enemy is off to the left of the weapon then the weapon is simply firing at nothing!
I've found critical flaws in both techniques and have no idea what to do.
Best Answer
-
tatiang Posts: 11,949
I admit I didn't read all of this but in terms of connecting two actors, right before you spawn them, increase an integer game attribute called game.spawnID by 1. In your spawned actors, change an integer self attribute called self.ID to game.spawnID at the top of their rule lists but not inside of any rule. Right before you destroy a weapon actor, change game.destroyID to self.ID. Then If self.ID = game.destroyID, destroy actor.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Answers
Ah! I wasn't thinking straight, I hadn't quite grasped that a change attribute action placed at the start of an actor's rule list, but not in a rule, would only occur once. It's late, I evidently need some sleep! Thanks mate, that is the answer!
You're welcome!
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User