Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
willmil11 committed Nov 20, 2022
1 parent 6a22221 commit c3fbc89
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 9 deletions.
24 changes: 22 additions & 2 deletions Js/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
//App.js script
//

let Control = false;

//Build app
BuildApp();

//Execute render function
Render();

//Events
document.onkeydown = (event) => {
if (event.key === " "){
document.onkeydown = async (event) => {
if (event.key === " " || event.key === "ArrowUp"){
Player.jump();
}
if (event.key === "r"){
location.reload();
}
if (event.key === "c"){
req.open("GET", "https://willmil11-friendly-adventure-45g974rww9rf5rj-1234.preview.app.github.dev/clear");
req.send("Open");
}
if (event.key === "Control"){
Control = true;
await wait(300);
Control = false;
}
if (event.key === "Shift"){
if (Control){
Control = false;
ToogleFps();
}
}
}
6 changes: 6 additions & 0 deletions Js/clearconsole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function reqListener() {
console.log(this.responseText);
}

const req = new XMLHttpRequest();
req.addEventListener("load", reqListener);
108 changes: 102 additions & 6 deletions Js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@

let Cooldown = false;
let RotateCooldown = false;
let fps = false;
let fpscounter = 0;
let fpscounter_ = "...";
let fpswait = 0;

function ToogleFps(){
if (fps){
fps = false;
}
else{
fps = true;
}
}

var background = new Image();
background.src = "https://d2zia2w5autnlg.cloudfront.net/352/618cff3e20616-large";

function mainthreadpause(ms) {
var date = Date.now();
Expand Down Expand Up @@ -30,13 +46,20 @@ function BuildApp() {
renderer_error_div.style.height = ((window.innerHeight / 4) + "px");
renderer_error_div.style.marginTop = ((window.innerHeight / 8) + "px");
renderer_error_div.innerHTML += "<font color='white' face='arial'><center><big><big><big>Can't load renderer :(</big></big></big></center></font>";
document.body.style.backgroundImage = "url(https://d2zia2w5autnlg.cloudfront.net/352/618cff3e20616-large)";
document.body.style.backgroundSize = "cover";
}

function Render() {
var renderer_element = document.getElementById("renderer");
var renderer = renderer_element.getContext("2d");
//Clearing the renderer
renderer.clearRect(0, 0, window.innerWidth, window.innerHeight);
//Render ground
renderer.fillStyle = "rgb(20, 50, 100)";
renderer.fillRect(0, (window.innerHeight - 19), window.innerWidth, 20);
renderer.fillStyle = "white";
renderer.fillRect(0, (window.innerHeight - 19), window.innerWidth, 1);
//Render the player
renderer.save();
renderer.translate((Player.pos.x + 25), (Player.pos.y + 25));
Expand All @@ -45,14 +68,35 @@ function Render() {

renderer.fillStyle = "yellow";
renderer.fillRect(Player.pos.x, Player.pos.y, 50, 50);
renderer.fillStyle = "rgb(82, 229, 255)";
renderer.fillRect((Player.pos.x + 12.5), (Player.pos.y + 12.5), 10, 10);
renderer.fillRect((Player.pos.x + (25 + (12.5 / 2))), (Player.pos.y + 12.5), 10, 10);
renderer.fillRect((Player.pos.x + 12.5), (Player.pos.y + (25 + (12.5 / 2))), 28, 10);
renderer.fillStyle = "black"
renderer.fillRect((Player.pos.x - 1), Player.pos.y, 52, 2);
renderer.fillRect((Player.pos.x - 2), Player.pos.y, 2, 51);
renderer.fillRect((Player.pos.x - 2), (Player.pos.y + 50), 52, 2);
renderer.fillRect((Player.pos.x + 50), (Player.pos.y + 52), 2, -52)
renderer.restore();
//Render obstacles
renderer.fillStyle = Obstacles.test.color;
renderer.fillRect(Obstacles.test.pos.x, Obstacles.test.pos.y, Obstacles.test.sizes.w, Obstacles.test.sizes.h);
if (fps){
fpscounter += 1;
fpswait = 0;
renderer.fillStyle = "rgb(21, 21, 20)";
renderer.fillRect((window.innerWidth / 1.1), 0, (window.innerWidth - (window.innerWidth / 1.1)), (window.innerHeight - (window.innerHeight / 1.05)))
renderer.font = ((window.innerWidth / 42) + "px Arial");
renderer.fillStyle = "white";
renderer.fillText((`${fpscounter_}` + " fps"), (window.innerWidth / 1.09), 13)
}
}

let Player = {
ground: (window.innerHeight - 50),
ground: (window.innerHeight - 71),
pos: {
x: (window.innerWidth / 32),
y: (window.innerHeight - 51),
y: (window.innerHeight - 71),
rotation: 0
},
rotate: async function () {
Expand All @@ -74,10 +118,10 @@ let Player = {
RotateCooldown = false;
Cooldown = true;
Player.rotate();
while (Player.pos.y > (Player.ground - 70)) {
while (Player.pos.y > (Player.ground - 130)) {
Player.pos.y = Player.pos.y - 4;
RotateCooldown = true;
await Render();
Render();
RotateCooldown = false;
await wait(1);
}
Expand All @@ -86,12 +130,64 @@ let Player = {
RotateCooldown = true;
await Render();
RotateCooldown = false;
await wait(1);
await wait(5);
}
RotateCooldown = true;
Player.pos.rotation = 0;
Render();
Cooldown = false;
}
}
};
};

let Obstacles = {
test: {
color: "green",
pos: {
x: (window.innerWidth / 2),
y: (window.innerHeight - 51)
},
sizes: {
w: 30,
h: 30
}
}
}

function CheckPlayerColisions(Obstaclex, Obstacley, Obstaclew, Obstacleh){
if (Player.pos.x + 50 >= Obstaclex && Player.pos.x <= Obstaclex + Obstaclew && Player.pos.y + 50 >= Obstacley && Player.pos.y<= Obstacley + Obstacleh){
return true;
}
}

//Make the player go straight
setInterval(() => {
if (CheckPlayerColisions(Obstacles.test.pos.x, Obstacles.test.pos.y, 30, 30)){
Player.pos.x = (window.innerWidth / 32)
}
if (Player.pos.x < (window.innerWidth / 3.5)){
if (window.innerWidth <= 500){
Player.pos.x += 2;
}
else{
if (window.innerWidth <= 1000){
Player.pos.x += 3;
}
else{
Player.pos.x += 4;
}
}
}
Render();
}, 5);

setInterval(() => {
if (fps){
fpscounter_ = fpscounter;
fpscounter = 0;
}
}, 1005);

setInterval(() => {
Render();
}, 1);
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<body>
</body>
</html>
<script src="./Js/clearconsole.js"></script>
<script src="./Js/functions.js"></script>
<script src="./Js/app.js"></script>
7 changes: 6 additions & 1 deletion localserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ console.info("[LocalServer] Running...");

http.createServer(function (req, res) {
console.info("[Request] Requested url: '" + req.url + "'");
if (req.url === "/clear"){
console.clear();
console.info("[Console] Console has been cleared...");
}
else{
if (req.url === "/" || ((req.url).slice(0, -("1668855878756").length)) === "/?vscodeBrowserReqId="){
try{
res.writeHead(200, {'Content-Type': 'text/html'});
Expand All @@ -30,5 +35,5 @@ http.createServer(function (req, res) {
res.writeHead(404);
res.end(("An error has occured:<br><br>" + error))
}
}
}}
}).listen(1234);

0 comments on commit c3fbc89

Please sign in to comment.