Basic code review

Hello. Finally, I have a basic game prototype. I’d like to know your opinion about my code style. Please point out all horrible stuff I’ve done and how I can improve, or steal things you might find interesting.
Project requires:

  • openfl 4
  • differ
  • spritesheet
  • edge - at the moment my fork is required (haxelib git edge feature/entity-get)

And the project is here: - controls are arrows, Z and X to fire. (If you are reading this in an community archive, I probably made it private again, sorry!)

Thank you, and I’m looking forward to read your tips.

I usually tell people to profile before optimizing, so that’s exactly what I did.

It looks like part of your problem is the “Differ” library. Whenever you change a shape’s position, it updates the shape’s matrix. These three lines call compose() a total of six times.

The View class iterates through map.keys(). This is extremely inefficient on hash maps, so I might suggest an Array or a BalancedTree instead.

A fair amount of time is also going into circle vs. circle collisions. You could speed these up some by inlining Shape.get_x(), Shape.get_y(), and Circle.get_transformedRadius().

On top of that, you’re calling the SAT2D.testCircleVsCircle() function for each collision test. This function not only checks for a collision, but also calculates details about that collision, like the distance and direction you’d have to move the objects before they stopped overlapping. The great thing about circle vs. circle collision testing is you can avoid taking the square root if all you need to know is “are they overlapping?” But to calculate overlap distance, you need to take the square root, and that’s slow. You’d be much better off just writing your own code. This code replaces line 30, and you’ll need to change line 8 of phys.component.BulletCollision to “var farShape:CircleShape.”

var xDifference:Float = collision.farShape.x -;
var yDifference:Float = collision.farShape.y -;
var totalRadius:Float = collision.farShape.transformedRadius +;
if(xDifference * xDifference + yDifference * yDifference <= totalRadius * totalRadius) {
    //The inner code stays the same.

By far your biggest time sink is garbage collection, which means you’re creating lots and lots of objects. Unfortunately, I wasn’t able to get memory allocations in Flash (even after trying multiple different debug players), and I couldn’t get telemetry to work on Windows.

Maybe it’s all of the NativePropertyIterator object that gets created each time you iterate through a View. Maybe it’s the Vector objects that get created by the Differ library’s functions. Maybe it’s something else.

Try profiling this yourself. If you can get memory data, you won’t have to guess.


Thank you @player_03 ! I found the root cause of my problem. The same applies to using differ without second thought - I was under impression that circle collision will be some inlined distance check. I learned my lesson (for a while at least). With disabled hardware acceleration hxscout stuttered at 0.5FPS when profiling CPP (I think shitload of telemetry calls makes a difference,). Still, I’ll be migrating toward ECX from edge, as it’s under active development and already is much less memoryhungry than edge.

Thank you very much for insight!

You’re welcome. Glad you figured it out!