SouthernGun

Untitled

Jan 26th, 2023
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const socket = io('ws://localhost:5000')
  2. socket.on('connect', () => {
  3.     console.log('user connected', socket.id)
  4. })
  5.  
  6.  
  7. const canvas = document.querySelector('canvas')
  8. const c = canvas.getContext('2d')
  9. canvas.width = 840
  10. canvas.height = 840
  11. c.fillRect(0, 0, canvas.width, canvas.height)
  12.  
  13. class Sprite {
  14.     constructor({position, velocity}){
  15.         this.position = position
  16.         this.velocity = velocity
  17.         this.height = 10
  18.         this.width = 10
  19.         this.lastKey
  20.         this.angle = 0
  21.         this.rotation
  22.         this.mouseX
  23.         this.mouseY
  24.         this.targetVelocityX
  25.         this.targetVelocityY
  26.         this.gauntlet = {
  27.             position: this.position,
  28.             width: 2,
  29.             height: -8,
  30.             }
  31.         this.health = 200
  32.         this.powerUp
  33.         this.isDashing
  34.        
  35.     }
  36.     draw() {
  37.         //health bar
  38.         c.fillStyle = 'red'
  39.         c.fillRect(this.position.x - 20, this.position.y - 30, this.width * 5, 5)
  40.  
  41.         c.fillStyle = 'green'
  42.         c.fillRect(this.position.x - 20, this.position.y - 30, this.width * 5 * (this.health / 200), 5)
  43.  
  44.         //following mouse cursor
  45.         c.save()
  46.         c.translate(this.position.x + this.width / 2, this.position.y + this.height / 2)
  47.         c.rotate(this.angle)
  48.         c.translate(-this.position.x - this.width / 2, -this.position.y - this.height / 2)
  49.         c.fillStyle = 'red'
  50.         c.fillRect(this.position.x, this.position.y, this.width, this.height)
  51.         c.fillStyle = 'purple'
  52.         c.fillRect(
  53.             this.gauntlet.position.x + 4,
  54.             this.gauntlet.position.y,
  55.             this.gauntlet.width,
  56.             this.gauntlet.height)
  57.  
  58.        
  59.         c.restore()
  60.    
  61.        
  62.     }
  63.     update(){
  64.         this.angle = Math.atan2(this.mouseX - this.position.x, - (this.mouseY - this.position.y))
  65.         this.draw()
  66.         this.position.x += this.velocity.x
  67.         this.position.y += this.velocity.y
  68.         if(this.position.y + this.height + this.velocity.y >= 500){
  69.             this.velocity.y = 0
  70.         }else if(this.position.x + this.width + this.velocity.x >= 500){
  71.             this.velocity.x = 0
  72.         }
  73.     }
  74. }
  75.  
  76. class Projectile {
  77.     constructor({position, velocity}, width, height, damage, color){
  78.         this.position = position
  79.         this.velocity = velocity
  80.         this.width = width,
  81.         this.height = height
  82.         this.distance
  83.         this.type
  84.         this.damage = damage
  85.         this.color = color
  86.        
  87.  
  88.        
  89.     }
  90.     draw(){
  91.         c.fillStyle = this.color
  92.         c.fillRect(
  93.         this.position.x,
  94.         this.position.y,
  95.         this.width,
  96.         this.height)
  97.     }
  98.     update(){
  99.         this.draw()
  100.         this.position.x += this.velocity.x
  101.         this.position.y += this.velocity.y
  102.     }
  103. }
  104. class Boundary {
  105.     static spacingX = 40
  106.     static spacingY = 40
  107.     constructor({position, width, height, modifier}){
  108.         this.position = position
  109.         this.width = width
  110.         this.height = height
  111.         this.modifier = modifier
  112.     }
  113.     draw(){
  114.         c.save()
  115.         this.rotateBoundary()
  116.         c.fillStyle = '#5FA8D3'
  117.         c.fillRect(this.position.x ,this.position.y, this.width, this.height)
  118.         c.restore()
  119.     }
  120.     rotateBoundary() {
  121.         const centerX = this.position.x + this.width / 2
  122.         const centerY = this.position.y + this.height / 2
  123.         c.translate(centerX, centerY)
  124.         c.rotate(this.modifier * Math.PI / 180)
  125.         c.translate(-centerX, -centerY)
  126.        
  127.        
  128.  
  129.     }
  130. }
  131. class Consumable {
  132.     constructor({position, radius, color}){
  133.         this.position = position
  134.         this.radius = radius
  135.         this.color = color
  136.     }
  137.     draw(){
  138.         c.beginPath()
  139.         c.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2)
  140.         c.fillStyle = this.color
  141.         c.fill()
  142.         c.closePath()
  143.     }
  144.  
  145. }
  146. const throttle = (fn, delay) => {
  147.     let last = 0;
  148.         return (...args) => {
  149.             const now = new Date().getTime()
  150.             if(now - last < delay){
  151.                 return;
  152.             }
  153.             last = now
  154.             return fn(...args)
  155.         }
  156. }
  157.  
  158. const boundaries = []
  159. const consumables = []
  160.  //creating the map
  161.  const map = [
  162.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  163.     [' ','+',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' '],
  164.     [' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  165.     [' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','=',' ',' ',' '],
  166.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' '],
  167.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  168.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  169.     [' ',' ',' ',' ',' ',' ',' ','-','.',' ',' ',' ','.','-',' ',' ',' ',' ',' ',' ',' '],
  170.     [' ',' ',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ',' ',' '],
  171.     [' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  172.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','+',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  173.     [' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  174.     [' ',' ',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ',' ',' '],
  175.     [' ',' ',' ',' ',' ',' ',' ','-','.',' ',' ',' ','.','-',' ',' ',' ',' ',' ',' ',' '],
  176.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  177.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  178.     [' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
  179.     [' ',' ',' ','=',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' '],
  180.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' '],
  181.     [' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','+',' '],
  182.     [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
  183.    
  184. ]
  185.  
  186. map.forEach((row, i) => {
  187.     row.forEach((symbol, j) => {
  188.         switch (symbol) {
  189.             case '-':
  190.                 boundaries.push(
  191.                     new Boundary({
  192.                         position: {
  193.                             x:Boundary.spacingX * j,
  194.                             y:Boundary.spacingY * i
  195.                         },
  196.                         width: 40,
  197.                         height: 40,
  198.                         modifier: 1
  199.                     })
  200.                 )
  201.                 break
  202.             case '#':
  203.                 boundaries.push(
  204.                     new Boundary({
  205.                         position: {
  206.                             x:Boundary.spacingX * j + 10,
  207.                             y:Boundary.spacingY * i + 10
  208.                         },
  209.                         width: 20,
  210.                         height: 20,
  211.                         modifier: 45
  212.                     })
  213.                 )
  214.                 break
  215.             case '.':
  216.                 consumables.push(
  217.                     new Consumable({
  218.                         position: {
  219.                             x:Boundary.spacingX * j + 20,
  220.                             y:Boundary.spacingY * i + 20
  221.                         },
  222.                         radius: 5,
  223.                         color: '#38B000'  
  224.                     })
  225.                 )
  226.                 break
  227.             case '+':
  228.                 consumables.push(
  229.                     new Consumable({
  230.                         position: {
  231.                             x:Boundary.spacingX * j + 20,
  232.                             y:Boundary.spacingY * i + 20
  233.                         },
  234.                         radius: 8,
  235.                         color: 'blue'  
  236.                     })
  237.                 )
  238.                 break
  239.             case '=':
  240.                 boundaries.push(
  241.                     new Boundary({
  242.                         position: {
  243.                              x:Boundary.spacingX * j,
  244.                              y:Boundary.spacingY * i
  245.                         },
  246.                         width: 40,
  247.                         height: 40,
  248.                         modifier: 45
  249.                     })
  250.                 )
  251.                 break
  252.             case '*':
  253.                 boundaries.push(
  254.                     new Boundary({
  255.                         position: {
  256.                             x:Boundary.spacingX * j + 10,
  257.                             y:Boundary.spacingY * i + 10
  258.                         },
  259.                         width: 20,
  260.                         height: 20,
  261.                         modifier: 1
  262.                     })
  263.                 )
  264.                 break
  265.         }
  266.     })
  267.  
  268. })
  269.  
  270.  
  271. const player = new Sprite({
  272.     position: {
  273.         x: 200,
  274.         y: 200
  275.     },
  276.     velocity: {
  277.         x: 0,
  278.         y: 0
  279.     },
  280.    
  281. })
  282.  
  283. const enemy = new Sprite({
  284.     position: {
  285.         x: 200,
  286.         y: 200
  287.     },
  288.     velocity: {
  289.         x: 0,
  290.         y: 0
  291.     },
  292. })
  293.  
  294. const projectiles = []
  295.  
  296.  
  297. const keys = {
  298.     a: {
  299.         pressed: false
  300.     },
  301.     d: {
  302.         pressed: false
  303.     },
  304.     s: {
  305.         pressed: false
  306.     },
  307.     w: {
  308.         pressed: false
  309.     },
  310.     shift: {
  311.         pressed: false
  312.     },
  313.     click: {
  314.         clicked: false
  315.     },
  316.     one: {
  317.         pressed: false
  318.     },
  319.     two: {
  320.         pressed: false
  321.     },
  322.     three: {
  323.         pressed: false
  324.     },
  325.     left: {
  326.         pressed: false
  327.     },
  328.     right: {
  329.         pressed: false
  330.     },
  331.     up: {
  332.         pressed: false
  333.     },
  334.     down: {
  335.         pressed: false
  336.     }
  337. }
  338. function circleCollisionDetection(p, c){
  339.     const distX = Math.abs(c.position.x - p.position.x - p.width / 2)
  340.     const distY = Math.abs(c.position.y - p.position.y - p.height / 2)
  341.         if(distX > (p.width / 2 + c.radius)){
  342.             return false
  343.         }
  344.         if(distY > (p.height / 2 + c.radius)){
  345.             return false
  346.         }
  347.         if(distX <= (p.width / 2)){
  348.             return true
  349.         } else if(distY <= (p.height / 2)){
  350.             return true
  351.         }
  352.         const dx = distX - p.width / 2
  353.         const dy = distY - p.height / 2
  354.         return (dx*dx + dy*dy <=(c.radius*c.radius))
  355. }
  356. let deletedItems = []
  357. let deletedItem = []
  358.  
  359.  
  360. function animate(){
  361.    
  362.     window.requestAnimationFrame(animate)
  363.    
  364.     c.fillStyle = '#CAE9FF'
  365.     c.fillRect(0,0, canvas.width, canvas.height)
  366.  
  367.     //consumable collision
  368.     for(let i = consumables.length - 1; 0 <= i; i--) {
  369.         const consumable = consumables[i]
  370.        
  371.         consumable.draw()
  372.         if(circleCollisionDetection(player, consumable)){
  373.             console.log('colliding')
  374.             if(consumable.color === '#38B000' && player.health < 200){
  375.                 enemy.health += 25
  376.                 deletedItem = consumables.splice(i, 1)
  377.                 deletedItems.push(deletedItem)
  378.                 deletedItems.forEach(deletedItem => {
  379.                     setTimeout(function(){
  380.                         consumables.splice(i, 0, ...deletedItem)
  381.                     }, 5000)
  382.                    
  383.                 })
  384.                 deletedItems.pop(deletedItem)
  385.             } else if(consumable.color === 'blue'){
  386.                 player.powerUp = true
  387.                 deletedItem = consumables.splice(i, 1)
  388.                 deletedItems.push(deletedItem)
  389.                 deletedItems.forEach(deletedItem => {
  390.                     setTimeout(function(){
  391.                         consumables.splice(i, 0, ...deletedItem)
  392.                     }, 5000)
  393.                     console.log(deletedItems)
  394.                    
  395.                 })
  396.                 deletedItems.pop(deletedItem)
  397.             }
  398.         }
  399.        
  400.         if(circleCollisionDetection(enemy, consumable)){
  401.             console.log('colliding')
  402.             if(consumable.color === '#38B000' && enemy.health < 200){
  403.                 enemy.health += 25
  404.                 deletedItem = consumables.splice(i, 1)
  405.                 deletedItems.push(deletedItem)
  406.                
  407.                 deletedItems.forEach(deletedItem => {
  408.                     setTimeout(function(){
  409.                         consumables.splice(i, 0, ...deletedItem)
  410.                     }, 5000)
  411.                    
  412.                     console.log(deletedItems)
  413.    
  414.                 })
  415.                 deletedItems.pop(deletedItem)
  416.                
  417.                    
  418.  
  419.             } else if(consumable.color === 'blue'){
  420.                 enemy.powerUp = true
  421.                 deletedItem = consumables.splice(i, 1)
  422.                 deletedItems.push(deletedItem)
  423.                 deletedItems.forEach(deletedItem => {
  424.                     setTimeout(function(){
  425.                         consumables.splice(i, 0, ...deletedItem)
  426.                     }, 5000)
  427.            
  428.                 })
  429.                 deletedItems.pop(deletedItem)
  430.                
  431.             }
  432.         }
  433.  
  434.     }
  435.    
  436.     boundaries.forEach(boundary => {
  437.         boundary.draw()
  438.        
  439.         if(player.position.y + player.velocity.y
  440.             <=
  441.             boundary.position.y + boundary.height &&
  442.         player.position.x + player.width + player.velocity.x
  443.             >=
  444.             boundary.position.x &&
  445.         player.position.y + player.height + player.velocity.y
  446.             >=
  447.             boundary.position.y &&
  448.         player.position.x + player.velocity.x
  449.             <=
  450.             boundary.position.x + boundary.width){
  451.                 console.log('colliding')
  452.                 player.velocity.x = 0
  453.                 player.velocity.y = 0
  454.         }
  455.         projectiles.forEach((projectile, index) => {
  456.             if(projectile.position.y
  457.                 <=
  458.                 boundary.position.y + boundary.height &&
  459.                 projectile.position.y + projectile.height
  460.                 >=
  461.                 boundary.position.y &&
  462.                 projectile.position.x + projectile.width
  463.                 >=
  464.                 boundary.position.x &&
  465.                 projectile.position.x
  466.                 <=
  467.                 boundary.position.x + boundary.width) {
  468.                     projectiles.splice(index, 1)
  469.             }
  470.         })
  471.     })
  472.     player.update()
  473.     enemy.update()
  474.    
  475.  
  476.     //projectile update, collision detection and garbage collection
  477.     projectiles.forEach((projectile, index) => {
  478.         const distance = Math.sqrt(Math.pow((player.position.x-projectile.position.x), 2) +
  479.         Math.pow((player.position.y-projectile.position.y), 2))
  480.        
  481.         if(projectile.color === 'green' && distance >= 250 && !player.powerUp){
  482.             projectiles.splice(index, 1)
  483.         }
  484.         if(projectile.color === 'green' && player.powerUp && distance >= 500){
  485.             projectiles.splice(index, 1)
  486.         }
  487.  
  488.         if(projectile.position.y + projectile.height <= 0){
  489.             projectiles.splice(index, 1)
  490.         } else if(projectile.position.y <= enemy.position.y + enemy.height &&
  491.             projectile.position.y + projectile.height >= enemy.position.y &&
  492.             projectile.position.x + projectile.width >= enemy.position.x &&
  493.             projectile.position.x <= enemy.position.x + enemy.width){
  494.             console.log('enemy hit!')
  495.             enemy.health -= projectile.damage
  496.             if(enemy.health <= 0){
  497.                 enemy.health = 0
  498.                 console.log('enemy is dead!')
  499.             }
  500.             projectiles.splice(index, 1)
  501.  
  502.         }
  503.         else if(projectile.color === 'red' && distance >= 600){
  504.             projectiles.splice(index, 1)
  505.         }
  506.         else {
  507.             projectile.update()
  508.         }
  509.        
  510.     })
  511.     player.velocity.x = 0
  512.     player.velocity.y = 0
  513.     enemy.velocity.x = 0
  514.     enemy.velocity.y = 0
  515.  
  516.     // player movement
  517.     if(keys.a.pressed ){
  518.         player.velocity.x = -3
  519.     } if(keys.d.pressed ){
  520.         player.velocity.x = 3
  521.     } if(keys.s.pressed ){
  522.         player.velocity.y = 3
  523.     } if(keys.w.pressed ){
  524.         player.velocity.y = -3
  525.     }
  526.     // enemy movement
  527.     if(keys.left.pressed ){
  528.         enemy.velocity.x = -2
  529.     } if(keys.right.pressed){
  530.         enemy.velocity.x = 2
  531.     } if(keys.down.pressed){
  532.         enemy.velocity.y = 2
  533.     } if(keys.up.pressed ){
  534.         enemy.velocity.y = -2
  535.    
  536.     }
  537.    
  538.     dash()
  539.    
  540.  
  541. }
  542. animate()
  543.  
  544.  
  545. function dash() {
  546.     //player dash
  547.     if(keys.shift.pressed && keys.a.pressed){
  548.         player.velocity.x = -10
  549.         player.isDashing = true
  550.        
  551.         setTimeout(() => {
  552.             keys.shift.pressed = false
  553.             player.isDashing = false
  554.            
  555.         }, 300)
  556.     } if(keys.shift.pressed && keys.d.pressed){
  557.         player.velocity.x = 10
  558.         player.isDashing = true
  559.         if(player.isDashing = true) {
  560.             keys.d.pressed = true
  561.         }
  562.         setTimeout(() => {
  563.             keys.shift.pressed = false
  564.             player.isDashing = false
  565.            
  566.         }, 300)
  567.     } if(keys.shift.pressed && keys.s.pressed){
  568.         player.velocity.y = 10
  569.         player.isDashing = true
  570.         if(player.isDashing = true) {
  571.             keys.s.pressed = true
  572.         }
  573.         setTimeout(() => {
  574.             keys.shift.pressed = false
  575.             player.isDashing = false
  576.            
  577.         }, 300)
  578.     } if(keys.shift.pressed && keys.w.pressed){
  579.         player.velocity.y = -10
  580.         player.isDashing = true
  581.         if(player.isDashing = true) {
  582.             keys.w.pressed = true
  583.         }
  584.         setTimeout(() => {
  585.             keys.shift.pressed = false
  586.             player.isDashing = false
  587.            
  588.         }, 300)
  589.     }
  590.    
  591.    
  592. }
  593.  
  594.  
  595. window.addEventListener('keydown', (event) => {
  596.     event.preventDefault()
  597.     console.log(event)
  598.    
  599.         if(event.code == 'KeyD'){
  600.             keys.d.pressed = true
  601.         player.lastKey = 'd'
  602.         }
  603.         if(event.code == 'KeyA'){
  604.             keys.a.pressed = true
  605.         player.lastKey = 'a'
  606.         }
  607.         if(event.code == 'KeyS'){
  608.             keys.s.pressed = true
  609.         player.lastKey = 's'
  610.         }
  611.         if(event.code == 'KeyW'){
  612.             keys.w.pressed = true
  613.         player.lastKey = 'w'
  614.         }  
  615.      switch (event.key)  {
  616.         case 'ArrowRight':
  617.         keys.right.pressed = true
  618.         enemy.lastKey = 'right'
  619.         break
  620.         case 'ArrowLeft':
  621.         keys.left.pressed = true
  622.         enemy.lastKey = 'left'
  623.         break
  624.         case 'ArrowDown':
  625.         keys.down.pressed = true
  626.         enemy.lastKey = 'down'
  627.         break
  628.         case 'ArrowUp':
  629.         keys.up.pressed = true
  630.         enemy.lastKey = 'up'
  631.         break
  632.  
  633.         case 'Shift':
  634.         keys.shift.pressed = true
  635.         player.lastKey = 'Shift'
  636.         break
  637.         case '1':
  638.         keys.one.pressed = true
  639.         keys.two.pressed = false
  640.         keys.three.pressed = false
  641.         break
  642.         case '2':
  643.         keys.two.pressed = true
  644.         keys.one.pressed = false
  645.         keys.three.pressed = false
  646.         break
  647.         case '3':
  648.         keys.three.pressed = true
  649.         keys.one.pressed = false
  650.         keys.two.pressed = false
  651.         break
  652.        
  653.  
  654.        
  655.     }
  656.     socket.emit("player1", player)
  657. })
  658. window.addEventListener('keyup', (event) => {
  659.     event.preventDefault()
  660.    
  661.         if(event.code == 'KeyD'){
  662.             keys.d.pressed = false
  663.            
  664.         }
  665.         if(event.code == 'KeyA'){
  666.             keys.a.pressed = false
  667.        
  668.         }
  669.         if(event.code == 'KeyS'){
  670.             keys.s.pressed = false
  671.        
  672.         }
  673.         if(event.code == 'KeyW'){
  674.             keys.w.pressed = false
  675.      
  676.     }
  677.     switch (event.key) {
  678.        
  679.         //case 'Shift':
  680.         //keys.shift.pressed = false
  681.         //lastKey = 'Shift'
  682.         //break
  683.  
  684.         case 'ArrowRight':
  685.         keys.right.pressed = false
  686.         break
  687.         case 'ArrowLeft':
  688.         keys.left.pressed = false
  689.         break
  690.         case 'ArrowDown':
  691.         keys.down.pressed = false
  692.         break
  693.         case 'ArrowUp':
  694.         keys.up.pressed = false
  695.         break
  696.        
  697.  
  698.     }
  699.     socket.emit("player1", player)
  700. })
  701.  
  702.  
  703. window.addEventListener('click', throttle(() => {
  704.     if(keys.one.pressed){
  705.         fire()
  706.     }
  707.     socket.emit("player1", player)
  708. }, 500))
  709. window.addEventListener('click', throttle(() => {
  710.     if(keys.two.pressed){
  711.         toxic()
  712.     }
  713.     socket.emit("player1", player)
  714. }, 1000))
  715. window.addEventListener('click', throttle(() => {
  716.     if(keys.three.pressed){
  717.         ice()
  718.     }
  719.     socket.emit("player1", player)
  720. }, 1500))
  721.  
  722. function fire(){
  723.     const targetX = player.mouseX - (player.position.x + player.width / 2)
  724.     const targetY = player.mouseY - (player.position.y + player.height / 2)
  725.     player.rotation = Math.atan2(targetY, targetX)
  726.     player.targetVelocityX = Math.cos(player.rotation)
  727.     player.targetVelocityY = Math.sin(player.rotation)
  728.  
  729.    
  730.             if(player.powerUp){
  731.                 projectiles.push(new Projectile({
  732.                     position: {
  733.                         x: player.position.x + player.width / 2,
  734.                         y: player.position.y + player.height / 2
  735.                     },
  736.                     velocity: {
  737.                         x: player.targetVelocityX * 10,
  738.                         y: player.targetVelocityY * 10
  739.                     },
  740.                 }, 7, 7, 75, 'red'))
  741.             } else {
  742.                 projectiles.push(new Projectile({
  743.                     position: {
  744.                         x: player.position.x + player.width / 2,
  745.                         y: player.position.y + player.height / 2
  746.                     },
  747.                     velocity: {
  748.                         x: player.targetVelocityX * 10,
  749.                         y: player.targetVelocityY * 10
  750.                     },
  751.                 }, 5, 5, 50, 'red'))
  752.             }
  753.  
  754.    
  755. }
  756. function toxic(){
  757.    
  758.     const targetX = player.mouseX - (player.position.x + player.width / 2)
  759.     const targetY = player.mouseY - (player.position.y + player.height / 2)
  760.     player.rotation = Math.atan2(targetY, targetX)
  761.     player.targetVelocityX = Math.cos(player.rotation)
  762.     player.targetVelocityY = Math.sin(player.rotation)
  763.     console.log(player.targetVelocityX)
  764.    
  765.     let toxicModifier = [-0.2, -0.1, 0, 0.1, 0.2]
  766.     let toxicObj = {}
  767.     if(player.powerUp){
  768.         toxicModifier = [-0.1, -0.05, 0, 0.05, 0.1]
  769.         for(let i = 0; i <= 5; i++){
  770.             for(let j = 0; j < toxicModifier.length; j ++){
  771.                
  772.                 player.targetVelocityX = Math.cos(player.rotation - toxicModifier[i])
  773.                 player.targetVelocityY = Math.sin(player.rotation - toxicModifier[i])
  774.             }
  775.             toxicObj = new Projectile({
  776.                 position: {
  777.                     x: player.position.x + player.width / 2,
  778.                     y: player.position.y + player.height / 2
  779.                 },
  780.                 velocity: {
  781.                     x: player.targetVelocityX * 8,
  782.                     y: player.targetVelocityY * 8
  783.                 },
  784.             }, 4, 4, 20, 'green')
  785.             projectiles.push(toxicObj)
  786.         }
  787.     } else {
  788.         for(let i = 0; i <= 5; i++){
  789.             for(let j = 0; j < toxicModifier.length; j ++){
  790.                 player.targetVelocityX = Math.cos(player.rotation - toxicModifier[i])
  791.                 player.targetVelocityY = Math.sin(player.rotation - toxicModifier[i])
  792.             }
  793.             toxicObj = new Projectile({
  794.                 position: {
  795.                     x: player.position.x + player.width / 2,
  796.                     y: player.position.y + player.height / 2
  797.                 },
  798.                 velocity: {
  799.                     x: player.targetVelocityX * 8,
  800.                     y: player.targetVelocityY * 8
  801.                 },
  802.             }, 4, 4, 20, 'green')
  803.             projectiles.push(toxicObj)
  804.         }
  805.     }      
  806.                
  807. }
  808. function ice(){
  809.    
  810.     const targetX = player.mouseX - (player.position.x + player.width / 2)
  811.     const targetY = player.mouseY - (player.position.y + player.height / 2)
  812.     player.rotation = Math.atan2(targetY, targetX)
  813.     player.targetVelocityX = Math.cos(player.rotation)
  814.     player.targetVelocityY = Math.sin(player.rotation)
  815.    
  816.     if(player.powerUp){
  817.         projectiles.push(new Projectile({
  818.             position: {
  819.                 x: player.position.x + player.width / 2,
  820.                 y: player.position.y + player.height / 2
  821.             },
  822.             velocity: {
  823.                 x: player.targetVelocityX * 20,
  824.                 y: player.targetVelocityY * 20
  825.             }
  826.         }, 3, 3, 200, 'blue'))
  827.    
  828.     } else {
  829.        
  830.         projectiles.push(new Projectile({
  831.             position: {
  832.                 x: player.position.x + player.width / 2,
  833.                 y: player.position.y + player.height / 2
  834.             },
  835.             velocity: {
  836.                 x: player.targetVelocityX * 15,
  837.                 y: player.targetVelocityY * 15
  838.             }
  839.         }, 3, 3, 150, 'blue'))
  840.     }
  841. }
  842. window.addEventListener('mousemove', (event) => {
  843.    
  844.     player.mouseX = event.offsetX
  845.     player.mouseY = event.offsetY
  846.    
  847.  
  848. })
  849.  
Advertisement
Add Comment
Please, Sign In to add comment