Skip to content

Commit

Permalink
Merge pull request #199 from QuantumCoderQC/tilesheet-improvements
Browse files Browse the repository at this point in the history
Tilesheet improvements
  • Loading branch information
luboslenco committed Oct 10, 2023
2 parents 21ddde4 + a23f6ac commit 8bf6b8f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
30 changes: 27 additions & 3 deletions Sources/iron/object/MeshObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class MeshObject extends Object {
public var cameraDistance: Float;
public var screenSize = 0.0;
public var frustumCulling = true;
public var tilesheet: Tilesheet = null;
public var activeTilesheet: Tilesheet = null;
public var tilesheets: Array<Tilesheet> = null;
public var skip_context: String = null; // Do not draw this context
public var force_context: String = null; // Draw only this context
static var lastPipeline: PipelineState = null;
Expand Down Expand Up @@ -79,7 +80,8 @@ class MeshObject extends Object {
particleSystems = null;
}
#end
if (tilesheet != null) tilesheet.remove();
if (activeTilesheet != null) activeTilesheet.remove();
if (tilesheets != null) for (ts in tilesheets) { ts.remove(); }
if (Scene.active != null) Scene.active.meshes.remove(this);
data.refcount--;
super.remove();
Expand Down Expand Up @@ -115,7 +117,29 @@ class MeshObject extends Object {
#end

public function setupTilesheet(sceneName: String, tilesheet_ref: String, tilesheet_action_ref: String) {
tilesheet = new Tilesheet(sceneName, tilesheet_ref, tilesheet_action_ref);
activeTilesheet = new Tilesheet(sceneName, tilesheet_ref, tilesheet_action_ref);
if(tilesheets == null) tilesheets = new Array<Tilesheet>();
tilesheets.push(activeTilesheet);
}

public function setActiveTilesheet(sceneName: String, tilesheet_ref: String, tilesheet_action_ref: String) {
var set = false;
// Check if tilesheet already created
if (tilesheets != null) {
for (ts in tilesheets) {
if (ts.raw.name == tilesheet_ref) {
activeTilesheet = ts;
activeTilesheet.play(tilesheet_action_ref);
set = true;
break;
}
}
}
// If not already created
if (!set) {
setupTilesheet(sceneName, tilesheet_ref, tilesheet_action_ref);
}

}

inline function isLodMaterial(): Bool {
Expand Down
8 changes: 4 additions & 4 deletions Sources/iron/object/ParticleSystem.hx
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class ParticleSystem {
dimx = object.transform.dim.x;
dimy = object.transform.dim.y;

if (object.tilesheet != null) {
tilesx = object.tilesheet.raw.tilesx;
tilesy = object.tilesheet.raw.tilesy;
tilesFramerate = object.tilesheet.raw.framerate;
if (object.activeTilesheet != null) {
tilesx = object.activeTilesheet.raw.tilesx;
tilesy = object.activeTilesheet.raw.tilesy;
tilesFramerate = object.activeTilesheet.raw.framerate;
}

// Animate
Expand Down
17 changes: 17 additions & 0 deletions Sources/iron/object/Tilesheet.hx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ class Tilesheet {
Scene.active.tilesheets.remove(this);
}

/**
* Set the frame of the current active tilesheet action. Automatically un-pauses action.
* @param frame Frame offset with 0 as the first frame of the active action.
**/
public function setFrameOffset(frame: Int) {
setFrame(action.start + frame);
paused = false;
}

/**
* Returns the current frame.
* @return Frame offset with 0 as the first frame of the active action.
*/
public function getFrameOffset(): Int {
return frame - action.start;
}

function update() {
if (!ready || paused || action.start >= action.end) return;

Expand Down
7 changes: 6 additions & 1 deletion Sources/iron/object/Uniforms.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1044,10 +1044,15 @@ class Uniforms {
var vy: Null<kha.FastFloat> = null;
switch (c.link) {
case "_tilesheetOffset": {
var ts = cast(object, MeshObject).tilesheet;
var ts = cast(object, MeshObject).activeTilesheet;
vx = ts.tileX;
vy = ts.tileY;
}
case "_tilesheetTiles": {
var ts = cast(object, MeshObject).activeTilesheet;
vx = ts.raw.tilesx;
vy = ts.raw.tilesy;
}
#if arm_morph_target
case "_morphScaleOffset": {
var mt = cast(object, MeshObject).morphTarget;
Expand Down

0 comments on commit 8bf6b8f

Please sign in to comment.