FEE_MathRunner/js/allg.js

112 lines
2.5 KiB
JavaScript
Raw Normal View History

// animation
let playerAnim = [];
let tomAnim = [];
let enemyAnim = [];
//player
2024-06-27 10:31:37 +02:00
let playerAnimState = 0;
2024-07-04 10:32:27 +02:00
let runDirection = 0;
2024-08-27 10:22:57 +02:00
let runSpeed = 12;
2024-08-26 15:13:13 +02:00
let animationSpeed = 50;
2024-06-27 11:00:09 +02:00
let currentPosition = 0;
2024-08-29 08:18:22 +02:00
let player = document.querySelector("#fuchs");
let playerSprite = document.querySelector("#fuchs img");
2024-07-04 10:22:40 +02:00
let dIsPressed = false;
let aIsPressed = false;
let runAnimation;
// Map
const boxBorderLeft = -40;
const boxBorderRight = 840;
2024-06-27 09:36:34 +02:00
fillArrays();
2024-08-27 10:13:36 +02:00
startAnimation();
2024-06-27 11:00:09 +02:00
//currentPosition = window.getComputedStyle(player).left;
2024-06-27 09:36:34 +02:00
2024-07-04 10:32:27 +02:00
// Pictures
function fillArrays()
{
fillArray("fuchs_", 8, playerAnim);
fillArray("tom_", 3, tomAnim);
fillArray("enemy_",3, enemyAnim);
}
2024-06-27 09:36:34 +02:00
2024-07-04 10:32:27 +02:00
// startGame
2024-08-27 10:22:57 +02:00
let start = setInterval(gameLoop, 50);
2024-08-29 08:36:56 +02:00
let info = setInterval(infos, 1000);
function infos()
{
console.log(currentPosition);
}
2024-06-27 10:31:37 +02:00
2024-07-04 10:32:27 +02:00
function gameLoop()
2024-07-04 10:22:40 +02:00
{
2024-07-04 10:32:27 +02:00
move();
2024-07-04 10:22:40 +02:00
}
2024-07-04 10:32:27 +02:00
//-------------------------------------------------------------- Animation -----------------------------------------------------------------------
function startAnimation()
2024-06-27 10:31:37 +02:00
{
2024-08-26 15:13:13 +02:00
runAnimation = setInterval(playerRunAnim, animationSpeed);
2024-06-27 10:31:37 +02:00
}
function playerRunAnim()
{
if(playerAnimState == 8)
{
playerAnimState = 0;
}
2024-08-29 08:18:22 +02:00
playerSprite.src = playerAnim[playerAnimState];
2024-06-27 10:31:37 +02:00
playerAnimState++;
}
2024-07-04 10:32:27 +02:00
//------------------------------------------------------------- checkInput ------------------------------------------------------------------
2024-06-27 10:31:37 +02:00
document.addEventListener('keydown', function(event) {
if(event.key == "d")
{
2024-07-04 10:22:40 +02:00
if(runDirection != 1)
2024-06-27 11:00:09 +02:00
{
2024-08-29 08:18:22 +02:00
playerSprite.style.transform = 'scaleX(1)';
2024-06-27 11:00:09 +02:00
}
runDirection = 1;
2024-07-04 10:22:40 +02:00
dIsPressed = true;
2024-06-27 10:31:37 +02:00
}
else if(event.key == "a")
{
2024-07-04 10:22:40 +02:00
if(runDirection != -1)
2024-06-27 11:00:09 +02:00
{
2024-08-29 08:18:22 +02:00
playerSprite.style.transform = 'scaleX(-1)';
2024-06-27 11:00:09 +02:00
}
runDirection = -1;
2024-07-04 10:22:40 +02:00
aIsPressed = true;
}
});
document.addEventListener('keyup', function(event)
{
if(event.key == "d")
{
dIsPressed = false;
}
else if(event.key == "a")
{
aIsPressed = false;
}
if(!aIsPressed && !dIsPressed)
{
runDirection = 0;
2024-06-27 10:31:37 +02:00
}
2024-06-27 11:00:09 +02:00
});
2024-07-04 10:32:27 +02:00
//---------------------------------------------------------- Move Player ------------------------------------------------------------
2024-06-27 11:00:09 +02:00
function move()
{
let nextPosition = currentPosition + runDirection * runSpeed;
if(nextPosition > boxBorderLeft && nextPosition < boxBorderRight)
{
currentPosition = nextPosition;
}
2024-06-27 11:00:09 +02:00
player.style.left = currentPosition + "px";
}