📜 ⬆️ ⬇️

just2d - create a “perfect” game engine. Step 1

Good morning, Habr.
At leisure, I was inspired to develop a new 2D game engine. However, with one unique feature. I want to make it really convenient for developers of different levels.
How to achieve this?
First, we will invent and write 2-3 typical games on the “ideal” engine, i.e. first the application itself will be created, and only then the engine will be written under its code.



The first game will be a platformer, which received the test name Terrorist Win.
Description:
run around the location, laying and exploding bombs to kill mobs.

')
The second game will most likely match3


I look forward to your ideal code examples.

Let's start the analysis of the first version of the "ideal" code for the platform)

start.js:
window.onload = init function init(){ //      Engine.setResurs({ 'block' : 'resurs/sprite/block.jpg', 'bomb' : 'resurs/sprite/bombs.png', 'cop' : 'resurs/sprite/cops.png', 'mob' : 'resurs/sprite/mobs.png', 'player' : 'resurs/sprite/player.png', 'mask' : 'resurs/sprite/mask.png', '_palm' : 'resurs/sprite/_palm.png', '_lavk' : 'resurs/sprite/_lavk.png', 'fire' : 'resurs/sprite/fires.png' }) /*     width -  () canvas height -  () canvas separator - id html  canvas size -    (        zoom) */ Engine.init({ width : 800, height : 512, separator : 'game_canvas', size : 32, }) /*   ""     (1-0 :   0 - , 1 - ) */ var map = new Array( new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new Array(1,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1), new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1) ) /*  : Engine.Task.AddScene('map') -     map     // GameObject -    ,    //  GameObject  box,   Vec2    1 var block = new GameObject('box', new Vec2(x, y), 1, 1) //   block.gravitati = false; //    ,    scene.ObjectAdd(false, block) */ var scene = new SceneControl('scene1', map[0].length, map.length) for(var y = 0; y < map.length; y++){ for(var x = 0; x < map[0].length; x++){ var sprite = new SpriteControl('block', new Vec2(x, y), Setting.set('size'), Setting.set('size') var block = new GameObject('box', new Vec2(x, y), 1, 1) block.sprite = sprite; block.gravitati = false; scene.ObjectAdd(false, block) } } /*     Player   Vec2     */ var player = new PlayerObject(new Vec2(5, 5)) scene.ObjectAdd('player', player) Engine.Task.AddScene('scene1', scene) /*    scene1*/ Engine.Task.PlayScene('scene1') //   Engine.Play(); } 


Well, the player.js code
 function PlayerObject(vec2){ // parrent -   this.parrent = new GameObject('box', vec2, 1, 1 ) //  events  parrent this.eventKeyUp = function(){ //   this.parrent.vec2 = this.parrent.vec2.summ(new Vec2(-2, 0)) } this.eventKeyLeft = function(){ //  this.parrent.vec2 = this.parrent.vec2.summ(new Vec2(0, -1)) } this.eventKeyRight = function(){ //  this.parrent.vec2 = this.parrent.vec2.summ(new Vec2(0, 1)) } this.eventKeyE = function(){ //  E //     var scene = Engine.Task.returnScene('map') // Bomb -          var bomb = new Bomb(new Vec2(this.parrent.vec2.x, this.parrent.vec2.y)) scene.ObjectAdd(false, bomb) } this.eventKeySpace = function(){ //  // bomb Detonation } this.collision = function(){ //   //    "" if(this.collisionFlagBottom){ //     this.parrent.vec2.y = 0 //    } } } 


I would also like to see examples for your “ideal” games, how would you feel comfortable writing

in fact, this is the first version of the pseudo-ideal code)
I look forward to your comments, notes and suggestions.

On trail ok:

project code on github.com/DrNemo/just2d
until a normal code template is developed, I will not start programming real code, i.e. You can not pay attention to the sketches from the folder just2d.v.0.0.1)
the idea as well as the prototype came to mind last night, do not hit hard)

UP1:
I plan to go from simple to complex, to increase functionality and capabilities from version to version

declared minimum:
Work with scenes - not falling into the window is cut off, you can set both your sizes and shifts

Scene objects are child elements of the scene, these are game objects themselves, can be arbitrary based on parrent
event handler reassignment supported

developments:
functions event handlers
eventClick, eventDblClick, eventHover, eventKeyX - clear
eventCollision - collision with an object of the same mask

collisionFlagBottom, collisionFlagLeft - etc. collision flags on the sides

simple physics
gravitati = new Vec2 (1, 0) or false if disabled

if you do not reassign events, then the default events are triggered.

same properties
enable, visible

UP2:
In order not to litter the topic I will continue in my blog style4web.ru/category/just2d

Source: https://habr.com/ru/post/163581/


All Articles