This is the Never Isometric Engine.
It is an engine for isometric games.
Feel free to visit the GitHub Repository and to try out our examples.
You can also jump directly to the documentation.
<script src= "https://www.neverstudio.de/isometricEngine.js"></script>
setCanvas("YourCanvasId")
World
object contains all physical objects.IsoObject
class can be used for creating physical objects.setCanvas(id,pixelized=false,full=false,nWidth = undefined,nHeight = 500)
pixelized=true
to enable position rounding in the draw function and a pixelated canvas.
full=true
to make the canvas fullscreen.
nWidth
and nHeight
. You can only set one of them.
setRatio(newRatio)
newRatio
parameter is expected to be of type float.toScreen(isoX,isoY,isoZ=0)
World.center
toIso(isoX,isoY,isoZ=0)
World.center
.grid(minimum = -1000,maximum = 1000, steps=10)
minimum
and the maximum
parameters correspond to the minimum and maximum x and y values used in the grid. steps
parameter changes the size of each square in the grid.isoLine(x1,y1,x2,y2,z1=0,z2=0,color="black",width = 5)
x1,y1,z1
and the x2,y2,z2
parameters correspond to the positions of the start and end point. color
parameter changes the color of the line.width
parameter changes the linewidth.drawImage(image,width,height,x,y,z=0,canvas = canvas, offset = [0,0])
image
parameter is expected to be a javascript image element.pixelized=true
was passed to setCanvas
.canvas
parameter is the canvas to draw on.offset
parameter is the x and y offset of the image in canvas coordinates.canvas
: Id of the Canvas Object all IsoObjects in the World Object are drawn on.keyObject
: You can pass an Object here to make it possible to move it using the Arrow-Keys or WASD.displayBox
: Whether the boundinhg boxes of the IsoObjects are displayed by default.displayShadow
: Whether the shadow bounding boxes of the IsoObjects are displayed by default.displayImage
: Whether the image of the IsoObjects are displayed by default.World.add()
method.images
: JSON containing all image paths from IsoObjects as keys and image objects as values.
images
: A JSON containing all images of the IsoObjects as image objects with the image paths as keys.mousePos
: The position of the mouse as an isometric coordinate.objects
: All objects that have been added to the world.add(object,func= yourFunction)
loadImage(img,func= yourFunction)
img
argument is the path of the image you want to loadfunc
parameter is a function that will be executed when the image is loaded successfully.remove(object)
setKeyObject(object)
World.smartMovement
to true.move(object,xchange,ychange,zchange=0)
World.detail
.center(object)
draw(clear=true)
clear
is set to false the canvas is not cleared before drawing.clear
is set to true.constructor(x,y,z,bounding,images,width=undefined,height=undefined,collision=undefined,screen=false)
x,y and z
: These properties are isometric coordinates and they determine the position of the object.
bounding
: This property is an Array that sets the IsoObjects' collsion box. The Array has the following format: [[minimum_x,minimum_y,minimum_z],[maximum_x,maximum_y,maximum_z]]
img
: A list containing the paths to all images the IsoObject is supposed to use for drawing.
width
: Width of the IsoObject (for drawing the image).
height
: Height of the IsoObject (for drawing the image).
collision
: A function that is executed when the IsoObject collides with another IsoObject.
screen
: You can also pass the x and y position as canvas coordinates. Then you have to set screen
to true.
aTurn and bTurn
: 0
by default. They represent how the IsObject is turned.
turnMap
: A JSON that maps image paths to specific ways the IsoObject is turned.
{
"front":{
"angle":[0,0], // [aTurn,bTurn]
"imgs":["img1.png","img2.png",...]
},
"behind":{
"angle":[0,180], // [aTurn,bTurn]
"imgs":["img5.png","img6.png",...]
}
}
displayBox, displayShadow, displayImage
: Same like with world. Changes the standard setting.
World.add()
method resets them to the World Objects defaults.
activeImage
: The url from the img
list that is used for drawing.
animationIndex
: The index of the image that should be drawn in the "img" list of the current direction in turnMap.
turnMap
, aTurn
=0 and bTurn
=0: animationIndex = 0
corresponds to "img1.png" as activeImage
imgChange
(in canvas coordinates): [0,0]
by default. Changes the image position relative to the coordinate.
imgChange
is overwritten by offsetMap
in the update()
method you should set the default value of offsetMap
instead.
offsetMap
(in canvas coordinates): {"default":[0,0]}
by default. JSON that maps image paths to corresponding offsets during drawing (like imgChange
).
{
"default":[0,0],// value that is used if the path of activeImage isn't a key of offsetMap
"img1.png":[1,5],// offset that is used when drawing img1.png
"img2.png":[1,5]// offset that is used when drawing img2.png
}
hasCollision
: Whether the IsoObject collides with other IsoObjects.
update()
: This function updates some internal values for collision and drawing.
activeImage
depending on how the IsoObject is turned.
move(xchange,ychange,zchange=0,draw=true)
: Moves the object. Collision is included. The canvas is only updated if draw is true.
moveForward(amount,draw=true)
: Moves the IsoObject forward based on how it is turned (currently only along the vertical plane). The canvas is only updated if draw is true.
moveTo(newx,newy,newz=0,draw=true)
: Moves the IsoObject to a specific position. The canvas is only updated if draw is true.
turn(aTurn,bTurn,draw=true)
: Turns the object by the given angles. The canvas is only updated if draw is true. Collision is included.
turnTo(aTurn,bTurn,draw=true)
: Turns the object to the given angles. The canvas is only updated if draw is true.
draw(image,offset=[0,0,0])
: Used by the World Object to draw the IsoObject.
animate(animation,min,max,steps=500)
: This method animates an object based on a JSON.
min
and max
are the start and end times of the animation in milliseconds. steps
is the size of individual timesteps (only necessary if "all" is set in animation
).
animation
is a json that determines how the animation works. Here is an example:
{
"all":{ // if the key is all the animation will be executed every steps milliseconds from min to max
"move":[10,10,0], //move can be used for moving the object with collision. This example moves the object by 10 in x- and y-direction and 0 in z-direction.
"displayBox":true //you can also directly set most properties of IsoObject. The example is equivalent to displayBox = true.
}
"10": { // executed when 10 percent of the time between min and max have passed
"moveForward":10, //moveForward can be used for moving the object with the moveForward method
"add_animationIndex:1" //you can also add values to properties by writing "add_" at the beginning of their names. Lists will be added element by element.
"mult_animationIndex:2" //same as "add_" but with multiplication.
"func_animationIndex:customFunc" //This is equivalent to animationIndex = customFunc(animationIndex)
}
}
image
is not a path or url but an image object instead.
offset
defines the x,y and z offset and is used by the World Object to center objects.
collide(other)
: Returns true
if the IsoObject and the other IsoObject other
collide. Otherwise it returns false
.
layering(other)
: Used by the World Objects draw method. It determines whether the IsoObject is in front(1
) or behind(1
) the IsoObject other
colliding(world=this.world)
: Returns a list containing all IsoObjects of world
that collide with the IsoObject. If there are none it returns undefined
world
is by default the world object the IsoObject is part of.
<html>
<canvas id="canvas" width="1000px" height="1000px"></canvas>
<script src="https://www.neverstudio.de/isometricEngine.js"></script>
<script>
setRatio(1/4)
setCanvas("canvas")
var every = new World("canvas")
var centered = new IsoObject(100,100,0,[[-10,-10,0],[10,10,100]],["/image/of/centered/object.png"])
var other1 = new IsoObject(0,0,0,[[-10,-10,0],[10,10,100]],["image/of/other/object1.png"])
var other2 = new IsoObject(50,100,0,[[-10,-10,0],[10,10,100]],["image/of/other/object2.png"])
every.add(centered)
every.add(other1)
every.add(other2)
every.center(centered)
every.setKeyObject(centered)
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Isometric Game with Gameplay</title>
<style>
#score {
font-size: 20px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script src="isometricEngine.js"></script>
<script>
// Set the canvas ID and isometric ratio
setCanvas("gameCanvas");
setRatio(1 / 2);
// Initialize variables
var score = 0;
var playerSpeed = 5;
// Function to update the score
function updateScore(newScore) {
score = newScore;
document.getElementById('score').innerText = 'Score: ' + score;
}
// Create the game world
var gameWorld = new World("gameCanvas");
// Create the player object
var player = new IsoObject(100, 100, 0, [[-10, -10, 0], [10, 10, 50]], ["fig1.png"]);
//create the collision function for the gems
function gemCollision(gem,other){
updateScore(score+1)//add points for finding the gem
gem.moveTo(random(-200,200),random(-200,200))
}
// Create collectible items (e.g., gems)
var gem1 = new IsoObject(200, 100, 0, [[-10, -10, 0], [10, 10, 70]], ["fig2.png"]);
var gem2 = new IsoObject(300, 250, 0, [[-10, -10, 0], [10, 10, 70]], ["fig3.png"]);
//Add collision function to gems
gem1.collision = gemCollision
gem2.collision = gemCollision
// Create enemies that move back and forth
var enemy1 = new IsoObject(400, 150, 0, [[-10, -10, 0], [10, 10, 70]], ["fig4.png"]);
var enemy2 = new IsoObject(150, 300, 0, [[-10, -10, 0], [10, 10, 70]], ["fig4.png"]);
//add collision function to enemies:
//the game restarts when they collide with something
enemy1.collision = resetGame
enemy2.collision = resetGame
// Add objects to the game world
gameWorld.add(player);
gameWorld.add(gem1);
gameWorld.add(gem2);
gameWorld.add(enemy1);
gameWorld.add(enemy2);
// Center the camera on the player and allow movement with the arrow keys
gameWorld.center(player);
gameWorld.setKeyObject(player);
// Function to reset the game (called when player hits an enemy)
function resetGame() {
alert("restart")
updateScore(0); // Reset score
player.moveTo(100, 100, 0); // Move player back to the start
}
// Move the enemies back and forth
function moveEnemies() {
var direction = 1;
setInterval(function() {
// Make enemies move randomly
gameWorld.move(enemy1, random(-20,20), random(-20,20), 0);
gameWorld.move(enemy2, random(-20,20), random(-20,20), 0);
}, 100);
}
// Game loop to continuously draw the scene and check for collisions
function gameLoop() {
//draw the world
gameWorld.draw(true);
requestAnimationFrame(gameLoop);
}
// Start the enemy movement and game loop
moveEnemies();
gameLoop();
</script>
</body>
</html>
Email: info@neverstudio.com