JSFiddle - React, Tailwind, and code Playground
by Michael Prosser
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
JavaScript
var weapons = [
{
name: "AK47",
roundsPerClip: 60,
roundsTotal: 400,
roundsInClip: 60,
rsp: 10
},
{
name: "Uzzie 9mm",
roundsPerClip: 90,
roundsTotal: 400,
roundsInClip: 90,
rsp: 30
}
]
var player = {
height: 2,
speed: 0,
maxSpeed: 1,
health: "100",
shooting: false,
weapon: weapons[0],
changeWeapon: function(weaponIndex){
this.weapon = weapons[weaponIndex];
console.log("Changed weapon to: " + this.weapon.name);
},
shoot: function(){
// is there any rounds in clip
if(!this.weapon.roundsInClip){
//no rounds in the clip
console.log("Out of Ammo");
return false;
}
if(player.shooting){
// middle of shot
console.log("Gun jammed");
return false;
}
console.log("Shooting");
player.shooting = true;
setTimeout(function(){
player.shooting = false;
},1000/player.weapon.rsp);
// we got rounds
this.weapon.roundsInClip--; // increments down by 1
// create a mesh bullet and start it moving
return this.weapon.roundsInClip;
}
}
$(document).keydown(function(e){
if(e.keyCode == 49){ // number 1 key
player.changeWeapon(0);
} else if(e.keyCode == 50){ // number 2 key
player.changeWeapon(1);
} else if(e.keyCode == 32){ // space bar to fire
player.shoot();
}
});
player.weapon.name = "Stungun";
console.log(weapons);
//player.speed = 0.2;
/*
console.log("Dan has " + player.weapon.roundsInClip + " rounds in his clip");
var cats = [];
cats.push("Yoda");
cats.push("Minky");
cats.push("Midnight");
console.log("cats[0]",cats[0]);
console.log("cats.length",cats.length);
for(var i=0;i<cats.length;i++){
player.shoot();
console.log("Dan has just shot " + cats[i] + " he now has " + player.weapon.roundsInClip + " rounds in his clip");
}
var enemies = [
{
species:...