Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FlxG.center() and camera.center() #3329

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

richTrash21
Copy link
Contributor

Closes #3309

Another attempt at #3310 based on Geokureli's suggestion

Adds FlxG.center() and camera.center() for centering sprites by their graphic size and FlxG.centerHitbox() and camera.centerHitbox() for centering objects by their hitbox size.

@NeeEoo
Copy link
Contributor

NeeEoo commented Jan 3, 2025

I'm not really sure if reflection in c++ works when using generics.

Because both FlxG.random.shuffle and FlxG.random.getObject both don't exist in reflection due to generics

@richTrash21
Copy link
Contributor Author

I'm not really sure if reflection in c++ works when using generics.

Because both FlxG.random.shuffle and FlxG.random.getObject both don't exist in reflection due to generics

As far as i know the reason why FlxG.random.shuffle and FlxG.random.getObject doesn't exist in reflection is due to @:generic metadata.

I've also made a small test to confirm that:

class Main {
	static function main() {
		var stringField:String;
		var intField:Int;
		var floatField:Float;

		stringField = test1("some string");
		intField = test1(30);
		floatField = test1(1.0034);

		stringField = test2("some string");
		intField = test2(30);
		floatField = test2(1.0034);

		trace(Reflect.getProperty(Main, "test1")); // returns test1 function
		trace(Reflect.getProperty(Main, "test2")); // returns null
	}

	static function test1<T>(v:T):T {
		trace("test generic func #1 (" + Std.string(v) + ")");
		return v;
	}

	@:generic
	static function test2<T>(v:T):T {
		trace("test generic func #2 (" + Std.string(v) + ")");
		return v;
	}
}

@NeeEoo
Copy link
Contributor

NeeEoo commented Jan 3, 2025

Damn, i wonder why haxe wouldn't generate a base one for reflection purposes

Copy link
Member

@Geokureli Geokureli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the actual graphic width of a sprite depends on sprite.getGraphicBounds and sprite.getGraphicMidpoint(), which is the world coordinates of the graphic, and accounts for origin, angle, offset, pixelPerfectPosition and any future features or features in extending classes.

Also note: there is sprite.getScreenBounds, which accounts for scrollFactor and camera.zoom

We should use those (and be sure to put any rect/point back into the pool), or if you think it's beneficial we can add a sprite.getScreenMidpoint()

I thought about maybe having getGraphicWidth, getScreenWidth and the like, and having the existing methods use that, but truthfully, it might be a hassle, and even a little less performant. I'm leaning against that, but let me know what you think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

remove screenCenter
3 participants