okay, i wont dig up this whole conversation, im not totaly convinced with that ... now to the optimisation ... here is my optimised version of your bresenham that is called quit often i guess. Its about double the speed
func traceLine2(_startPos: Vector2, _endPos: Vector2) -> Array:
var lineTiles = []
var startPos:Vector2 = Vector2(min(_startPos.x,_endPos.x),min(_startPos.y,_endPos.y))
var endPos:Vector2 = Vector2(max(_startPos.x,_endPos.x),max(_startPos.y,_endPos.y))
var delta:Vector2 = endPos - startPos
var e:float = 0
if delta.x > delta.y:
e = delta.y / delta.x
for x in delta.x+1:
lineTiles.append(startPos+Vector2(x,x*e).floor())
else:
e = delta.x / delta.y
for y in delta.y+1:
lineTiles.append(startPos+Vector2(y*e,y).floor())
return lineTiles
try it ... how much does this do?
What i did:
- use as many build in function as possible
- try to avoid if statments
- initialise all variables before the loop ... use type hints
If this makes a major impact then you should consider to not call it as a function, do it inline of the other loops as every function call has a overhead. Do not save the result in a array, this has to be created every time, use the loop in the traceline and do the "collision-check" immidiatley
if mapNode.get_cellv(ptValid) != 5 and mapNode.get_cellv(ptValid) != 10:
both conditions have to be checked ... not good. Better rearrange your tiles for a simple if mapNode.get_cellv(ptValid) > 5
... would be better
listPairId seems some sort of index pairs ...i would avoid using array of arrays ... if you are storing simple id's then use PoolIntArray if possible and store them right after another
This will lead to a very ugly code with lots of repetitions ... but it should be significant faster