IWE HTML5

Jednoduchý engine pro práci s izometrickým prostředím.
Vypracováno v rámci SOČ 2014.

View project onGitHub

Anotace (English version)

Tato práce se věnuje problematice vytvoření JavaScriptového enginu pro izometrické zobrazení. V první části práce je stručně objasněn princip izometrické projekce a jsou zde popsány technologie, pomocí kterých je možné tohoto cíle dosáhnout. Další část je věnována popisu vývoje enginu a ukázce jeho využití v praxi na příkladu známé hry Snake. Do příloh je umístěna podrobná dokumentace všech metod. Přínosem by tato práce měla být především pro začínající vývojáře her, kteří se chtějí seznámit s problematikou izometrie.

Praktické ukázky

Následují tři ejdnoduché ukázky, které jsou seřazeny podle postupu vývoje (dlaždicové) hry. Postupem času budou ukázky přibývat, pokud sami využíváte tento engin a máte něco zajímavého, nebo by se vám nějaká ukázka líbila, ale zatím tu není, tak určitě napište (email, issue na github, atp..).

MapBuilder

Základem každé hry je postředí, do kterého je později zasazen děj a postavy. Tato ukázka demonstruje jednoduchý mapBuilder.

Kolize

Ve chvíli, kdy již máme připravené prostředí, je další nezbytnou funkcí pro vytváření herní logiky schopnost detekovat kolize s určitými objekty. Tato ukázka demonstruje, jak je toho možné dosáhnout za pomoci tohoto enginu.

Snake

Už jen schopnost vytvořit prostředí a detekovat kolize nám otevírá spoustu možností pro tvorbu složitější herní logiky. Toto reprezentuje následující ukázka:

Documentation

iwe Type: object
Description:

This is the main object which wraps whole content of this engine. Every other methods, which I'll be talking about here are parameters of this object which means that they're accessible by dot notation. For example: iwe.INIT();

Note:

In the next stable version (v2.0) this structure will be changed and this object will be replaced by construction function. It will allows you to create more than one instance on one page. If you want to use it right now, you can checkout the HEAD commit of branch oop-refactoring

INIT() Type: method
IN: {canvas: "string", tile: {width: int, height: int}, startAt:{x: int, y: int}, [fullScreen: boolean]}
OUT: ---
Description:

This is the initialization method which you will use on the beginning of every file. The default value for the fullScreen is false.

Example:
   iwe.INIT({
     canvas: 'main',
     tile: {width: 52, height: 30},
     startAt: {x: 250, y: 15},
     fullScreen: true
    });
Canvas Type: object
Description:

Based on what you insert to the canvas parameter inside INIT({canvas: "(canvas-id)"}). This object represents the original canvas element form DOM. It's basically the same as when you write document.getElementById("canvas-id"). That means you can use it the same way and also change the canvas parameters.

Sample:
iwe.Canvas.height = 500;
iwe.Canvas.width = 1000;
iwe.Canvas.style = 'border: 2px solid red; cursor: pointer';
//and so on ...
Map Type: object
Description:

This object includes another object offset with parameters x (int) and y (int). When you change one this parameters the map will be shifted in the next redraw iteration.

Sample:
function mapShift(e){
    isDraged = true;
    var coor = getMouseCoor(e);
    iwe.Map.offset.x -= mouse_x - coor.x;
    iwe.Map.offset.y -= mouse_y - coor.y;
    mouse_x = coor.x;
    mouse_y = coor.y;
    redraw();
}
Note:

I know that it's a bit weird and i'm going to change it.

Model Type: array
Description:

Using the iwe.fillModel() method will be this array filled with instances of object whose constructors you will pas in tile parameter of the iwe.fillModel() method.

fillModel() Type: method
IN: [[number of construction function], [...]], {t[number of construction function], t[...]}
Description:

On the input it expects 2D array, which contains the number referring to the construction function as the first parameter. On the secound place there will be an array

Sample:
function mapShift(e){
    isDraged = true;
    var coor = getMouseCoor(e);
    iwe.Map.offset.x -= mouse_x - coor.x;
    iwe.Map.offset.y -= mouse_y - coor.y;
    mouse_x = coor.x;
    mouse_y = coor.y;
    redraw();
}
tile Type: object
Description:

This object has two parameters x (int) and y (int). These parameters influenced the isometric transformation The value of these parameters you can set inside of iwe.INIT() or directly iwe.tile.x = (int).

Note:

I strongly advise you to do it inside of iwe.INIT() - it's much clearer.

getTile() Type: method
IN: x (int), y (int)
OUT: returns instance of the tile on this coordinates (from iwe.Model)
Description:

This method is really useful, for example when you have to get the tile which was clicked by the user.

Sample:
function canvasClick(e){
	var mouseCoor = getMouseCoor(e);
	var coor = iwe.TRANSFORM.toModel(mouseCoor.x, mouseCoor.y);
	var tile = iwe.getTile(coor.x, coor.y);
	tile.offsetY += 10;
	redraw();
}
isVisible() Type: method
IN: x (int), y (int)
OUT: boolean
Description:

On the input it expects coordinates of some existing object. Based on iwe.Map.offset it will decide if this object is visible or not and returns the appropriate value.

Note:

It's used for example inside of the engine in iwe.DRAW.image() to make the canvas draw only visible sprites (objects).

TRANSFORM Type: object
Description:

This object is used just as logical cover for iwe.TRANSFORM.toIso() and iwe.TRANSFORM.toModel().

TRANSFORM.toIso() Type: method
IN: x (int), y (int)
OUT: {x: (int), y: (int)}
Description:

On the input it expects coordinates from 2D model, which are transformed to isometric coordinates and are returned as parameters x and y of the output object

TRANSFORM.toModel() Type: method
IN: x (int), y (int)
OUT: {x: (int), y: (int)}
Description:

On the input it expects coordinates from isometric environment, which are transformed to 2D coordinates and are returned as parameters x and y of the output object

DRAW Type: object
Description:

This object is used just as logical cover for iwe.DRAW.image(), iwe.DRAW.tile() and iwe.DRAW.all().

DRAW.image() Type: method
IN: instance of Image(), x (int), y (int)
OUT: boolean
Description:

On the first place of the input it expects instance of class Image(), on the other two are 2D coordinates.
It is basically the same like Canvas.ctx.drawImage(url, x, y) with that difference, that before the object is placed to the canvas it is tested with method iwe.isVisible() if it is in the visible ara of canvas.

DRAW.tile() Type: method
IN: instance of tile
Description:

As the one and only input it expects instance of some tile (for example form iwe.Model[]).
This method loads the coordinates of the tile, using the iwe.TRANSFORM.toIso() it transform them in to isometric and call iwe.DRAW.image() to draw the tile.

DRAW.all() Type: method
IN: nothing
OUT: nothing
Description:

This method simply go through whole iwe.Model[] array and call iwe.DRAW.tile() on every tile instance saved in this array.

images[] Type: array
Description:

Inside of this array there are stored every images you want to use in your app. All of those images are store as instances of Images(). This array you will use always when you want to use some preloaded images. It's filled by the iwe.loadImages() method.

Sample:
iwe.DRAW.image(iwe.images[tile.spriteId], tile.x, tile.y);
loadImages() Type: method
IN: "string", array[]
Description:

The first parameter is a string refers to the source folder of your images. The next parameter is an array filled with stings refers to the name and extension of the image. Every images is loaded to the temporary memory and instances of the Image() saves in to the iwe.images[] array.

Sample:
    iwe.loadImages('./images/', ['grass.png','unwalkable.png','hero.png']);
fullScreen() Type: method
IN: nothing
OUT: nothing
Description:

Using this method you can make the canvas element to have the same size as the users active screen (inside of the browser). You can call this method anytime or set the fullscreen implicitly inside of the iwe.INIT() by setting the fullscreen parameter to true.

CLEAR() Type: method
IN: nothing
OUT: nothing
Description:

When you call this method, it will delete whole content of the canvas element. This method is useful (with iwe.DRAW.all()) always when you want to redraw whole scene.

Ke stažení

Odkazy

Tyto stránky jsou prozatím pouze betaverzí a budou do budoucna zdokonalovány. Pokud máte jakékoli nápady, nebo dotazy, neváhejte se na mě obrátit :)

@TonnyVlcek | vlcek.a@email.cz | 2014