Named after my IRL initials, a game engine I am working on to create Javascript games.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
JS/test.js

128 lines
3.4 KiB

5 years ago
var canvas = document.getElementById("mainScreen");
var con = canvas.getContext("2d");
var ironplate = loadImage("ironplate.png");
var keysDown = [];
var mouseDown = [];
var GAMESPEED = 60;
canvas.addEventListener("mousemove",mouseMoveEvent);
canvas.addEventListener("mouseup",mouseReleaseEvent);
canvas.addEventListener("mousedown",mousePressEvent);
//window.addEventListener("key",keyEvent);
window.addEventListener("keydown",keyPressEvent);
window.addEventListener("keyup",keyReleaseEvent);
window.addEventListener('contextmenu', event => event.preventDefault());
5 years ago
drawSetFont("16px Tahoma");
setGameSpeed(GAMESPEED);
setBackgroundColor("black");
resetTransform();
/*
drawSetFont("20px Tahoma");
drawSetHAlign("left");
drawSetColor("#FFFFFF");
drawLine(0,0,50,50);
drawCircle(150,150,30);
drawText(256,64,"Hello World!");
drawSetColor("yellow");
drawRectangle(640,480,660,540,false);
*/
var frame=0;
var gameObjs = [];
instance_create(new DebugObj(0,0,null));
instance_create(new IronPlate(48,48,ironplate));
function onGameUpdate() {
setBackgroundColor("black");
gameObjs.forEach(stepEvent);
gameObjs.forEach(drawObject);
}
function stepEvent(item,index) {
item.stepEvent();
}
function drawObject(item,index) {
//drawText(64,32*(index+1),"Draw Object: "+item+" ("+item.visible+") / ("+item.x+","+item.y+")");
item.drawEvent();
}
function mouseMoveEvent(e) {
for (i=0;i<gameObjs.length;i++) {
gameObjs[i].mouseMoveEvent(e);
}
}
function keyEvent(e) {
for (i=0;i<gameObjs.length;i++) {
gameObjs[i].keyEvent(e);
}
}
function mousePressEvent(e) {
//https://www.w3schools.com/jsref/obj_mouseevent.asp
/*
"LEFT"
"RIGHT"
"MIDDLE"
"MOUSE4"
"MOUSE5"
*/
4 years ago
var pressedDown = {left:false,right:false,middle:false,mouse4:false,mouse5:false}
5 years ago
mouseDown.splice(0,mouseDown.length);
var buttonamt = e.buttons;
4 years ago
if (buttonamt>=16) {buttonamt-=16;pressedDown.mouse5=true;mouseDown.push("MOUSE5");}
if (buttonamt>=8) {buttonamt-=8;pressedDown.mouse4=true;mouseDown.push("MOUSE4");}
if (buttonamt>=4) {buttonamt-=4;pressedDown.middle=true;mouseDown.push("MIDDLE");}
if (buttonamt>=2) {buttonamt-=2;pressedDown.right=true;mouseDown.push("RIGHT");}
if (buttonamt>=1) {buttonamt-=1;pressedDown.left=true;mouseDown.push("LEFT");}
5 years ago
for (i=0;i<gameObjs.length;i++) {
4 years ago
gameObjs[i].mousePressEvent(e,pressedDown);
5 years ago
}
}
function mouseReleaseEvent(e) {
4 years ago
var pressedDown = {left:false,right:false,middle:false,mouse4:false,mouse5:false}
5 years ago
mouseDown.splice(0,mouseDown.length);
var buttonamt = e.buttons;
4 years ago
if (buttonamt>=16) {buttonamt-=16;pressedDown.mouse5=true;mouseDown.push("MOUSE5");}
if (buttonamt>=8) {buttonamt-=8;pressedDown.mouse4=true;mouseDown.push("MOUSE4");}
if (buttonamt>=4) {buttonamt-=4;pressedDown.middle=true;mouseDown.push("MIDDLE");}
if (buttonamt>=2) {buttonamt-=2;pressedDown.right=true;mouseDown.push("RIGHT");}
if (buttonamt>=1) {buttonamt-=1;pressedDown.left=true;mouseDown.push("LEFT");}
5 years ago
for (i=0;i<gameObjs.length;i++) {
4 years ago
gameObjs[i].mouseReleaseEvent(e,pressedDown);
5 years ago
}
}
function keyPressEvent(e) {
//https://www.w3schools.com/jsref/obj_keyboardevent.asp
if (keysDown.indexOf(e.key)==-1) {
keysDown.push(e.key);
}
for (i=0;i<gameObjs.length;i++) {
gameObjs[i].keyPressEvent(e);
}
}
function keyReleaseEvent(e) {
keysDown.pop(keysDown.indexOf(e.key));
for (i=0;i<gameObjs.length;i++) {
gameObjs[i].keyReleaseEvent(e);
}
}
window.onload = function() {
//drawSpriteExt(320,400,ironplate,0.5,0.5);
}