[SOLVED] CollideLine Implementation?


(Bora Kasap) #1

How can i implement collideLine function as a collideLineByProperty function? I just wanna ignore the entities have a specific property of a type and just wanna take the first entitiy of matching the specitif property and type both.


(Ssnyder Coder) #2

I’m not sure I understand what you are asking. Are you asking for a way to use the world.collideLine() method to get a specific entity of a certain type, but not other entities of the same type? Or rather a way to get any entity that is not a specific type?

In either case, can you just change the type of the entity that you want to find, and then just use the collideLine method for that type?


(Bora Kasap) #3

yeah but, i’m using entity type for these collisions

enemy to enemy, enemy to player, enemy to playerbullet

but also, i’ve another string property in enemy => var kind:String;

so, i’m using this “kind” variable to set enemies’ real types like enemy:kamikaze or enemy:hunter…

and now, i need to use collideLine function only for specific “kind” of enemy.

and i’m asking myself right now, why i am not using an extension inside type property to define enemies’ kinds like “type = enemykamikaze” “type = enemyhunter”… and the answer is, i made it like that because of making more performence for collision of lots of(50 bullets & 100 enemies) bullets and enemies… i didn’t want a bullet to check difrerent 7 types of enemies inside every bullet… but now, after lots of time when i started flashpunk, even as3… i’m asking myself again, is that makes a performance difference?


(Zachary Lewis) #4

You might consider using World.getType() in conjunction with collideLine.


(Ssnyder Coder) #5

You could implement your “typeExtension” strategy and compare the resulting FPS with the FPS of your current implementation. If there is a major difference that is unacceptable, then I suppose you could either modify World’s collideLine() method to accept multiple types, or “attach” a supplementary invisible entity of the desired type to your entity. Maybe something like this:

public class AttachedTypeEntity extends Entity
{
	public var entity:Entity;
	public function AttachedTypeEntity(entity:Entity, type:String) 
	{
		this.setHitbox(entity.width, entity.height, entity.originX, entity.originY);
		this.type = type;
		this.entity = entity;
		this.moveTo(entity.x, entity.y)
	} 
	override public function update():void 
	{
		super.update();
		this.moveTo(entity.x, entity.y)
		if(entity.world == null){
			this.world.remove(this);
		}
	}

Your enemies would just have to add it to the world in their added() method like this:

	public var attachedType:Entity;
	override public function added():void 
	{
		super.added();
		attachedType = new AttachedTypeEntity(this, "hunter");
		world.add(attachedType);
	}

Whenever you do collision checks and hit an AttachedTypeEntity, you could either cast it and access the real entity, or just operate on the AttachedTypeEntity (provided you override some of its methods to apply the operation to the original entity). You can also access the extra type from your real entity when needed. Its not the most elegant solution, but it could work.


(Bora Kasap) #6

Oh man! That is the best solution i can think and i’ve started to think about it last night. And you’ve found the same solution with me. But, why it is not the most elegant solution? I can update everything in game for the most elegant solution if needed? So, if you have the most elegant solution please share it with me?


(Bora Kasap) #7

Holy…

I’ve just realized what zachwlewis is meaning…

I can get all enemies to an array then i can remove specific kinds from this array before calling collideLine function… but… mm… how? how can i make collideLine check collisions only for entities in the array?

what if i use this in update? it looks fast :smiley: is it?

private var elist:Array = new Array();
private var e:Enemy;
private var foundedHunter:Enemy;

update()
{
   _elist.length = 0;
   _world.getType("enemy", _elist");
   for each(_e in _elist)
   {
      if (_e.kind == "hunter") _e.type = "hunter";
   }
   _foundedHunter = _world.collideLine("hunter", fromX, toY);
   for each(_e in _elist)
   {
      _e.type = "enemy";
   }
}

actually, i’m not going to use getType function to create this array, i’m going to use a global array and add every hunter to this array when they added to the world… so, it’ll be faster than this… by the way, i won’t need if(_e.kind) stuff… it’ll be more more faster… oh, i think thats enough…


(Bora Kasap) #8

Everything solved! Working great! Thanks for help <3


(Ssnyder Coder) #9

I’m glad you solved it! Yeah, I didn’t quite understand zach’s solution until now. That is definitely the best option. The reason the solution I presented wasn’t “elegant” was because of all the additional entities required that would have no function outside of being a label.


(Bora Kasap) #10

Thats the complete solution… from world class… and from a noob coder :smiley:

public function refreshHUNTERLIST():void
		{
			this.elist.length = 0;
			this.hlist.length = 0;
			this.getType("enemy", elist);
			
			for each(var filtering:Enemy in elist)
			{
				if (filtering.kind == "hunter")
				{
					this.hlist.push(filtering);
				}
			}
		}
		
public function collideLINES():void
{
	if (this.hlist.length > 1))
	{
		var hunter:Enemy;
		
		for each(hunter in this.hlist)
		{
			hunter.type = "hunter";
		}
		
		for each(hunter in this.hlist)
		{
			if (hunter.lightning == null)
			{
				var target:Enemy = Enemy(this.collideLine("hunter", hunter.x + xtarget * 18, hunter.y + ytarget * 18, hunter.x + xtarget * 700, hunter.y + ytarget * 700, 16));
				if (target != null)
				{
					hunter.lightning = Lightning(this.create(Lightning)).init(hunter, target, hunter.lightningcolors);
				}
			}
		}
		
		for each(hunter in this.hlist)
		{
			hunter.type = "enemy";
		}
	}
}