Skip to content

Commit

Permalink
some more api comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
codescapade committed Oct 19, 2024
1 parent 9ea10e6 commit 3f45c76
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 14 deletions.
43 changes: 43 additions & 0 deletions src/jume/Jume.hx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ class Jume {
view.pixelFilter ? NEAREST : LINEAR);
}

/**
* Launch the game.
* @param sceneType The scene to start the game with.
*/
public function launch(sceneType: Class<Scene>) {
events.addListener({ type: SceneEvent.CHANGE, callback: onSceneChange });
changeScene(sceneType);
Expand All @@ -197,18 +201,28 @@ class Jume {
#end
}

/**
* Called when the game gets focus.
*/
public function hasFocus() {
inFocus = true;
FocusEvent.send(FocusEvent.HAS_FOCUS);
scene.hasFocus();
}

/**
* Called when the game loses focus.
*/
public function lostFocus() {
inFocus = false;
FocusEvent.send(FocusEvent.LOST_FOCUS);
scene.lostFocus();
}

/**
* Setup the built-in asset loaders.
* @param assets The asset manager.
*/
function addAssetLoaders(assets: Assets) {
assets.registerLoader(new AtlasLoader());
assets.registerLoader(new BitmapFontLoader());
Expand All @@ -219,6 +233,11 @@ class Jume {
assets.registerLoader(new TilesetLoader());
}

/**
* Called when the browser window size changes.
* @param width The window width in pixels.
* @param height The window height in pixels.
*/
function resize(width: Int, height: Int) {
final ratio = view.pixelRatio;
if (view.isFullScreen) {
Expand All @@ -237,6 +256,9 @@ class Jume {
scene.resize(width * ratio, height * ratio);
}

/**
* Game loop when running in headless mode.
*/
function headlessLoop() {
final now = Timer.stamp();
final passed = now - prevTime;
Expand All @@ -246,6 +268,10 @@ class Jume {
Timer.delay(headlessLoop, Std.int(1.0 / 60.0 * 1000));
}

/**
* Game loop when running in browser mode.
* @param time not used.
*/
function loop(time: Float) {
Browser.window.requestAnimationFrame(loop);

Expand All @@ -265,6 +291,10 @@ class Jume {
}
}

/**
* The update loop. Updates time and the active scene.
* @param dt The time passed since the last update in seconds.
*/
function update(dt: Float) {
if (inFocus || !pauseUnfocused) {
if (dt > MAX_DT) {
Expand All @@ -280,6 +310,9 @@ class Jume {
}
}

/**
* The render loop.
*/
function render() {
graphics.transform.identity();
graphics.pushTarget(target);
Expand All @@ -293,19 +326,29 @@ class Jume {
graphics.transform.identity();
graphics.color.copyFrom(Color.WHITE);

// Scale to make the render target fit the screen.
Mat4.fromScale(view.viewScaleX, view.viewScaleY, 1, graphics.transform);

// Render the main render target to the screen.
graphics.start();
tempPos.set(0, 0);
graphics.drawRenderTarget(tempPos, target);
graphics.present();
}

/**
* Callback for the scene change event.
* @param event The scene event.
*/
function onSceneChange(event: SceneEvent) {
changeScene(event.sceneType);
event.canceled = true;
}

/**
* Change the current scene.
* @param sceneType The new scene to start.
*/
function changeScene(sceneType: Class<Scene>) {
if (scene != null) {
scene.destroy();
Expand Down
9 changes: 9 additions & 0 deletions src/jume/events/SceneEvent.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ package jume.events;

import jume.ecs.Scene;

/**
* Scene management events.
*/
class SceneEvent extends Event {
/**
* This event type to change to a new scene.
*/
public static final CHANGE: EventType<SceneEvent> = 'jume_scene_event';

/**
* The scene type to change to.
*/
var sceneType: Class<Scene>;
}
3 changes: 3 additions & 0 deletions src/jume/graphics/Flip.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package jume.graphics;

/**
* Type that holds x and y axis flip on an image when rendering.
*/
typedef Flip = {
var x: Bool;
var y: Bool;
Expand Down
3 changes: 3 additions & 0 deletions src/jume/graphics/animation/AnimationMode.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package jume.graphics.animation;

/**
* The different animation play modes.
*/
enum abstract AnimationMode(String) {
var NORMAL = 'normal';
var LOOP = 'loop';
Expand Down
27 changes: 23 additions & 4 deletions src/jume/tilemap/TilemapColliders.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package jume.tilemap;
import jume.math.Rectangle;
import jume.math.Vec2;

/**
* Helper to keep track of which tiles have been checked when generating colliders.
*/
typedef CollisionTile = {
var id: Int;
var checked: Bool;
};

typedef GenFromIntGridProps = {
typedef GenFromIntGridParams = {
var grid: Array<Array<Int>>;
var worldX: Int;
var worldY: Int;
Expand All @@ -17,7 +20,7 @@ typedef GenFromIntGridProps = {
var collisionIds: Array<Int>;
};

typedef GenColliderProps = {
typedef GenColliderParams = {
var tiles: Array<Array<CollisionTile>>;
var worldX: Int;
var worldY: Int;
Expand All @@ -26,7 +29,12 @@ typedef GenColliderProps = {
var collisionIds: Array<Int>;
};

function generateFromIntGrid(params: GenFromIntGridProps): Array<Rectangle> {
/**
* Generate colliders from an integer 2d grid.
* @param params
* @return Array<Rectangle>
*/
function generateFromIntGrid(params: GenFromIntGridParams): Array<Rectangle> {
final tiles: Array<Array<CollisionTile>> = [];

for (y in 0...params.grid.length) {
Expand All @@ -47,6 +55,12 @@ function generateFromIntGrid(params: GenFromIntGridProps): Array<Rectangle> {
});
}

/**
* Check if a tile is part of the ids that generate colliders.
* @param id
* @param collisionIds
* @return Bool
*/
function isCollisionTile(id: Int, collisionIds: Array<Int>): Bool {
// If no ids specified every non-empty tile is a collision tile.
if (collisionIds.length == 0) {
Expand All @@ -56,7 +70,12 @@ function isCollisionTile(id: Int, collisionIds: Array<Int>): Bool {
return collisionIds.contains(id);
}

function generateColliders(params: GenColliderProps): Array<Rectangle> {
/**
* Generate the tile colliders.
* @param params
* @return Array<Rectangle>
*/
function generateColliders(params: GenColliderParams): Array<Rectangle> {
final colliders: Array<Rectangle> = [];
final start = Vec2.get();
final current = Vec2.get();
Expand Down
57 changes: 48 additions & 9 deletions src/jume/tilemap/Tileset.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,79 @@ import haxe.Exception;
import jume.graphics.Image;
import jume.math.Rectangle;

/**
* Tilesets are used to render different tiles in a tilemap.
*/
class Tileset {
/**
* The width per tile in pixels.
*/
public final tileWidth: Int;

/**
* The height per tile in pixels.
*/
public final tileHeight: Int;

/**
* The image that has all the tiles.
*/
public final image: Image;

/**
* The images rectangles for each ile.
*/
public final tiles: Array<Rectangle>;

/**
* Create a new Tileset instance.
* @param image The tileset image.
* @param tileWidth The width per tile in pixels.
* @param tileHeight The height per tile in pixels.
* @param spacing The spacing between tiles in pixels.
* @param margin The margin between the edge of the image and the tile in pixels.
*/
public function new(image: Image, tileWidth: Int, tileHeight: Int, spacing = 0, margin = 0) {
this.image = image;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
tiles = createTileRects(margin, spacing);
}

/**
* Get the image rect for a tile index.
* @param index The index in the tileset.
* @return The rectangle for the index.
*/
public function getRect(index: Int): Rectangle {
if (index < 0 || index >= tiles.length) {
throw new Exception('Tile with index ${index} is out of range');
}

return tiles[index];
}

/**
* Create the rectangles for each tile.
* @param spacing The spacing between tiles in pixels.
* @param margin The margin between the edge of the image and the tile in pixels.
* @return The created rectangles.
*/
function createTileRects(margin: Int, spacing: Int): Array<Rectangle> {
final width = image.width;
final height = image.height;
final horizontalTiles = Math.floor((width - margin * 2 + spacing) / (tileWidth + spacing));
final verticalTiles = Math.floor((height - margin * 2 + spacing) / (tileHeight + spacing));

this.tiles = [];
final tileRects: Array<Rectangle> = [];
for (y in 0...verticalTiles) {
for (x in 0...horizontalTiles) {
final xPos = margin + x * tileWidth + x * spacing;
final yPos = margin + y * tileHeight + y * spacing;
this.tiles.push(new Rectangle(xPos, yPos, tileWidth, tileHeight));
tileRects.push(new Rectangle(xPos, yPos, tileWidth, tileHeight));
}
}
}

public function getRect(index: Int): Rectangle {
if (index < 0 || index >= tiles.length) {
throw new Exception('Tile with index ${index} is out of range');
}

return tiles[index];
return tileRects;
}
}
9 changes: 8 additions & 1 deletion src/jume/utils/Macros.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import haxe.macro.Expr;

using haxe.macro.Tools;

/**
* Add the @:inject metadata for autocomplete.
*/
function init() {
Compiler.registerCustomMetadata({
metadata: ':inject',
Expand All @@ -16,6 +19,10 @@ function init() {
}, 'jume');
}

/**
* Run the @:inject macro.
* @return The updated fields.
*/
function inject(): Array<Field> {
// Get all the fields in the event class.
final fields = Context.getBuildFields();
Expand All @@ -38,7 +45,6 @@ function inject(): Array<Field> {
final classType = fType.toType().getClass();
final serviceType = Context.getType('jume.di.Service').getClass();

// // Make sure the class to inject implements `Service`.
var implementsService = false;
if (classType.interfaces != null) {
for (interfaceRef in classType.interfaces) {
Expand All @@ -48,6 +54,7 @@ function inject(): Array<Field> {
}
}

// Make sure the class to inject implements `Service`.
if (implementsService) {
final path = classType.pack.concat([classType.name]);

Expand Down

0 comments on commit 3f45c76

Please sign in to comment.