diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 94d74c5..e091459 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -326,7 +326,7 @@ void gameobject_draw_debug(int go) { .g = 255, .a = 255 }; - draw_cppoint(pos, 3.f, color); + cpBodyEachShape(g->body, body_draw_shapes_dbg, NULL); } diff --git a/source/engine/input.c b/source/engine/input.c index 7cf1e1c..ff45bf9 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -329,7 +329,7 @@ const char *keyname_extd(int key) { return "minus"; break; case SAPP_KEYCODE_GRAVE_ACCENT: - return "backtick"; + return "`"; break; case SAPP_KEYCODE_LEFT_BRACKET: diff --git a/source/engine/render.c b/source/engine/render.c index 03843ea..c30c5f1 100644 --- a/source/engine/render.c +++ b/source/engine/render.c @@ -415,7 +415,6 @@ void full_2d_pass(struct window *window) hudproj = HMM_Orthographic_RH_NO(0, window->width, 0, window->height, -1.f, 1.f); sprite_draw_all(); - sprite_flush(); call_draw(); //// DEBUG @@ -436,6 +435,7 @@ void full_2d_pass(struct window *window) call_gui(); debug_flush(&hudproj); text_flush(&hudproj); + sprite_flush(); #ifndef NO_EDITOR call_nk_gui(); diff --git a/source/engine/sprite.c b/source/engine/sprite.c index a28d6ed..d865b2f 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -113,6 +113,10 @@ void sprite_io(struct sprite *sprite, FILE *f, int read) { void sprite_draw_all() { sg_apply_pipeline(pip_sprite); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection)); + for (int i = 0; i < arrlen(sprites); i++) + sprite_draw(&sprites[i]); + + return; static struct sprite **layers[5]; diff --git a/source/scripts/components.js b/source/scripts/components.js index 0017572..b320381 100644 --- a/source/scripts/components.js +++ b/source/scripts/components.js @@ -30,7 +30,7 @@ var sprite = clone(component, { pos: [0,0], get visible() { return this.enabled; }, set visible(x) { this.enabled = x; }, - set asset(str) { this.path = str; Log.warn(`SET ${str} ON THE SPRITE`); this.sync();}, + set asset(str) { this.path = str; this.sync();}, angle: 0, rect: {s0:0, s1: 1, t0: 0, t1: 1}, @@ -48,7 +48,6 @@ var sprite = clone(component, { get pos() { return cmd(111, this.id); }, set pos(x) { cmd(37,this.id,x); }, set layer(x) { cmd(60, this.id, x); }, - get layer() { return this.gameobject.draw_layer; }, get boundingbox() { var dim = this.dimensions; @@ -67,22 +66,25 @@ var sprite = clone(component, { kill() { cmd(9,this.id); }, }); sprite.obscure('boundingbox'); + sprite.layer = 1; return sprite; }, - input_kp9_pressed() { this.pos = [0,0]; }, - input_kp8_pressed() { this.pos = [-0.5, 0]; }, - input_kp7_pressed() { this.pos = [-1,0]; }, - input_kp6_pressed() { this.pos = [0,-0.5]; }, - input_kp5_pressed() { this.pos = [-0.5,-0.5]; }, - input_kp4_pressed() { this.pos = [-1,-0.5]; }, - input_kp3_pressed() { this.pos = [0, -1]; }, - input_kp2_pressed() { this.pos = [-0.5,-1]; }, - input_kp1_pressed() { this.pos = [-1,-1]; }, - POS_MID: [-0.5, -0.5], }); +sprite.inputs = {}; +sprite.inputs.kp9 = function() { this.pos = [0,0]; }; +sprite.inputs.kp8 = function() { this.pos = [-0.5, 0]; }; +sprite.inputs.kp7 = function() { this.pos = [-1,0]; }; +sprite.inputs.kp6 = function() { this.pos = [0,-0.5]; }; +sprite.inputs.kp5 = function() { this.pos = [-0.5,-0.5]; }; +sprite.inputs.kp4 = function() { this.pos = [-1,-0.5]; }; +sprite.inputs.kp3 = function() { this.pos = [0, -1]; }; +sprite.inputs.kp2 = function() { this.pos = [-0.5,-1]; }; +sprite.inputs.kp1 = function() { this.pos = [-1,-1]; }; + + /* Container to play sprites and anim2ds */ var char2d = clone(sprite, { clone(anims) { @@ -335,6 +337,7 @@ var polygon2d = clone(collider2d, { }); polygon2d.inputs = {}; +polygon2d.inputs.post = function() { this.sync(); }; polygon2d.inputs.f10 = function() { this.points = sortpointsccw(this.points); }; @@ -346,7 +349,7 @@ polygon2d.inputs['C-lm'] = function() { polygon2d.inputs['C-lm'].doc = "Add a point to location of mouse."; polygon2d.inputs['S-lm'] = function() { - var idx = grab_from_points(screen2world(Mouse.pos), this.points.map(this.gameobject.this2world,this.gameobject), 25); + var idx = grab_from_points(Mouse.worldpos, this.points.map(p => this.gameobject.this2world(p)), 25); if (idx === -1) return; this.points.splice(idx, 1); }; diff --git a/source/scripts/editor.js b/source/scripts/editor.js index f820ac6..31d6896 100644 --- a/source/scripts/editor.js +++ b/source/scripts/editor.js @@ -4,10 +4,6 @@ */ prototypes.generate_ur('.'); -/* This is the editor level & camera - NOT the currently edited level, but a level to hold editor things */ -var editor_level = Primum.spawn(ur.arena); -var editor_camera = Game.camera; - var editor_config = { grid_size: 100, grid_color: [99, 255, 128, 100], @@ -28,8 +24,8 @@ var editor = { moveoffset: [0,0], startrot: 0, rotoffset: 0, - camera: editor_camera, - edit_level: {}, /* The current level that is being edited */ + camera: undefined, + edit_level: undefined, /* The current level that is being edited */ working_layer: 0, cursor: undefined, edit_mode: "basic", @@ -535,7 +531,6 @@ var editor = { } GUI.text("0,0", world2screen([0,0])); - Debug.point([0,0],3); var clvl = this.edit_level; var ypos = 200; @@ -597,12 +592,12 @@ var editor = { } } - Game.objects.forEach(function(obj) { + editor.edit_level.objects.forEach(function(obj) { if (!obj.selectable) - gui_img("icons/icons8-lock-16.png", world2screen(obj.pos)); + GUI.image("icons/icons8-lock-16.png", world2screen(obj.pos)); }); - Debug.draw_grid(1, editor_config.grid_size/editor_camera.zoom, editor_config.grid_color); + Debug.draw_grid(1, editor_config.grid_size/editor.camera.zoom, editor_config.grid_color); var startgrid = screen2world([-20,Window.height]).map(function(x) { return Math.snap(x, editor_config.grid_size); }, this); var endgrid = screen2world([Window.width, 0]); @@ -650,11 +645,12 @@ var editor = { var log = cmd(84); var f = log.prev('\n', 0, 10); Nuke.scrolltext(log.slice(f)); - + this.replstr = Nuke.textbox(this.replstr); Nuke.end(); } - }, + + replstr: "", ed_debug() { if (!Debug.phys_drawing) @@ -687,11 +683,12 @@ var editor = { lvl_history: [], load(file) { - if (this.edit_level) this.lvl_history.push(this.edit_level.ur); +// if (this.edit_level) this.lvl_history.push(this.edit_level.ur); // this.edit_level.kill(); // this.edit_level = Level.loadfile(file); // this.curlvl = this.edit_level.save(); - Primum.spawn(prototypes.get_ur(file)); +// Primum.spawn(prototypes.get_ur(file)); + editor.edit_level.spawn(prototypes.get_ur(file)); this.unselect(); }, @@ -2035,7 +2032,7 @@ limited_editor.inputs['C-q'] = function() // World.kill(); World.clear_all(); editor.load_json(editor.stash); - Game.view_camera(editor_camera); + Game.view_camera(editor.camera); } /* This is used for editing during a paused game */ @@ -2049,6 +2046,10 @@ Debug.register_call(editor.ed_debug, editor); if (IO.exists("editor.config")) load_configs("editor.config"); -editor.edit_level = editor_level; +/* This is the editor level & camera - NOT the currently edited level, but a level to hold editor things */ +editor.edit_level = Primum.spawn(ur.arena); +editor.edit_level.selectable = false; +editor.camera = Game.camera; + Game.stop(); diff --git a/source/scripts/engine.js b/source/scripts/engine.js index 5851521..96adc04 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -39,6 +39,7 @@ Log.level = 1; var Color = { white: [255,255,255,255], + black: [0,0,0,255], blue: [84,110,255,255], green: [120,255,10,255], yellow: [251,255,43,255], @@ -552,27 +553,13 @@ load("scripts/entity.js"); var World = Object.create(gameobject); var Primum = World; +gameobject.make_parentable(Primum); Primum.tag = "PRIMUM"; Primum.selectable = false; Primum.ur = { tag: "Primum" }; -Primum.objects = []; - -World.remove_child = function(child) { - this.objects.remove(child); -} - -World.add_child = function(child) { - child.unparent(); - objects.push(child); - child.level = World; -} - /* Reparent this object to a new one */ -World.reparent = function(parent) { } - -World.unparent = function() { } - -World.name = "World"; +World.reparent = function(parent) { Log.warn("Cannot reparent the Primum."); } +World.unparent = function() { Log.warn("The Primum has no parent, always."); } World.fullpath = function() { return World.name; }; /* Load configs */ diff --git a/source/scripts/entity.js b/source/scripts/entity.js index 448dada..63e20ea 100644 --- a/source/scripts/entity.js +++ b/source/scripts/entity.js @@ -18,6 +18,7 @@ var gameobject = { spawn(ur) { if (typeof ur === 'string') ur = prototypes.get_ur(ur); + return ur.type.make(this); }, @@ -328,21 +329,11 @@ var gameobject = { var obj = Object.create(this); this.instances.push(obj); obj.toString = function() { - var props = obj.prop_obj(); - for (var key in props) - if (typeof props[key] === 'object' && !props[key] === null && props[key].empty) - delete props[key]; - - var edited = !props.empty; -// return (edited ? "#" : "") + obj.name + " object " + obj.body + ", layer " + obj.draw_layer + ", phys " + obj.layer; - return obj.ur.tag; - }; + if (obj.ur) + return obj.ur.tag; + + return "NO UR"}; - obj.fullpath = function() { - return obj.ur.tag; - }; - obj.deflock('toString'); - obj.defc('from', this.name); obj.defn('body', make_gameobject(this.scale, this.phys, this.mass, @@ -350,34 +341,9 @@ var gameobject = { this.elasticity) ); obj.sync(); obj.defn('components', {}); + Game.register_obj(obj); - var objects = []; - obj.objects = objects; - - obj.remove_child = function(child) { - objects.remove(child); - } - - obj.add_child = function(child) { - child.unparent(); - objects.push(child); - child.level = obj; - } - - /* Reparent this object to a new one */ - obj.reparent = function(parent) { - if (parent === obj.level) - return; - - parent.add_child(obj); - } - - obj.unparent = function() { - if (!obj.level) return; - obj.level.remove_child(obj); - } - cmd(113, obj.body, obj); /* Now that it's concrete in the engine, these functions update to return engine data */ @@ -394,7 +360,7 @@ var gameobject = { set pos(x) { var diff = x.sub(this.pos); - objects.forEach(function(x) { x.pos = x.pos.add(diff); }); + this.objects.forEach(function(x) { x.pos = x.pos.add(diff); }); set_body(2,obj.body,x); }, get pos() { return q_body(1,obj.body); }, @@ -413,7 +379,6 @@ var gameobject = { for (var prop in obj) { if (typeof obj[prop] === 'object' && 'make' in obj[prop]) { - if (prop === 'flipper') return; obj[prop] = obj[prop].make(obj.body); obj[prop].defn('gameobject', obj); obj.components[prop] = obj[prop]; @@ -421,6 +386,8 @@ var gameobject = { }; obj.check_registers(obj); + + gameobject.make_parentable(obj); /* Spawn subobjects defined */ if (obj.$) { @@ -430,6 +397,8 @@ var gameobject = { if (typeof obj.start === 'function') obj.start(); + level.add_child(obj); + return obj; }, @@ -448,6 +417,33 @@ var gameobject = { }, } +gameobject.make_parentable = function(obj) { + var objects = []; + + obj.remove_child = function(child) { + objects.remove(child); + } + + obj.add_child = function(child) { + child.unparent(); + objects.push(child); + } + + /* Reparent this object to a new one */ + obj.reparent = function(parent) { + if (parent === obj.level) + return; + + parent.add_child(obj); + } + + obj.unparent = function() { + if (!obj.level) return; + obj.level.remove_child(obj); + } + obj.objects = objects; +} + var locks = ['height', 'width', 'visible', 'body', 'controlled', 'selectable', 'save', 'velocity', 'angularvelocity', 'alive', 'boundingbox', 'name', 'scale', 'angle', 'properties', 'moi', 'relpos', 'relangle', 'up', 'down', 'right', 'left', 'bodytype', 'gizmo', 'pos']; locks.forEach(x => gameobject.obscure(x)); @@ -529,6 +525,7 @@ prototypes.from_obj = function(name, obj) tag: name, type: newobj }; + newobj.ur = prototypes.ur[name]; return prototypes.ur[name]; } diff --git a/source/scripts/gui.js b/source/scripts/gui.js index 48199ba..00d92c2 100644 --- a/source/scripts/gui.js +++ b/source/scripts/gui.js @@ -17,8 +17,8 @@ var GUI = { }, image(path,pos) { - let wh = cmd(64,path); - gui_img(path,pos.slice().sub(wh), 1.0, 0.0); + var wh = cmd(64,path); + gui_img(path,pos, [1.0,1.0], 0.0, 0.0, [0.0,0.0], 0.0, Color.black); return cwh2bb([0,0], wh); }, diff --git a/source/scripts/input.js b/source/scripts/input.js index 1c5dfa5..2bcf50d 100644 --- a/source/scripts/input.js +++ b/source/scripts/input.js @@ -112,6 +112,7 @@ var Player = { for (var pawn of this.pawns.reverse()) { if (typeof pawn.inputs?.mouse?.[type] === 'function') { pawn.inputs.mouse[type].call(pawn,...args); + pawn.inputs.post?.call(pawn); return; } } @@ -141,8 +142,10 @@ var Player = { fn = pawn.inputs[cmd].down; } - if (typeof fn === 'function') + if (typeof fn === 'function') { fn.call(pawn, ... args); + pawn.inputs.post?.call(pawn); + } } },