From a5f15a86f392e269e9e646c1bfb3bfbfee3a00d9 Mon Sep 17 00:00:00 2001 From: Vortex <73261680+Vortex2Oblivion@users.noreply.github.com> Date: Sun, 22 Dec 2024 09:16:03 -0500 Subject: [PATCH] unload old texture when calling loadGraphic --- src/flixel/FlxObject.hx | 71 +++++++++++++++++++++++++++++++--- src/flixel/FlxSprite.hx | 4 ++ src/flixel/math/FlxPoint.hx | 5 +++ src/flixel/math/FlxVelocity.hx | 26 +++++++++++++ src/raylib/Vector2.hx | 2 +- src/raylib/Vector3.hx | 2 +- src/raylib/Vector4.hx | 2 +- test/src/PlayState.hx | 3 +- 8 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/flixel/math/FlxPoint.hx create mode 100644 src/flixel/math/FlxVelocity.hx diff --git a/src/flixel/FlxObject.hx b/src/flixel/FlxObject.hx index 2e9cbe6..3ae94f4 100644 --- a/src/flixel/FlxObject.hx +++ b/src/flixel/FlxObject.hx @@ -1,5 +1,7 @@ package flixel; +import flixel.math.FlxVelocity; +import flixel.math.FlxPoint; import flixel.util.FlxAxes; class FlxObject extends FlxBasic { @@ -13,6 +15,24 @@ class FlxObject extends FlxBasic { public var angle:Float = 0; + public var velocity(default, null):FlxPoint; + + public var maxVelocity(default, null):FlxPoint; + + public var scrollFactor(default, null):FlxPoint; + + public var acceleration(default, null):FlxPoint; + + public var drag(default, null):FlxPoint; + + public var angularVelocity:Float = 0; + + public var angularAcceleration:Float = 0; + + public var angularDrag:Float = 0; + + public var maxAngular:Float = 10000; + public function new(x:Float = 0, y:Float, width:Float = 0, height:Float = 0) { super(); @@ -20,6 +40,22 @@ class FlxObject extends FlxBasic { this.y = y; this.width = width; this.height = height; + + initVars(); + } + + @:noCompletion + inline function initVars() { + scrollFactor = FlxPoint.create(1, 1); + initMotionVars(); + } + + @:noCompletion + inline function initMotionVars() { + velocity = FlxPoint.create(); + acceleration = FlxPoint.create(); + drag = FlxPoint.create(); + maxVelocity = FlxPoint.create(10000, 10000); } public function screenCenter(axes:FlxAxes = XY):FlxObject { @@ -32,14 +68,29 @@ class FlxObject extends FlxBasic { return this; } - @:noCompletion - function get_width():Float { - return width; + override public function update(elapsed:Float) { + updateMotion(elapsed); } @:noCompletion - function get_height():Float { - return height; + function updateMotion(elapsed:Float):Void { + var velocityDelta:Float = 0.5 * (FlxVelocity.computeVelocity(angularVelocity, angularAcceleration, angularDrag, maxAngular, elapsed) + - angularVelocity); + angularVelocity += velocityDelta; + angle += angularVelocity * elapsed; + angularVelocity += velocityDelta; + + velocityDelta = 0.5 * (FlxVelocity.computeVelocity(velocity.x, acceleration.x, drag.x, maxVelocity.x, elapsed) - velocity.x); + velocity.x += velocityDelta; + var delta:Float = velocity.x * elapsed; + velocity.x += velocityDelta; + x += delta; + + velocityDelta = 0.5 * (FlxVelocity.computeVelocity(velocity.y, acceleration.y, drag.y, maxVelocity.y, elapsed) - velocity.y); + velocity.y += velocityDelta; + delta = velocity.y * elapsed; + velocity.y += velocityDelta; + y += delta; } @:noCompletion @@ -51,4 +102,14 @@ class FlxObject extends FlxBasic { function set_height(value:Float):Float { return height = value; } + + @:noCompletion + function get_height():Float { + return height; + } + + @:noCompletion + function get_width():Float { + return width; + } } diff --git a/src/flixel/FlxSprite.hx b/src/flixel/FlxSprite.hx index 9180dbd..45c8b10 100644 --- a/src/flixel/FlxSprite.hx +++ b/src/flixel/FlxSprite.hx @@ -29,11 +29,13 @@ class FlxSprite extends FlxObject { } public inline overload extern function loadGraphic(graphic:String):FlxSprite { + unloadTexture(texture); texture = loadTexture(graphic); return this; } public inline overload extern function loadGraphic(image:Image):FlxSprite { + unloadTexture(texture); texture = loadTextureFromImage(image); return this; } @@ -65,11 +67,13 @@ class FlxSprite extends FlxObject { return texture; } + @:noCompletion override function set_width(value:Float) { texture.width = Std.int(value); return super.set_width(value); } + @:noCompletion override function set_height(value:Float) { texture.height = Std.int(value); return super.set_height(value); diff --git a/src/flixel/math/FlxPoint.hx b/src/flixel/math/FlxPoint.hx new file mode 100644 index 0000000..a926bfe --- /dev/null +++ b/src/flixel/math/FlxPoint.hx @@ -0,0 +1,5 @@ +package flixel.math; + +import raylib.Vector2; + +typedef FlxPoint = Vector2; \ No newline at end of file diff --git a/src/flixel/math/FlxVelocity.hx b/src/flixel/math/FlxVelocity.hx new file mode 100644 index 0000000..a9f8cd5 --- /dev/null +++ b/src/flixel/math/FlxVelocity.hx @@ -0,0 +1,26 @@ +package flixel.math; + +class FlxVelocity { + public static function computeVelocity(velocity:Float, acceleration:Float, drag:Float, max:Float, elapsed:Float):Float { + if (acceleration != 0) { + velocity += acceleration * elapsed; + } else if (drag != 0) { + var _drag:Float = drag * elapsed; + if (velocity - _drag > 0) { + velocity -= _drag; + } else if (velocity + _drag < 0) { + velocity += _drag; + } else { + velocity = 0; + } + } + if ((velocity != 0) && (max != 0)) { + if (velocity > max) { + velocity = max; + } else if (velocity < -max) { + velocity = -max; + } + } + return velocity; + } +} diff --git a/src/raylib/Vector2.hx b/src/raylib/Vector2.hx index 9a0c7c5..e56a7c0 100644 --- a/src/raylib/Vector2.hx +++ b/src/raylib/Vector2.hx @@ -6,7 +6,7 @@ extern class Vector2 { public var y:Float; - public static inline function create(x:Float, y:Float):Vector2 { + public static inline function create(x:Float = 0, y:Float = 0):Vector2 { return untyped __cpp__("Vector2{(float){0}, (float){1}}", x, y); } } diff --git a/src/raylib/Vector3.hx b/src/raylib/Vector3.hx index 8245b07..d676cc0 100644 --- a/src/raylib/Vector3.hx +++ b/src/raylib/Vector3.hx @@ -8,7 +8,7 @@ extern class Vector3 { public var z:Float; - public static inline function create(x:Float, y:Float, z:Float):Vector3 { + public static inline function create(x:Float = 0, y:Float = 0, z:Float = 0):Vector3 { return untyped __cpp__("Vector3{(float){0}, (float){1}, (float){2}}", x, y, z); } } diff --git a/src/raylib/Vector4.hx b/src/raylib/Vector4.hx index 3d1e4b0..b4b2e47 100644 --- a/src/raylib/Vector4.hx +++ b/src/raylib/Vector4.hx @@ -10,7 +10,7 @@ extern class Vector4 { public var w:Float; - public static inline function create(x:Float, y:Float, z:Float, w:Float):Vector4 { + public static inline function create(x:Float = 0, y:Float = 0, z:Float = 0, w:Float = 0):Vector4 { return untyped __cpp__("Vector4{(float){0}, (float){1}, (float){2}, (float){3}}", x, y, z, w); } } diff --git a/test/src/PlayState.hx b/test/src/PlayState.hx index 36f51d3..457c517 100644 --- a/test/src/PlayState.hx +++ b/test/src/PlayState.hx @@ -16,6 +16,7 @@ class PlayState extends FlxState { skateboard = new FlxSprite(); skateboard.loadGraphic("images/skateboard.png"); + skateboard.velocity.x = 15; add(skateboard); scythe = new FlxSprite(); @@ -31,7 +32,7 @@ class PlayState extends FlxState { scythe.x = RayMath.lerp(scythe.x, Raylib.getMousePosition().x, elapsed * 10); scythe.y = RayMath.lerp(scythe.y, Raylib.getMousePosition().y, elapsed * 10); - + if (Raylib.isKeyPressed(KEY_SPACE)) { FlxG.switchState(new OtherState()); }