commit e258330757a4461f602568fad1e55bc35a49ed36 Author: Daniel Legt Date: Wed Jun 24 19:56:56 2026 +0300 initial diff --git a/character.js b/character.js new file mode 100644 index 0000000..ecb9345 --- /dev/null +++ b/character.js @@ -0,0 +1,74 @@ +var c = { + velocity: { + x: 0, + y: 0, + }, + pos: { + x: 0, + y: 0, + }, + size: 50, + img: null, + jump() { + if ( this.velocity <= 0.15 ) { + this.velocity.y = -5; + } + }, + step() { + // Apply Gravity on our velocity. + this.velocity.y = this.velocity.y + 0.15; + + // Check for collision with platform ONLY WHEN FALLING. + if ( this.velocity.y > 0 ) { + let newY = false; + let charXLeft = this.pos.x; + let charXRight = this.pos.x + this.size; + + let charYTop = this.pos.y + let charYBottom = this.pos.y + this.size + + for ( let p of getPlatforms() ) { + // Is the X coord colliding? + let isXColliding = false; + + let pLeft = p.position.x; + let pRight = p.position.x + p.size.x; + let pTop = p.position.y; + let pBot = p.position.y + p.size.y; + + // Check Left/Right collision + if ( charXLeft < pRight && charXLeft > pLeft ) { + isXColliding = true; + } else if ( charXRight < pRight && charXRight > pLeft ) { + isXColliding = true; + } + + if ( isXColliding ) { + // Check if touching platform + if ( charYBottom < pBot && charYBottom > pTop ) { + newY = pTop - this.size; + break; + } + } + } + + if ( newY != false ) { + this.velocity.y = 0; + this.pos.y = newY; + } + } + + // Applying the velocity to our character position + this.pos.x = this.pos.x + this.velocity.x; + this.pos.y = this.pos.y + this.velocity.y; + + this.pos.x = mouseX; + }, + draw() { + fill(255, 125, 125); + if ( this.img != null ) { + image(this.img, c.pos.x, c.pos.y, this.size*1.25, this.size*1.25); + } + // square(c.pos.x, c.pos.y, c.size) + } +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..e414383 --- /dev/null +++ b/index.html @@ -0,0 +1,18 @@ + + + + + + + + + + + +
+
+ + + + + diff --git a/penguin.png b/penguin.png new file mode 100644 index 0000000..1c8b9d8 Binary files /dev/null and b/penguin.png differ diff --git a/platform.js b/platform.js new file mode 100644 index 0000000..77e69c4 --- /dev/null +++ b/platform.js @@ -0,0 +1,40 @@ +window.PLATFORMS_LIST = [] + +function getPlatforms() { + return PLATFORMS_LIST; +} + +/** + * Spawn a platform in the world and append it to the platforms list for collision checking + * @param {number} X The X Coordinate of the platform + * @param {number} Y The Y Coordinate of the platform + */ +function spawnPlatform(X, Y) { + + // Randomize Width a little bit + const DEFAULT_WIDTH = 125; + + const newWidth = DEFAULT_WIDTH + ( (Math.random() - 0.5) * DEFAULT_WIDTH ) + + const platform = { + position: { + x: X, + y: Y, + }, + size: { + x: newWidth, + y: 15, + }, + step() { + + }, + draw() { + fill(newWidth, newWidth, 255) + rect(this.position.x, this.position.y, this.size.x, this.size.y) + } + } + + PLATFORMS_LIST.push(platform); + + return platform; +} \ No newline at end of file diff --git a/sketch.js b/sketch.js new file mode 100644 index 0000000..6bc323a --- /dev/null +++ b/sketch.js @@ -0,0 +1,49 @@ +const TEXT_SIZE = 12; +const CHARACTER_SIZE = 50; + +var charimg; + +function preload() { + charimg = loadImage('/penguin.png'); +} + +function setup() { + createCanvas(300, 600); + frameRate(60) + + textSize(TEXT_SIZE) + textAlign(LEFT, TOP); + + c.size = CHARACTER_SIZE; + c.pos.x = width / 2 - CHARACTER_SIZE / 2; + c.pos.y = height / 2 - CHARACTER_SIZE / 2; + c.img = charimg; +} + +function draw() { + fill(0, 0, 0); + background(225); + text(`X/Y : ${mouseX}|${mouseY}`, 2, 2); + + getPlatforms() + .forEach(p => { + p.step(); + p.draw() + }) + + c.step(); // Run Logic + c.draw(); // Draw the character +} + +function keyPressed() { + if (key === 'c') { + c.jump(); + } +} + +function mouseClicked() { + if (mouseButton === LEFT) { + spawnPlatform(mouseX, mouseY); + console.log(PLATFORMS_LIST) + } +} \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..9386f1c --- /dev/null +++ b/style.css @@ -0,0 +1,7 @@ +html, body { + margin: 0; + padding: 0; +} +canvas { + display: block; +}