Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const socket = io('ws://localhost:5000')
- socket.on('connect', () => {
- console.log('user connected', socket.id)
- })
- const canvas = document.querySelector('canvas')
- const c = canvas.getContext('2d')
- canvas.width = 840
- canvas.height = 840
- c.fillRect(0, 0, canvas.width, canvas.height)
- class Sprite {
- constructor({position, velocity}){
- this.position = position
- this.velocity = velocity
- this.height = 10
- this.width = 10
- this.lastKey
- this.angle = 0
- this.rotation
- this.mouseX
- this.mouseY
- this.targetVelocityX
- this.targetVelocityY
- this.gauntlet = {
- position: this.position,
- width: 2,
- height: -8,
- }
- this.health = 200
- this.powerUp
- this.isDashing
- }
- draw() {
- //health bar
- c.fillStyle = 'red'
- c.fillRect(this.position.x - 20, this.position.y - 30, this.width * 5, 5)
- c.fillStyle = 'green'
- c.fillRect(this.position.x - 20, this.position.y - 30, this.width * 5 * (this.health / 200), 5)
- //following mouse cursor
- c.save()
- c.translate(this.position.x + this.width / 2, this.position.y + this.height / 2)
- c.rotate(this.angle)
- c.translate(-this.position.x - this.width / 2, -this.position.y - this.height / 2)
- c.fillStyle = 'red'
- c.fillRect(this.position.x, this.position.y, this.width, this.height)
- c.fillStyle = 'purple'
- c.fillRect(
- this.gauntlet.position.x + 4,
- this.gauntlet.position.y,
- this.gauntlet.width,
- this.gauntlet.height)
- c.restore()
- }
- update(){
- this.angle = Math.atan2(this.mouseX - this.position.x, - (this.mouseY - this.position.y))
- this.draw()
- this.position.x += this.velocity.x
- this.position.y += this.velocity.y
- if(this.position.y + this.height + this.velocity.y >= 500){
- this.velocity.y = 0
- }else if(this.position.x + this.width + this.velocity.x >= 500){
- this.velocity.x = 0
- }
- }
- }
- class Projectile {
- constructor({position, velocity}, width, height, damage, color){
- this.position = position
- this.velocity = velocity
- this.width = width,
- this.height = height
- this.distance
- this.type
- this.damage = damage
- this.color = color
- }
- draw(){
- c.fillStyle = this.color
- c.fillRect(
- this.position.x,
- this.position.y,
- this.width,
- this.height)
- }
- update(){
- this.draw()
- this.position.x += this.velocity.x
- this.position.y += this.velocity.y
- }
- }
- class Boundary {
- static spacingX = 40
- static spacingY = 40
- constructor({position, width, height, modifier}){
- this.position = position
- this.width = width
- this.height = height
- this.modifier = modifier
- }
- draw(){
- c.save()
- this.rotateBoundary()
- c.fillStyle = '#5FA8D3'
- c.fillRect(this.position.x ,this.position.y, this.width, this.height)
- c.restore()
- }
- rotateBoundary() {
- const centerX = this.position.x + this.width / 2
- const centerY = this.position.y + this.height / 2
- c.translate(centerX, centerY)
- c.rotate(this.modifier * Math.PI / 180)
- c.translate(-centerX, -centerY)
- }
- }
- class Consumable {
- constructor({position, radius, color}){
- this.position = position
- this.radius = radius
- this.color = color
- }
- draw(){
- c.beginPath()
- c.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2)
- c.fillStyle = this.color
- c.fill()
- c.closePath()
- }
- }
- const throttle = (fn, delay) => {
- let last = 0;
- return (...args) => {
- const now = new Date().getTime()
- if(now - last < delay){
- return;
- }
- last = now
- return fn(...args)
- }
- }
- const boundaries = []
- const consumables = []
- //creating the map
- const map = [
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ','+',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' '],
- [' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','=',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ','-','.',' ',' ',' ','.','-',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','+',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ','-','.',' ',' ',' ','.','-',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ','=',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' '],
- [' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','+',' '],
- [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
- ]
- map.forEach((row, i) => {
- row.forEach((symbol, j) => {
- switch (symbol) {
- case '-':
- boundaries.push(
- new Boundary({
- position: {
- x:Boundary.spacingX * j,
- y:Boundary.spacingY * i
- },
- width: 40,
- height: 40,
- modifier: 1
- })
- )
- break
- case '#':
- boundaries.push(
- new Boundary({
- position: {
- x:Boundary.spacingX * j + 10,
- y:Boundary.spacingY * i + 10
- },
- width: 20,
- height: 20,
- modifier: 45
- })
- )
- break
- case '.':
- consumables.push(
- new Consumable({
- position: {
- x:Boundary.spacingX * j + 20,
- y:Boundary.spacingY * i + 20
- },
- radius: 5,
- color: '#38B000'
- })
- )
- break
- case '+':
- consumables.push(
- new Consumable({
- position: {
- x:Boundary.spacingX * j + 20,
- y:Boundary.spacingY * i + 20
- },
- radius: 8,
- color: 'blue'
- })
- )
- break
- case '=':
- boundaries.push(
- new Boundary({
- position: {
- x:Boundary.spacingX * j,
- y:Boundary.spacingY * i
- },
- width: 40,
- height: 40,
- modifier: 45
- })
- )
- break
- case '*':
- boundaries.push(
- new Boundary({
- position: {
- x:Boundary.spacingX * j + 10,
- y:Boundary.spacingY * i + 10
- },
- width: 20,
- height: 20,
- modifier: 1
- })
- )
- break
- }
- })
- })
- const player = new Sprite({
- position: {
- x: 200,
- y: 200
- },
- velocity: {
- x: 0,
- y: 0
- },
- })
- const enemy = new Sprite({
- position: {
- x: 200,
- y: 200
- },
- velocity: {
- x: 0,
- y: 0
- },
- })
- const projectiles = []
- const keys = {
- a: {
- pressed: false
- },
- d: {
- pressed: false
- },
- s: {
- pressed: false
- },
- w: {
- pressed: false
- },
- shift: {
- pressed: false
- },
- click: {
- clicked: false
- },
- one: {
- pressed: false
- },
- two: {
- pressed: false
- },
- three: {
- pressed: false
- },
- left: {
- pressed: false
- },
- right: {
- pressed: false
- },
- up: {
- pressed: false
- },
- down: {
- pressed: false
- }
- }
- function circleCollisionDetection(p, c){
- const distX = Math.abs(c.position.x - p.position.x - p.width / 2)
- const distY = Math.abs(c.position.y - p.position.y - p.height / 2)
- if(distX > (p.width / 2 + c.radius)){
- return false
- }
- if(distY > (p.height / 2 + c.radius)){
- return false
- }
- if(distX <= (p.width / 2)){
- return true
- } else if(distY <= (p.height / 2)){
- return true
- }
- const dx = distX - p.width / 2
- const dy = distY - p.height / 2
- return (dx*dx + dy*dy <=(c.radius*c.radius))
- }
- let deletedItems = []
- let deletedItem = []
- function animate(){
- window.requestAnimationFrame(animate)
- c.fillStyle = '#CAE9FF'
- c.fillRect(0,0, canvas.width, canvas.height)
- //consumable collision
- for(let i = consumables.length - 1; 0 <= i; i--) {
- const consumable = consumables[i]
- consumable.draw()
- if(circleCollisionDetection(player, consumable)){
- console.log('colliding')
- if(consumable.color === '#38B000' && player.health < 200){
- enemy.health += 25
- deletedItem = consumables.splice(i, 1)
- deletedItems.push(deletedItem)
- deletedItems.forEach(deletedItem => {
- setTimeout(function(){
- consumables.splice(i, 0, ...deletedItem)
- }, 5000)
- })
- deletedItems.pop(deletedItem)
- } else if(consumable.color === 'blue'){
- player.powerUp = true
- deletedItem = consumables.splice(i, 1)
- deletedItems.push(deletedItem)
- deletedItems.forEach(deletedItem => {
- setTimeout(function(){
- consumables.splice(i, 0, ...deletedItem)
- }, 5000)
- console.log(deletedItems)
- })
- deletedItems.pop(deletedItem)
- }
- }
- if(circleCollisionDetection(enemy, consumable)){
- console.log('colliding')
- if(consumable.color === '#38B000' && enemy.health < 200){
- enemy.health += 25
- deletedItem = consumables.splice(i, 1)
- deletedItems.push(deletedItem)
- deletedItems.forEach(deletedItem => {
- setTimeout(function(){
- consumables.splice(i, 0, ...deletedItem)
- }, 5000)
- console.log(deletedItems)
- })
- deletedItems.pop(deletedItem)
- } else if(consumable.color === 'blue'){
- enemy.powerUp = true
- deletedItem = consumables.splice(i, 1)
- deletedItems.push(deletedItem)
- deletedItems.forEach(deletedItem => {
- setTimeout(function(){
- consumables.splice(i, 0, ...deletedItem)
- }, 5000)
- })
- deletedItems.pop(deletedItem)
- }
- }
- }
- boundaries.forEach(boundary => {
- boundary.draw()
- if(player.position.y + player.velocity.y
- <=
- boundary.position.y + boundary.height &&
- player.position.x + player.width + player.velocity.x
- >=
- boundary.position.x &&
- player.position.y + player.height + player.velocity.y
- >=
- boundary.position.y &&
- player.position.x + player.velocity.x
- <=
- boundary.position.x + boundary.width){
- console.log('colliding')
- player.velocity.x = 0
- player.velocity.y = 0
- }
- projectiles.forEach((projectile, index) => {
- if(projectile.position.y
- <=
- boundary.position.y + boundary.height &&
- projectile.position.y + projectile.height
- >=
- boundary.position.y &&
- projectile.position.x + projectile.width
- >=
- boundary.position.x &&
- projectile.position.x
- <=
- boundary.position.x + boundary.width) {
- projectiles.splice(index, 1)
- }
- })
- })
- player.update()
- enemy.update()
- //projectile update, collision detection and garbage collection
- projectiles.forEach((projectile, index) => {
- const distance = Math.sqrt(Math.pow((player.position.x-projectile.position.x), 2) +
- Math.pow((player.position.y-projectile.position.y), 2))
- if(projectile.color === 'green' && distance >= 250 && !player.powerUp){
- projectiles.splice(index, 1)
- }
- if(projectile.color === 'green' && player.powerUp && distance >= 500){
- projectiles.splice(index, 1)
- }
- if(projectile.position.y + projectile.height <= 0){
- projectiles.splice(index, 1)
- } else if(projectile.position.y <= enemy.position.y + enemy.height &&
- projectile.position.y + projectile.height >= enemy.position.y &&
- projectile.position.x + projectile.width >= enemy.position.x &&
- projectile.position.x <= enemy.position.x + enemy.width){
- console.log('enemy hit!')
- enemy.health -= projectile.damage
- if(enemy.health <= 0){
- enemy.health = 0
- console.log('enemy is dead!')
- }
- projectiles.splice(index, 1)
- }
- else if(projectile.color === 'red' && distance >= 600){
- projectiles.splice(index, 1)
- }
- else {
- projectile.update()
- }
- })
- player.velocity.x = 0
- player.velocity.y = 0
- enemy.velocity.x = 0
- enemy.velocity.y = 0
- // player movement
- if(keys.a.pressed ){
- player.velocity.x = -3
- } if(keys.d.pressed ){
- player.velocity.x = 3
- } if(keys.s.pressed ){
- player.velocity.y = 3
- } if(keys.w.pressed ){
- player.velocity.y = -3
- }
- // enemy movement
- if(keys.left.pressed ){
- enemy.velocity.x = -2
- } if(keys.right.pressed){
- enemy.velocity.x = 2
- } if(keys.down.pressed){
- enemy.velocity.y = 2
- } if(keys.up.pressed ){
- enemy.velocity.y = -2
- }
- dash()
- }
- animate()
- function dash() {
- //player dash
- if(keys.shift.pressed && keys.a.pressed){
- player.velocity.x = -10
- player.isDashing = true
- setTimeout(() => {
- keys.shift.pressed = false
- player.isDashing = false
- }, 300)
- } if(keys.shift.pressed && keys.d.pressed){
- player.velocity.x = 10
- player.isDashing = true
- if(player.isDashing = true) {
- keys.d.pressed = true
- }
- setTimeout(() => {
- keys.shift.pressed = false
- player.isDashing = false
- }, 300)
- } if(keys.shift.pressed && keys.s.pressed){
- player.velocity.y = 10
- player.isDashing = true
- if(player.isDashing = true) {
- keys.s.pressed = true
- }
- setTimeout(() => {
- keys.shift.pressed = false
- player.isDashing = false
- }, 300)
- } if(keys.shift.pressed && keys.w.pressed){
- player.velocity.y = -10
- player.isDashing = true
- if(player.isDashing = true) {
- keys.w.pressed = true
- }
- setTimeout(() => {
- keys.shift.pressed = false
- player.isDashing = false
- }, 300)
- }
- }
- window.addEventListener('keydown', (event) => {
- event.preventDefault()
- console.log(event)
- if(event.code == 'KeyD'){
- keys.d.pressed = true
- player.lastKey = 'd'
- }
- if(event.code == 'KeyA'){
- keys.a.pressed = true
- player.lastKey = 'a'
- }
- if(event.code == 'KeyS'){
- keys.s.pressed = true
- player.lastKey = 's'
- }
- if(event.code == 'KeyW'){
- keys.w.pressed = true
- player.lastKey = 'w'
- }
- switch (event.key) {
- case 'ArrowRight':
- keys.right.pressed = true
- enemy.lastKey = 'right'
- break
- case 'ArrowLeft':
- keys.left.pressed = true
- enemy.lastKey = 'left'
- break
- case 'ArrowDown':
- keys.down.pressed = true
- enemy.lastKey = 'down'
- break
- case 'ArrowUp':
- keys.up.pressed = true
- enemy.lastKey = 'up'
- break
- case 'Shift':
- keys.shift.pressed = true
- player.lastKey = 'Shift'
- break
- case '1':
- keys.one.pressed = true
- keys.two.pressed = false
- keys.three.pressed = false
- break
- case '2':
- keys.two.pressed = true
- keys.one.pressed = false
- keys.three.pressed = false
- break
- case '3':
- keys.three.pressed = true
- keys.one.pressed = false
- keys.two.pressed = false
- break
- }
- socket.emit("player1", player)
- })
- window.addEventListener('keyup', (event) => {
- event.preventDefault()
- if(event.code == 'KeyD'){
- keys.d.pressed = false
- }
- if(event.code == 'KeyA'){
- keys.a.pressed = false
- }
- if(event.code == 'KeyS'){
- keys.s.pressed = false
- }
- if(event.code == 'KeyW'){
- keys.w.pressed = false
- }
- switch (event.key) {
- //case 'Shift':
- //keys.shift.pressed = false
- //lastKey = 'Shift'
- //break
- case 'ArrowRight':
- keys.right.pressed = false
- break
- case 'ArrowLeft':
- keys.left.pressed = false
- break
- case 'ArrowDown':
- keys.down.pressed = false
- break
- case 'ArrowUp':
- keys.up.pressed = false
- break
- }
- socket.emit("player1", player)
- })
- window.addEventListener('click', throttle(() => {
- if(keys.one.pressed){
- fire()
- }
- socket.emit("player1", player)
- }, 500))
- window.addEventListener('click', throttle(() => {
- if(keys.two.pressed){
- toxic()
- }
- socket.emit("player1", player)
- }, 1000))
- window.addEventListener('click', throttle(() => {
- if(keys.three.pressed){
- ice()
- }
- socket.emit("player1", player)
- }, 1500))
- function fire(){
- const targetX = player.mouseX - (player.position.x + player.width / 2)
- const targetY = player.mouseY - (player.position.y + player.height / 2)
- player.rotation = Math.atan2(targetY, targetX)
- player.targetVelocityX = Math.cos(player.rotation)
- player.targetVelocityY = Math.sin(player.rotation)
- if(player.powerUp){
- projectiles.push(new Projectile({
- position: {
- x: player.position.x + player.width / 2,
- y: player.position.y + player.height / 2
- },
- velocity: {
- x: player.targetVelocityX * 10,
- y: player.targetVelocityY * 10
- },
- }, 7, 7, 75, 'red'))
- } else {
- projectiles.push(new Projectile({
- position: {
- x: player.position.x + player.width / 2,
- y: player.position.y + player.height / 2
- },
- velocity: {
- x: player.targetVelocityX * 10,
- y: player.targetVelocityY * 10
- },
- }, 5, 5, 50, 'red'))
- }
- }
- function toxic(){
- const targetX = player.mouseX - (player.position.x + player.width / 2)
- const targetY = player.mouseY - (player.position.y + player.height / 2)
- player.rotation = Math.atan2(targetY, targetX)
- player.targetVelocityX = Math.cos(player.rotation)
- player.targetVelocityY = Math.sin(player.rotation)
- console.log(player.targetVelocityX)
- let toxicModifier = [-0.2, -0.1, 0, 0.1, 0.2]
- let toxicObj = {}
- if(player.powerUp){
- toxicModifier = [-0.1, -0.05, 0, 0.05, 0.1]
- for(let i = 0; i <= 5; i++){
- for(let j = 0; j < toxicModifier.length; j ++){
- player.targetVelocityX = Math.cos(player.rotation - toxicModifier[i])
- player.targetVelocityY = Math.sin(player.rotation - toxicModifier[i])
- }
- toxicObj = new Projectile({
- position: {
- x: player.position.x + player.width / 2,
- y: player.position.y + player.height / 2
- },
- velocity: {
- x: player.targetVelocityX * 8,
- y: player.targetVelocityY * 8
- },
- }, 4, 4, 20, 'green')
- projectiles.push(toxicObj)
- }
- } else {
- for(let i = 0; i <= 5; i++){
- for(let j = 0; j < toxicModifier.length; j ++){
- player.targetVelocityX = Math.cos(player.rotation - toxicModifier[i])
- player.targetVelocityY = Math.sin(player.rotation - toxicModifier[i])
- }
- toxicObj = new Projectile({
- position: {
- x: player.position.x + player.width / 2,
- y: player.position.y + player.height / 2
- },
- velocity: {
- x: player.targetVelocityX * 8,
- y: player.targetVelocityY * 8
- },
- }, 4, 4, 20, 'green')
- projectiles.push(toxicObj)
- }
- }
- }
- function ice(){
- const targetX = player.mouseX - (player.position.x + player.width / 2)
- const targetY = player.mouseY - (player.position.y + player.height / 2)
- player.rotation = Math.atan2(targetY, targetX)
- player.targetVelocityX = Math.cos(player.rotation)
- player.targetVelocityY = Math.sin(player.rotation)
- if(player.powerUp){
- projectiles.push(new Projectile({
- position: {
- x: player.position.x + player.width / 2,
- y: player.position.y + player.height / 2
- },
- velocity: {
- x: player.targetVelocityX * 20,
- y: player.targetVelocityY * 20
- }
- }, 3, 3, 200, 'blue'))
- } else {
- projectiles.push(new Projectile({
- position: {
- x: player.position.x + player.width / 2,
- y: player.position.y + player.height / 2
- },
- velocity: {
- x: player.targetVelocityX * 15,
- y: player.targetVelocityY * 15
- }
- }, 3, 3, 150, 'blue'))
- }
- }
- window.addEventListener('mousemove', (event) => {
- player.mouseX = event.offsetX
- player.mouseY = event.offsetY
- })
Advertisement
Add Comment
Please, Sign In to add comment