From 8119e6e9fa5f690826e7eccc22f27988705c5102 Mon Sep 17 00:00:00 2001 From: Jennifer Creighton Date: Sat, 16 Jun 2018 21:24:09 -0400 Subject: [PATCH] ccccchanges --- Animation.js | 8 ---- Egg.js | 61 ++++++++++++------------- Game.js | 24 ++++++++++ Tamagotchi.js | 88 ++++++++++++++++++++++++++++++++++++ images/TamagotchiSprite.png | Bin 0 -> 5498 bytes index.html | 4 +- styles.css | 4 +- 7 files changed, 148 insertions(+), 41 deletions(-) create mode 100644 Game.js create mode 100644 Tamagotchi.js create mode 100644 images/TamagotchiSprite.png diff --git a/Animation.js b/Animation.js index 5aac49f..0654bb4 100644 --- a/Animation.js +++ b/Animation.js @@ -1,8 +1,4 @@ class Animation { - constructor() { - - } - animate(draw) { return new Promise((resolve, reject) => { var animation = () => { @@ -17,8 +13,4 @@ class Animation { requestAnimationFrame(animation); }); } - - clear() { - /* noop */ - } } diff --git a/Egg.js b/Egg.js index 7c8aeb5..657b75a 100644 --- a/Egg.js +++ b/Egg.js @@ -6,35 +6,47 @@ class Egg { this.sprite.height = 120; this.sprite.width = 900; this.sprite.frameCount = 5; + this.sprite.frameHeight = 120; this.sprite.frameWidth = this.sprite.width / this.sprite.frameCount; this.sprite.startFrame = 2; - this.currentFrame = 2; - this.increment = 1; + this.clear = this.clear.bind(this); this.bounce = this.bounce.bind(this); + this.hatch = this.hatch.bind(this); + } + + clear() { + const { + frameWidth, + frameHeight, + } = this.sprite; + + context.clearRect(0, 0, frameWidth, frameHeight); } bounce(maxBounce) { const { frameCount, frameWidth, - height, + frameHeight, image, startFrame, } = this.sprite; var bounce = 0; + var increment = 1; + var currentFrame = 2 return (resolve) => { - context.clearRect(0, 0, frameWidth, height); - context.drawImage(image, this.currentFrame * frameWidth, 0, frameWidth, height, 0, 0, frameWidth, height); + this.clear(); + context.drawImage(image, currentFrame * frameWidth, 0, frameWidth, frameHeight, 0, 0, frameWidth, frameHeight); - if (this.currentFrame === frameCount - 1) { - this.increment = -1; + if (currentFrame === frameCount - 1) { + increment = -1; } - if (this.currentFrame === startFrame) { - this.increment = 1; + if (currentFrame === startFrame) { + increment = 1; bounce++; } @@ -43,44 +55,31 @@ class Egg { return true; } - this.currentFrame += this.increment; + currentFrame += increment; }; } - hatch(resolve) { + hatch() { const { frameCount, frameWidth, - height, + frameHeight, image, startFrame, } = this.sprite; + var currentFrame = 2 + return (resolve) => { - context.clearRect(0, 0, frameWidth, height); - context.drawImage(image, this.currentFrame * frameWidth, 0, frameWidth, height, 0, 0, frameWidth, height); + this.clear(); + context.drawImage(image, currentFrame * frameWidth, 0, frameWidth, frameHeight, 0, 0, frameWidth, frameHeight); - if (this.currentFrame === 0) { + if (currentFrame === 0) { resolve(); return true; } - this.currentFrame--; + currentFrame--; }; } } - -var { animate } = new Animation(); -var egg = new Egg(); -// Get canvas -var canvas = document.querySelector('.animation'); -canvas.width = 180; -canvas.height = 120; -var context = canvas.getContext('2d'); -var chooseButton = document.querySelector('.choose'); - -chooseButton.addEventListener('click', () => { - animate(egg.bounce(3)).then(() => { - animate(egg.hatch()) - }); -}); diff --git a/Game.js b/Game.js new file mode 100644 index 0000000..04e93ec --- /dev/null +++ b/Game.js @@ -0,0 +1,24 @@ +var { animate } = new Animation(); +var egg = new Egg(); +var tamagotchi = new Tamagotchi(); +// Get canvas +var canvas = document.querySelector('.animation'); +canvas.width = 180; +canvas.height = 120; +var context = canvas.getContext('2d'); +var chooseButton = document.querySelector('.choose'); + +chooseButton.addEventListener('click', () => { + animate(egg.bounce(3)) + .then(() => { + console.log('Bouncing complete!'); + console.log('Time to hatch!'); + return animate(egg.hatch()); + }).then(() => { + egg.clear(); + canvas.width = tamagotchi.sprite.frameHeight; + canvas.height = tamagotchi.sprite.frameWidth; + console.log('BABY TAMAGOTCHI!') + animate(tamagotchi.bounce()); + }); +}); \ No newline at end of file diff --git a/Tamagotchi.js b/Tamagotchi.js new file mode 100644 index 0000000..b42c8e0 --- /dev/null +++ b/Tamagotchi.js @@ -0,0 +1,88 @@ +class Tamagotchi { + constructor() { + this.sprite = {}; + this.sprite.image = new Image(); + this.sprite.image.src = 'images/TamagotchiSprite.png'; + this.sprite.height = 480; + this.sprite.width = 360; + this.sprite.rows = 4; + this.sprite.columns = 3; + this.sprite.frameCount = 3; + this.sprite.frameWidth = this.sprite.width / this.sprite.columns; + this.sprite.frameHeight = this.sprite.height / this.sprite.rows; + + this.sprite.dislike = 0; + this.sprite.jump = 1; + this.sprite.eat = 2; + this.sprite.chill = 3; + + this.eat = 0; + this.maxEat = 10; + } + + clear() { + const { + frameWidth, + frameHeight, + } = this.sprite; + + context.clearRect(0, 0, frameWidth, frameHeight); + } + + dislike() { + + } + + jump() { + + } + + eat() { + + } + + bounce() { + const { + chill, + image, + frameCount, + frameHeight, + frameWidth, + } = this.sprite; + + var bounce = 0; + var maxBounce = 2; + var increment = 1; + var currentFrame = 0; + + return (resolve) => { + this.clear(); + context.drawImage(image, currentFrame * frameWidth, chill * frameHeight, frameWidth, frameHeight, 0, 0, frameWidth, frameHeight); + + if (currentFrame === frameCount - 1) { + increment = -1; + } + + if (currentFrame === 0) { + increment = 1; + bounce++; + } + + if (bounce == maxBounce) { + resolve(); + return true; + } + + currentFrame += increment; + }; + + + // return (resolve) => { + // goes up & down + // moves left + // goes up & down + // moves right + // repeat + // }; + } +} \ No newline at end of file diff --git a/images/TamagotchiSprite.png b/images/TamagotchiSprite.png new file mode 100644 index 0000000000000000000000000000000000000000..a8e0225e5191340cdbb9f9b681d758fbb1c928ae GIT binary patch literal 5498 zcmds5X;c&0woVmA2#64Hf)*rzVGu#lMnh<%5R_2~$Rr4)uR~;LKtx0)E8x_O;uCOS z2-*rVi3Y?5f#`Kafiwa(A`qIZg2>dDNkO=~?pn9sTJQDi*Xv!of4mqowcQF zBr?=B7}ja4K;`fZM&=tj+FAq7-rVDR;e9YA_KlO8-5x2K@ns6T*ZKL)Y2yS1K`|Pg z@&DAAHX0qAS)hJ&zM!veq0LOkt0DMVmR@MU z5DYfC^Dx@VWp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Sr$>KH-X_l)*Cbk;m|!W->bd8PoOENG z&eVDis5rINtnYWLV@}u+I2*Kj@E$oGoVx@hS;Ke#;~GER;z7CsuQ$GVZrfBsQA^Lb zg-vs>e(wu?v`J7Woq>FN9_y0%ZozSWD`t{Pr-;{7=ae=TaLdz6>BMx&+7GS5^kCKHM% z1@*#%97xJZ235$bh?zWvSIiG3yEiLY_W-mRYWe zSnC$>LN)w~|GVQ9z6e9^f>)UHxZGaa0fOw60rvJxGUu0ER^YWOlm#>GVpu@kg-+^tDc&pSXVl=D(Z zMF^LhDPbiKcGv|9IBPnHd>#kR^GeNu=Ot$dqV7I#H#{E)>HjHc=sYBy1*4IHJ7AD8%T3m5h zdEfU*1`au9f1AJKb1h5z(?|G~U?Eys6;(AWRpw@;U%e$MdOY_Fg^8=%SYO`g=r`iG z6Lo>HGn%;Mg6Nb5;oW|_-L1Z~c`o0q^6(46j5_U)ED%GmW~_s}aeV^*xA2bEfsSDE z)2Gho)ymAd?3UWN!k<-M`rN@SJEX0{J-cl{YEnYok%lNbuvttEZH}!ZsNYp?Tv+R* zUgMy) + + - + \ No newline at end of file diff --git a/styles.css b/styles.css index c025db7..ec6c728 100644 --- a/styles.css +++ b/styles.css @@ -46,6 +46,8 @@ } .animation { + left: 0; + right: 0; bottom: 4%; - left: 20%; + margin: 0 auto; }