GabeLinux

Web - 6 - 3

Feb 13th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // declare all our variables
  2. const WIDTH = 720
  3. const HEIGHT = 480
  4. const GROUNDHEIGHT = HEIGHT - 40
  5.  
  6. const canvas = document.querySelector('#game')
  7. canvas.width = WIDTH
  8. canvas.height = HEIGHT
  9.  
  10. const ctx = canvas.getContext('2d')
  11.  
  12. const imgLoader = createImageLoader()
  13.  
  14. const playerAnimations = Object.assign({},
  15.   createAnimation('run', 'images/kenny/run', 6, 7),
  16.   createAnimation('idle', 'images/kenny/idle', 2, 25),
  17.   createAnimation('jump', 'images/kenny/jump', 1, 0, false),
  18.   createAnimation('fall', 'images/kenny/fall', 1, 0, false),
  19.   createAnimation('dead', 'images/kenny/hit', 1, 0, false)
  20. )
  21.  
  22. imgLoader.loadAll()
  23.   .then(() => { gameStart() })
  24.  
  25. let prevLoop
  26. let prevDraws = []
  27. let player
  28.  
  29. // create an image loader that will use promises to load all of the given images
  30. function createImageLoader() {
  31.   let l = {}
  32.   l.images = []
  33.  
  34.   l.loadImage = (src) => {
  35.     let img = new Image()
  36.     img.src = src
  37.  
  38.     let p = new Promise((resolve, reject) => {
  39.       img.addEventListener('load', () => { resolve(img) })
  40.       img.addEventListener('error', (err) => { reject(new Error('Image failed to load')) })
  41.     })
  42.  
  43.     l.images.push(p)
  44.  
  45.     return img
  46.   }
  47.  
  48.   l.loadAll = () => {
  49.     return Promise.all(l.images)
  50.       .then((images) => l.images = images)
  51.       .catch((error) => { console.error(error) })
  52.   }
  53.  
  54.   return l
  55. }
  56.  
  57. // make an animation object with our parameters and loaded animation frames
  58. function createAnimation(name, path, length, time, loop = true) {
  59.   let a = {}
  60.   a[name] = {}
  61.  
  62.   a[name].frames = new Array(length).fill(null).map((val, indx) => imgLoader.loadImage(`${path}/${indx}.png`))
  63.   a[name].frameTime = time
  64.   a[name].loop = loop
  65.  
  66.   return a
  67. }
  68.  
  69. // make a player object that will animate and jump
  70. function createPlayer(animations, x, scale = 1) {
  71.   let p = {}
  72.  
  73.   p.state = 'run'
  74.  
  75.   p.frame = 0
  76.   p.frameTicks = 0
  77.   p.animations = animations
  78.  
  79.   p.frameTime = p.animations[p.state].frameTime
  80.   p.image = p.animations[p.state]['frames'][p.frame]
  81.  
  82.   p.scale = scale
  83.   p.width = p.image.width * p.scale
  84.   p.height = p.image.height * p.scale
  85.   p.x = x
  86.   p.y = GROUNDHEIGHT - p.height
  87.  
  88.   p.getRight = () => {
  89.     return p.x + p.width
  90.   }
  91.  
  92.   p.getBottom = () => {
  93.     return p.y + p.height
  94.   }
  95.  
  96.   p.updateImage = () => {
  97.     const bottom = p.getBottom()
  98.     p.image = p.animations[p.state]['frames'][p.frame]
  99.     p.width = p.image.width * p.scale
  100.     p.height = p.image.height * p.scale
  101.     p.y = bottom - p.height
  102.   }
  103.  
  104.   p.updateAnimation = (delta) => {
  105.     p.frameTicks += 0.06 * delta
  106.  
  107.     if ((p.animations[p.state].loop || p.frame < p.animations[p.state]['frames'].length - 1) && p.frameTicks >= p.frameTime) {
  108.       p.frame++
  109.       p.frameTicks = 0
  110.  
  111.       if (p.frame === p.animations[p.state]['frames'].length)
  112.         p.frame = 0
  113.  
  114.       p.updateImage()
  115.     }
  116.   }
  117.  
  118.   p.setState = (state) => {
  119.     if (state === p.state) return
  120.  
  121.     p.frame = 0
  122.     p.frameTicks = 0
  123.     p.state = state
  124.     p.frameTime = p.animations[p.state].frameTime
  125.  
  126.     p.updateImage()
  127.   }
  128.  
  129.   p.update = (delta) => {
  130.     p.updateAnimation(delta)
  131.   }
  132.  
  133.   p.draw = () => {
  134.     ctx.drawImage(p.image, p.x, p.y, p.width, p.height)
  135.  
  136.     return [{
  137.       x: p.x,
  138.       y: p.y,
  139.       width: p.width,
  140.       height: p.height
  141.     }]
  142.   }
  143.  
  144.   return p
  145. }
  146.  
  147.  
  148. // when all images have loaded, create our needed objects and start drawing
  149. function gameStart() {
  150.   ctx.fillStyle = '#FFFFFF'
  151.   ctx.fillRect(0, 0, WIDTH, HEIGHT)
  152.  
  153.   player = createPlayer(playerAnimations, 100, 0.25)
  154.  
  155.   prevLoop = performance.now()
  156.   window.requestAnimationFrame(gameLoop)
  157. }
  158.  
  159.  
  160. // update all our game objects
  161. function update(delta) {
  162.   player.update(delta)
  163. }
  164.  
  165. // clear the areas that were drawn last frame and draw updated objects
  166. function draw() {
  167.   prevDraws.forEach((array) => {
  168.     array.forEach((val) => {
  169.       ctx.fillStyle = 'white'
  170.       ctx.fillRect(
  171.         Math.round(val.x),
  172.         Math.round(val.y),
  173.         Math.round(val.width),
  174.         Math.round(val.height)
  175.       )
  176.     })
  177.   })
  178.  
  179.   prevDraws = []
  180.  
  181.   prevDraws.push(player.draw())
  182. }
  183.  
  184. // sync our game loop to the window framerate, then update and draw each frame
  185. function gameLoop(time) {
  186.   const delta = time - prevLoop
  187.   prevLoop = time
  188.  
  189.   update(delta)
  190.   draw()
  191.  
  192.   window.requestAnimationFrame(gameLoop)
  193. }
Advertisement
Add Comment
Please, Sign In to add comment