123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173 |
- // Copyright (c) 2014 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- /**
- * T-Rex runner.
- * @param {string} outerContainerId Outer containing element id.
- * @param {!Object=} opt_config
- * @constructor
- * @implements {EventListener}
- * @export
- */
- function Runner(outerContainerId, opt_config) {
- // Singleton
- if (Runner.instance_) {
- return Runner.instance_;
- }
- Runner.instance_ = this;
- this.outerContainerEl = document.querySelector(outerContainerId);
- this.containerEl = null;
- this.snackbarEl = null;
- // A div to intercept touch events. Only set while (playing && useTouch).
- this.touchController = null;
- this.config = opt_config || Object.assign(Runner.config, Runner.normalConfig);
- // Logical dimensions of the container.
- this.dimensions = Runner.defaultDimensions;
- this.gameType = null;
- Runner.spriteDefinition = Runner.spriteDefinitionByType['original'];
- this.altGameImageSprite = null;
- this.altGameModeActive = false;
- this.altGameModeFlashTimer = null;
- this.fadeInTimer = 0;
- this.canvas = null;
- this.canvasCtx = null;
- this.tRex = null;
- this.distanceMeter = null;
- this.distanceRan = 0;
- this.highestScore = 0;
- this.syncHighestScore = false;
- this.time = 0;
- this.runningTime = 0;
- this.msPerFrame = 1000 / FPS;
- this.currentSpeed = this.config.SPEED;
- Runner.slowDown = false;
- this.obstacles = [];
- this.activated = false; // Whether the easter egg has been activated.
- this.playing = false; // Whether the game is currently in play state.
- this.crashed = false;
- this.paused = false;
- this.inverted = false;
- this.invertTimer = 0;
- this.resizeTimerId_ = null;
- this.playCount = 0;
- // Sound FX.
- this.audioBuffer = null;
- /** @type {Object} */
- this.soundFx = {};
- this.generatedSoundFx = null;
- // Global web audio context for playing sounds.
- this.audioContext = null;
- // Images.
- this.images = {};
- this.imagesLoaded = 0;
- // Gamepad state.
- this.pollingGamepads = false;
- this.gamepadIndex = undefined;
- this.previousGamepad = null;
- if (this.isDisabled()) {
- this.setupDisabledRunner();
- } else {
- if (Runner.isAltGameModeEnabled()) {
- this.initAltGameType();
- Runner.gameType = this.gameType;
- }
- this.loadImages();
- window['initializeEasterEggHighScore'] =
- this.initializeHighScore.bind(this);
- }
- }
- /**
- * Default game width.
- * @const
- */
- const DEFAULT_WIDTH = 600;
- /**
- * Frames per second.
- * @const
- */
- const FPS = 60;
- /** @const */
- const IS_HIDPI = window.devicePixelRatio > 1;
- /** @const */
- const IS_IOS = /CriOS/.test(window.navigator.userAgent);
- /** @const */
- const IS_MOBILE = /Android/.test(window.navigator.userAgent) || IS_IOS;
- /** @const */
- const IS_RTL = document.querySelector('html').dir == 'rtl';
- /** @const */
- const ARCADE_MODE_URL = 'chrome://dino/';
- /** @const */
- const RESOURCE_POSTFIX = 'offline-resources-';
- /** @const */
- const A11Y_STRINGS = {
- ariaLabel: 'dinoGameA11yAriaLabel',
- description: 'dinoGameA11yDescription',
- gameOver: 'dinoGameA11yGameOver',
- highScore: 'dinoGameA11yHighScore',
- jump: 'dinoGameA11yJump',
- started: 'dinoGameA11yStartGame',
- speedLabel: 'dinoGameA11ySpeedToggle',
- };
- /**
- * Default game configuration.
- * Shared config for all versions of the game. Additional parameters are
- * defined in Runner.normalConfig and Runner.slowConfig.
- */
- Runner.config = {
- AUDIOCUE_PROXIMITY_THRESHOLD: 190,
- AUDIOCUE_PROXIMITY_THRESHOLD_MOBILE_A11Y: 250,
- BG_CLOUD_SPEED: 0.2,
- BOTTOM_PAD: 10,
- // Scroll Y threshold at which the game can be activated.
- CANVAS_IN_VIEW_OFFSET: -10,
- CLEAR_TIME: 3000,
- CLOUD_FREQUENCY: 0.5,
- FADE_DURATION: 1,
- FLASH_DURATION: 1000,
- GAMEOVER_CLEAR_TIME: 1200,
- INITIAL_JUMP_VELOCITY: 12,
- INVERT_FADE_DURATION: 12000,
- MAX_BLINK_COUNT: 3,
- MAX_CLOUDS: 6,
- MAX_OBSTACLE_LENGTH: 3,
- MAX_OBSTACLE_DUPLICATION: 2,
- RESOURCE_TEMPLATE_ID: 'audio-resources',
- SPEED: 6,
- SPEED_DROP_COEFFICIENT: 3,
- ARCADE_MODE_INITIAL_TOP_POSITION: 35,
- ARCADE_MODE_TOP_POSITION_PERCENT: 0.1,
- };
- Runner.normalConfig = {
- ACCELERATION: 0.001,
- AUDIOCUE_PROXIMITY_THRESHOLD: 190,
- AUDIOCUE_PROXIMITY_THRESHOLD_MOBILE_A11Y: 250,
- GAP_COEFFICIENT: 0.6,
- INVERT_DISTANCE: 700,
- MAX_SPEED: 13,
- MOBILE_SPEED_COEFFICIENT: 1.2,
- SPEED: 6,
- };
- Runner.slowConfig = {
- ACCELERATION: 0.0005,
- AUDIOCUE_PROXIMITY_THRESHOLD: 170,
- AUDIOCUE_PROXIMITY_THRESHOLD_MOBILE_A11Y: 220,
- GAP_COEFFICIENT: 0.3,
- INVERT_DISTANCE: 350,
- MAX_SPEED: 9,
- MOBILE_SPEED_COEFFICIENT: 1.5,
- SPEED: 4.2,
- };
- /**
- * Default dimensions.
- */
- Runner.defaultDimensions = {
- WIDTH: DEFAULT_WIDTH,
- HEIGHT: 150,
- };
- /**
- * CSS class names.
- * @enum {string}
- */
- Runner.classes = {
- ARCADE_MODE: 'arcade-mode',
- CANVAS: 'runner-canvas',
- CONTAINER: 'runner-container',
- CRASHED: 'crashed',
- ICON: 'icon-offline',
- INVERTED: 'inverted',
- SNACKBAR: 'snackbar',
- SNACKBAR_SHOW: 'snackbar-show',
- TOUCH_CONTROLLER: 'controller',
- };
- /**
- * Sound FX. Reference to the ID of the audio tag on interstitial page.
- * @enum {string}
- */
- Runner.sounds = {
- BUTTON_PRESS: 'offline-sound-press',
- HIT: 'offline-sound-hit',
- SCORE: 'offline-sound-reached',
- };
- /**
- * Key code mapping.
- * @enum {Object}
- */
- Runner.keycodes = {
- JUMP: {'38': 1, '32': 1}, // Up, spacebar
- DUCK: {'40': 1}, // Down
- RESTART: {'13': 1}, // Enter
- };
- /**
- * Runner event names.
- * @enum {string}
- */
- Runner.events = {
- ANIM_END: 'webkitAnimationEnd',
- CLICK: 'click',
- KEYDOWN: 'keydown',
- KEYUP: 'keyup',
- POINTERDOWN: 'pointerdown',
- POINTERUP: 'pointerup',
- RESIZE: 'resize',
- TOUCHEND: 'touchend',
- TOUCHSTART: 'touchstart',
- VISIBILITY: 'visibilitychange',
- BLUR: 'blur',
- FOCUS: 'focus',
- LOAD: 'load',
- GAMEPADCONNECTED: 'gamepadconnected',
- };
- Runner.prototype = {
- /**
- * Initialize alternative game type.
- */
- initAltGameType() {
- if (GAME_TYPE.length > 0) {
- this.gameType = loadTimeData && loadTimeData.valueExists('altGameType') ?
- GAME_TYPE[parseInt(loadTimeData.getValue('altGameType'), 10) - 1] :
- '';
- }
- },
- /**
- * Whether the easter egg has been disabled. CrOS enterprise enrolled devices.
- * @return {boolean}
- */
- isDisabled() {
- return loadTimeData && loadTimeData.valueExists('disabledEasterEgg');
- },
- /**
- * For disabled instances, set up a snackbar with the disabled message.
- */
- setupDisabledRunner() {
- this.containerEl = document.createElement('div');
- this.containerEl.className = Runner.classes.SNACKBAR;
- this.containerEl.textContent = loadTimeData.getValue('disabledEasterEgg');
- this.outerContainerEl.appendChild(this.containerEl);
- // Show notification when the activation key is pressed.
- document.addEventListener(Runner.events.KEYDOWN, function(e) {
- if (Runner.keycodes.JUMP[e.keyCode]) {
- this.containerEl.classList.add(Runner.classes.SNACKBAR_SHOW);
- document.querySelector('.icon').classList.add('icon-disabled');
- }
- }.bind(this));
- },
- /**
- * Setting individual settings for debugging.
- * @param {string} setting
- * @param {number|string} value
- */
- updateConfigSetting(setting, value) {
- if (setting in this.config && value !== undefined) {
- this.config[setting] = value;
- switch (setting) {
- case 'GRAVITY':
- case 'MIN_JUMP_HEIGHT':
- case 'SPEED_DROP_COEFFICIENT':
- this.tRex.config[setting] = value;
- break;
- case 'INITIAL_JUMP_VELOCITY':
- this.tRex.setJumpVelocity(value);
- break;
- case 'SPEED':
- this.setSpeed(/** @type {number} */ (value));
- break;
- }
- }
- },
- /**
- * Creates an on page image element from the base 64 encoded string source.
- * @param {string} resourceName Name in data object,
- * @return {HTMLImageElement} The created element.
- */
- createImageElement(resourceName) {
- const imgSrc = loadTimeData && loadTimeData.valueExists(resourceName) ?
- loadTimeData.getString(resourceName) :
- null;
- if (imgSrc) {
- const el =
- /** @type {HTMLImageElement} */ (document.createElement('img'));
- el.id = resourceName;
- el.src = imgSrc;
- document.getElementById('offline-resources').appendChild(el);
- return el;
- }
- return null;
- },
- /**
- * Cache the appropriate image sprite from the page and get the sprite sheet
- * definition.
- */
- loadImages() {
- let scale = '1x';
- this.spriteDef = Runner.spriteDefinition.LDPI;
- if (IS_HIDPI) {
- scale = '2x';
- this.spriteDef = Runner.spriteDefinition.HDPI;
- }
- Runner.imageSprite = /** @type {HTMLImageElement} */
- (document.getElementById(RESOURCE_POSTFIX + scale));
- if (this.gameType) {
- Runner.altGameImageSprite = /** @type {HTMLImageElement} */
- (this.createImageElement('altGameSpecificImage' + scale));
- Runner.altCommonImageSprite = /** @type {HTMLImageElement} */
- (this.createImageElement('altGameCommonImage' + scale));
- }
- Runner.origImageSprite = Runner.imageSprite;
- // Disable the alt game mode if the sprites can't be loaded.
- if (!Runner.altGameImageSprite || !Runner.altCommonImageSprite) {
- Runner.isAltGameModeEnabled = () => false;
- this.altGameModeActive = false;
- }
- if (Runner.imageSprite.complete) {
- this.init();
- } else {
- // If the images are not yet loaded, add a listener.
- Runner.imageSprite.addEventListener(Runner.events.LOAD,
- this.init.bind(this));
- }
- },
- /**
- * Load and decode base 64 encoded sounds.
- */
- loadSounds() {
- if (!IS_IOS) {
- this.audioContext = new AudioContext();
- const resourceTemplate =
- document.getElementById(this.config.RESOURCE_TEMPLATE_ID).content;
- for (const sound in Runner.sounds) {
- let soundSrc =
- resourceTemplate.getElementById(Runner.sounds[sound]).src;
- soundSrc = soundSrc.substr(soundSrc.indexOf(',') + 1);
- const buffer = decodeBase64ToArrayBuffer(soundSrc);
- // Async, so no guarantee of order in array.
- this.audioContext.decodeAudioData(buffer, function(index, audioData) {
- this.soundFx[index] = audioData;
- }.bind(this, sound));
- }
- }
- },
- /**
- * Sets the game speed. Adjust the speed accordingly if on a smaller screen.
- * @param {number=} opt_speed
- */
- setSpeed(opt_speed) {
- const speed = opt_speed || this.currentSpeed;
- // Reduce the speed on smaller mobile screens.
- if (this.dimensions.WIDTH < DEFAULT_WIDTH) {
- const mobileSpeed = Runner.slowDown ? speed :
- speed * this.dimensions.WIDTH /
- DEFAULT_WIDTH * this.config.MOBILE_SPEED_COEFFICIENT;
- this.currentSpeed = mobileSpeed > speed ? speed : mobileSpeed;
- } else if (opt_speed) {
- this.currentSpeed = opt_speed;
- }
- },
- /**
- * Game initialiser.
- */
- init() {
- // Hide the static icon.
- document.querySelector('.' + Runner.classes.ICON).style.visibility =
- 'hidden';
- this.adjustDimensions();
- this.setSpeed();
- const ariaLabel = getA11yString(A11Y_STRINGS.ariaLabel);
- this.containerEl = document.createElement('div');
- this.containerEl.setAttribute('role', IS_MOBILE ? 'button' : 'application');
- this.containerEl.setAttribute('tabindex', '0');
- this.containerEl.setAttribute('title', ariaLabel);
- this.containerEl.className = Runner.classes.CONTAINER;
- // Player canvas container.
- this.canvas = createCanvas(this.containerEl, this.dimensions.WIDTH,
- this.dimensions.HEIGHT);
- // Live region for game status updates.
- this.a11yStatusEl = document.createElement('span');
- this.a11yStatusEl.className = 'offline-runner-live-region';
- this.a11yStatusEl.setAttribute('aria-live', 'assertive');
- this.a11yStatusEl.textContent = '';
- Runner.a11yStatusEl = this.a11yStatusEl;
- // Add checkbox to slow down the game.
- this.slowSpeedCheckboxLabel = document.createElement('label');
- this.slowSpeedCheckboxLabel.className = 'slow-speed-option hidden';
- this.slowSpeedCheckboxLabel.textContent =
- getA11yString(A11Y_STRINGS.speedLabel);
- this.slowSpeedCheckbox = document.createElement('input');
- this.slowSpeedCheckbox.setAttribute('type', 'checkbox');
- this.slowSpeedCheckbox.setAttribute(
- 'title', getA11yString(A11Y_STRINGS.speedLabel));
- this.slowSpeedCheckbox.setAttribute('tabindex', '0');
- this.slowSpeedCheckbox.setAttribute('checked', 'checked');
- this.slowSpeedToggleEl = document.createElement('span');
- this.slowSpeedToggleEl.className = 'slow-speed-toggle';
- this.slowSpeedCheckboxLabel.appendChild(this.slowSpeedCheckbox);
- this.slowSpeedCheckboxLabel.appendChild(this.slowSpeedToggleEl);
- if (IS_IOS) {
- this.outerContainerEl.appendChild(this.a11yStatusEl);
- } else {
- this.containerEl.appendChild(this.a11yStatusEl);
- }
- announcePhrase(getA11yString(A11Y_STRINGS.description));
- this.generatedSoundFx = new GeneratedSoundFx();
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (this.canvas.getContext('2d'));
- this.canvasCtx.fillStyle = '#f7f7f7';
- this.canvasCtx.fill();
- Runner.updateCanvasScaling(this.canvas);
- // Horizon contains clouds, obstacles and the ground.
- this.horizon = new Horizon(this.canvas, this.spriteDef, this.dimensions,
- this.config.GAP_COEFFICIENT);
- // Distance meter
- this.distanceMeter = new DistanceMeter(this.canvas,
- this.spriteDef.TEXT_SPRITE, this.dimensions.WIDTH);
- // Draw t-rex
- this.tRex = new Trex(this.canvas, this.spriteDef.TREX);
- this.outerContainerEl.appendChild(this.containerEl);
- this.outerContainerEl.appendChild(this.slowSpeedCheckboxLabel);
- this.startListening();
- this.update();
- window.addEventListener(Runner.events.RESIZE,
- this.debounceResize.bind(this));
- // Handle dark mode
- const darkModeMediaQuery =
- window.matchMedia('(prefers-color-scheme: dark)');
- this.isDarkMode = darkModeMediaQuery && darkModeMediaQuery.matches;
- darkModeMediaQuery.addListener((e) => {
- this.isDarkMode = e.matches;
- });
- },
- /**
- * Create the touch controller. A div that covers whole screen.
- */
- createTouchController() {
- this.touchController = document.createElement('div');
- this.touchController.className = Runner.classes.TOUCH_CONTROLLER;
- this.touchController.addEventListener(Runner.events.TOUCHSTART, this);
- this.touchController.addEventListener(Runner.events.TOUCHEND, this);
- this.outerContainerEl.appendChild(this.touchController);
- },
- /**
- * Debounce the resize event.
- */
- debounceResize() {
- if (!this.resizeTimerId_) {
- this.resizeTimerId_ =
- setInterval(this.adjustDimensions.bind(this), 250);
- }
- },
- /**
- * Adjust game space dimensions on resize.
- */
- adjustDimensions() {
- clearInterval(this.resizeTimerId_);
- this.resizeTimerId_ = null;
- const boxStyles = window.getComputedStyle(this.outerContainerEl);
- const padding = Number(boxStyles.paddingLeft.substr(0,
- boxStyles.paddingLeft.length - 2));
- this.dimensions.WIDTH = this.outerContainerEl.offsetWidth - padding * 2;
- if (this.isArcadeMode()) {
- this.dimensions.WIDTH = Math.min(DEFAULT_WIDTH, this.dimensions.WIDTH);
- if (this.activated) {
- this.setArcadeModeContainerScale();
- }
- }
- // Redraw the elements back onto the canvas.
- if (this.canvas) {
- this.canvas.width = this.dimensions.WIDTH;
- this.canvas.height = this.dimensions.HEIGHT;
- Runner.updateCanvasScaling(this.canvas);
- this.distanceMeter.calcXPos(this.dimensions.WIDTH);
- this.clearCanvas();
- this.horizon.update(0, 0, true);
- this.tRex.update(0);
- // Outer container and distance meter.
- if (this.playing || this.crashed || this.paused) {
- this.containerEl.style.width = this.dimensions.WIDTH + 'px';
- this.containerEl.style.height = this.dimensions.HEIGHT + 'px';
- this.distanceMeter.update(0, Math.ceil(this.distanceRan));
- this.stop();
- } else {
- this.tRex.draw(0, 0);
- }
- // Game over panel.
- if (this.crashed && this.gameOverPanel) {
- this.gameOverPanel.updateDimensions(this.dimensions.WIDTH);
- this.gameOverPanel.draw(this.altGameModeActive, this.tRex);
- }
- }
- },
- /**
- * Play the game intro.
- * Canvas container width expands out to the full width.
- */
- playIntro() {
- if (!this.activated && !this.crashed) {
- this.playingIntro = true;
- this.tRex.playingIntro = true;
- // CSS animation definition.
- const keyframes = '@-webkit-keyframes intro { ' +
- 'from { width:' + Trex.config.WIDTH + 'px }' +
- 'to { width: ' + this.dimensions.WIDTH + 'px }' +
- '}';
- document.styleSheets[0].insertRule(keyframes, 0);
- this.containerEl.addEventListener(Runner.events.ANIM_END,
- this.startGame.bind(this));
- this.containerEl.style.webkitAnimation = 'intro .4s ease-out 1 both';
- this.containerEl.style.width = this.dimensions.WIDTH + 'px';
- this.setPlayStatus(true);
- this.activated = true;
- } else if (this.crashed) {
- this.restart();
- }
- },
- /**
- * Update the game status to started.
- */
- startGame() {
- if (this.isArcadeMode()) {
- this.setArcadeMode();
- }
- this.toggleSpeed();
- this.runningTime = 0;
- this.playingIntro = false;
- this.tRex.playingIntro = false;
- this.containerEl.style.webkitAnimation = '';
- this.playCount++;
- this.generatedSoundFx.background();
- announcePhrase(getA11yString(A11Y_STRINGS.started));
- if (Runner.audioCues) {
- this.containerEl.setAttribute('title', getA11yString(A11Y_STRINGS.jump));
- }
- // Handle tabbing off the page. Pause the current game.
- document.addEventListener(Runner.events.VISIBILITY,
- this.onVisibilityChange.bind(this));
- window.addEventListener(Runner.events.BLUR,
- this.onVisibilityChange.bind(this));
- window.addEventListener(Runner.events.FOCUS,
- this.onVisibilityChange.bind(this));
- },
- clearCanvas() {
- this.canvasCtx.clearRect(0, 0, this.dimensions.WIDTH,
- this.dimensions.HEIGHT);
- },
- /**
- * Checks whether the canvas area is in the viewport of the browser
- * through the current scroll position.
- * @return boolean.
- */
- isCanvasInView() {
- return this.containerEl.getBoundingClientRect().top >
- Runner.config.CANVAS_IN_VIEW_OFFSET;
- },
- /**
- * Enable the alt game mode. Switching out the sprites.
- */
- enableAltGameMode() {
- Runner.imageSprite = Runner.altGameImageSprite;
- Runner.spriteDefinition = Runner.spriteDefinitionByType[Runner.gameType];
- if (IS_HIDPI) {
- this.spriteDef = Runner.spriteDefinition.HDPI;
- } else {
- this.spriteDef = Runner.spriteDefinition.LDPI;
- }
- this.altGameModeActive = true;
- this.tRex.enableAltGameMode(this.spriteDef.TREX);
- this.horizon.enableAltGameMode(this.spriteDef);
- this.generatedSoundFx.background();
- },
- /**
- * Update the game frame and schedules the next one.
- */
- update() {
- this.updatePending = false;
- const now = getTimeStamp();
- let deltaTime = now - (this.time || now);
- // Flashing when switching game modes.
- if (this.altGameModeFlashTimer < 0 || this.altGameModeFlashTimer === 0) {
- this.altGameModeFlashTimer = null;
- this.tRex.setFlashing(false);
- this.enableAltGameMode();
- } else if (this.altGameModeFlashTimer > 0) {
- this.altGameModeFlashTimer -= deltaTime;
- this.tRex.update(deltaTime);
- deltaTime = 0;
- }
- this.time = now;
- if (this.playing) {
- this.clearCanvas();
- // Additional fade in - Prevents jump when switching sprites
- if (this.altGameModeActive &&
- this.fadeInTimer <= this.config.FADE_DURATION) {
- this.fadeInTimer += deltaTime / 1000;
- this.canvasCtx.globalAlpha = this.fadeInTimer;
- } else {
- this.canvasCtx.globalAlpha = 1;
- }
- if (this.tRex.jumping) {
- this.tRex.updateJump(deltaTime);
- }
- this.runningTime += deltaTime;
- const hasObstacles = this.runningTime > this.config.CLEAR_TIME;
- // First jump triggers the intro.
- if (this.tRex.jumpCount === 1 && !this.playingIntro) {
- this.playIntro();
- }
- // The horizon doesn't move until the intro is over.
- if (this.playingIntro) {
- this.horizon.update(0, this.currentSpeed, hasObstacles);
- } else if (!this.crashed) {
- const showNightMode = this.isDarkMode ^ this.inverted;
- deltaTime = !this.activated ? 0 : deltaTime;
- this.horizon.update(
- deltaTime, this.currentSpeed, hasObstacles, showNightMode);
- }
- // Check for collisions.
- let collision = hasObstacles &&
- checkForCollision(this.horizon.obstacles[0], this.tRex);
- // For a11y, audio cues.
- if (Runner.audioCues && hasObstacles) {
- const jumpObstacle =
- this.horizon.obstacles[0].typeConfig.type != 'COLLECTABLE';
- if (!this.horizon.obstacles[0].jumpAlerted) {
- const threshold = Runner.isMobileMouseInput ?
- Runner.config.AUDIOCUE_PROXIMITY_THRESHOLD_MOBILE_A11Y :
- Runner.config.AUDIOCUE_PROXIMITY_THRESHOLD;
- const adjProximityThreshold = threshold +
- (threshold * Math.log10(this.currentSpeed / Runner.config.SPEED));
- if (this.horizon.obstacles[0].xPos < adjProximityThreshold) {
- if (jumpObstacle) {
- this.generatedSoundFx.jump();
- }
- this.horizon.obstacles[0].jumpAlerted = true;
- }
- }
- }
- // Activated alt game mode.
- if (Runner.isAltGameModeEnabled() && collision &&
- this.horizon.obstacles[0].typeConfig.type == 'COLLECTABLE') {
- this.horizon.removeFirstObstacle();
- this.tRex.setFlashing(true);
- collision = false;
- this.altGameModeFlashTimer = this.config.FLASH_DURATION;
- this.runningTime = 0;
- this.generatedSoundFx.collect();
- }
- if (!collision) {
- this.distanceRan += this.currentSpeed * deltaTime / this.msPerFrame;
- if (this.currentSpeed < this.config.MAX_SPEED) {
- this.currentSpeed += this.config.ACCELERATION;
- }
- } else {
- this.gameOver();
- }
- const playAchievementSound = this.distanceMeter.update(deltaTime,
- Math.ceil(this.distanceRan));
- if (!Runner.audioCues && playAchievementSound) {
- this.playSound(this.soundFx.SCORE);
- }
- // Night mode.
- if (!Runner.isAltGameModeEnabled()) {
- if (this.invertTimer > this.config.INVERT_FADE_DURATION) {
- this.invertTimer = 0;
- this.invertTrigger = false;
- this.invert(false);
- } else if (this.invertTimer) {
- this.invertTimer += deltaTime;
- } else {
- const actualDistance =
- this.distanceMeter.getActualDistance(Math.ceil(this.distanceRan));
- if (actualDistance > 0) {
- this.invertTrigger =
- !(actualDistance % this.config.INVERT_DISTANCE);
- if (this.invertTrigger && this.invertTimer === 0) {
- this.invertTimer += deltaTime;
- this.invert(false);
- }
- }
- }
- }
- }
- if (this.playing || (!this.activated &&
- this.tRex.blinkCount < Runner.config.MAX_BLINK_COUNT)) {
- this.tRex.update(deltaTime);
- this.scheduleNextUpdate();
- }
- },
- /**
- * Event handler.
- * @param {Event} e
- */
- handleEvent(e) {
- return (function(evtType, events) {
- switch (evtType) {
- case events.KEYDOWN:
- case events.TOUCHSTART:
- case events.POINTERDOWN:
- this.onKeyDown(e);
- break;
- case events.KEYUP:
- case events.TOUCHEND:
- case events.POINTERUP:
- this.onKeyUp(e);
- break;
- case events.GAMEPADCONNECTED:
- this.onGamepadConnected(e);
- break;
- }
- }.bind(this))(e.type, Runner.events);
- },
- /**
- * Initialize audio cues if activated by focus on the canvas element.
- * @param {Event} e
- */
- handleCanvasKeyPress(e) {
- if (!this.activated && !Runner.audioCues) {
- this.toggleSpeed();
- Runner.audioCues = true;
- this.generatedSoundFx.init();
- Runner.generatedSoundFx = this.generatedSoundFx;
- Runner.config.CLEAR_TIME *= 1.2;
- } else if (e.keyCode && Runner.keycodes.JUMP[e.keyCode]) {
- this.onKeyDown(e);
- }
- },
- /**
- * Prevent space key press from scrolling.
- * @param {Event} e
- */
- preventScrolling(e) {
- if (e.keyCode === 32) {
- e.preventDefault();
- }
- },
- /**
- * Toggle speed setting if toggle is shown.
- */
- toggleSpeed() {
- if (Runner.audioCues) {
- const speedChange = Runner.slowDown != this.slowSpeedCheckbox.checked;
- if (speedChange) {
- Runner.slowDown = this.slowSpeedCheckbox.checked;
- const updatedConfig =
- Runner.slowDown ? Runner.slowConfig : Runner.normalConfig;
- Runner.config = Object.assign(Runner.config, updatedConfig);
- this.currentSpeed = updatedConfig.SPEED;
- this.tRex.enableSlowConfig();
- this.horizon.adjustObstacleSpeed();
- }
- if (this.playing) {
- this.disableSpeedToggle(true);
- }
- }
- },
- /**
- * Show the speed toggle.
- * From focus event or when audio cues are activated.
- * @param {Event=} e
- */
- showSpeedToggle(e) {
- const isFocusEvent = e && e.type == 'focus';
- if (Runner.audioCues || isFocusEvent) {
- this.slowSpeedCheckboxLabel.classList.toggle(
- HIDDEN_CLASS, isFocusEvent ? false : !this.crashed);
- }
- },
- /**
- * Disable the speed toggle.
- * @param {boolean} disable
- */
- disableSpeedToggle(disable) {
- if (disable) {
- this.slowSpeedCheckbox.setAttribute('disabled', 'disabled');
- } else {
- this.slowSpeedCheckbox.removeAttribute('disabled');
- }
- },
- /**
- * Bind relevant key / mouse / touch listeners.
- */
- startListening() {
- // A11y keyboard / screen reader activation.
- this.containerEl.addEventListener(
- Runner.events.KEYDOWN, this.handleCanvasKeyPress.bind(this));
- if (!IS_MOBILE) {
- this.containerEl.addEventListener(
- Runner.events.FOCUS, this.showSpeedToggle.bind(this));
- }
- this.canvas.addEventListener(
- Runner.events.KEYDOWN, this.preventScrolling.bind(this));
- this.canvas.addEventListener(
- Runner.events.KEYUP, this.preventScrolling.bind(this));
- // Keys.
- document.addEventListener(Runner.events.KEYDOWN, this);
- document.addEventListener(Runner.events.KEYUP, this);
- // Touch / pointer.
- this.containerEl.addEventListener(Runner.events.TOUCHSTART, this);
- document.addEventListener(Runner.events.POINTERDOWN, this);
- document.addEventListener(Runner.events.POINTERUP, this);
- if (this.isArcadeMode()) {
- // Gamepad
- window.addEventListener(Runner.events.GAMEPADCONNECTED, this);
- }
- },
- /**
- * Remove all listeners.
- */
- stopListening() {
- document.removeEventListener(Runner.events.KEYDOWN, this);
- document.removeEventListener(Runner.events.KEYUP, this);
- if (this.touchController) {
- this.touchController.removeEventListener(Runner.events.TOUCHSTART, this);
- this.touchController.removeEventListener(Runner.events.TOUCHEND, this);
- }
- this.containerEl.removeEventListener(Runner.events.TOUCHSTART, this);
- document.removeEventListener(Runner.events.POINTERDOWN, this);
- document.removeEventListener(Runner.events.POINTERUP, this);
- if (this.isArcadeMode()) {
- window.removeEventListener(Runner.events.GAMEPADCONNECTED, this);
- }
- },
- /**
- * Process keydown.
- * @param {Event} e
- */
- onKeyDown(e) {
- // Prevent native page scrolling whilst tapping on mobile.
- if (IS_MOBILE && this.playing) {
- e.preventDefault();
- }
- if (this.isCanvasInView()) {
- // Allow toggling of speed toggle.
- if (Runner.keycodes.JUMP[e.keyCode] &&
- e.target == this.slowSpeedCheckbox) {
- return;
- }
- if (!this.crashed && !this.paused) {
- // For a11y, screen reader activation.
- const isMobileMouseInput = IS_MOBILE &&
- e.type === Runner.events.POINTERDOWN &&
- e.pointerType == 'mouse' && e.target == this.containerEl ||
- (IS_IOS && e.pointerType == 'touch' &&
- document.activeElement == this.containerEl);
- if (Runner.keycodes.JUMP[e.keyCode] ||
- e.type === Runner.events.TOUCHSTART || isMobileMouseInput ||
- (Runner.keycodes.DUCK[e.keyCode] && this.altGameModeActive)) {
- e.preventDefault();
- // Starting the game for the first time.
- if (!this.playing) {
- // Started by touch so create a touch controller.
- if (!this.touchController && e.type === Runner.events.TOUCHSTART) {
- this.createTouchController();
- }
- if (isMobileMouseInput) {
- this.handleCanvasKeyPress(e);
- }
- this.loadSounds();
- this.setPlayStatus(true);
- this.update();
- if (window.errorPageController) {
- errorPageController.trackEasterEgg();
- }
- }
- // Start jump.
- if (!this.tRex.jumping && !this.tRex.ducking) {
- if (Runner.audioCues) {
- this.generatedSoundFx.cancelFootSteps();
- } else {
- this.playSound(this.soundFx.BUTTON_PRESS);
- }
- this.tRex.startJump(this.currentSpeed);
- }
- // Ducking is disabled on alt game modes.
- } else if (
- !this.altGameModeActive && this.playing &&
- Runner.keycodes.DUCK[e.keyCode]) {
- e.preventDefault();
- if (this.tRex.jumping) {
- // Speed drop, activated only when jump key is not pressed.
- this.tRex.setSpeedDrop();
- } else if (!this.tRex.jumping && !this.tRex.ducking) {
- // Duck.
- this.tRex.setDuck(true);
- }
- }
- }
- }
- },
- /**
- * Process key up.
- * @param {Event} e
- */
- onKeyUp(e) {
- const keyCode = String(e.keyCode);
- const isjumpKey = Runner.keycodes.JUMP[keyCode] ||
- e.type === Runner.events.TOUCHEND || e.type === Runner.events.POINTERUP;
- if (this.isRunning() && isjumpKey) {
- this.tRex.endJump();
- } else if (Runner.keycodes.DUCK[keyCode]) {
- this.tRex.speedDrop = false;
- this.tRex.setDuck(false);
- } else if (this.crashed) {
- // Check that enough time has elapsed before allowing jump key to restart.
- const deltaTime = getTimeStamp() - this.time;
- if (this.isCanvasInView() &&
- (Runner.keycodes.RESTART[keyCode] || this.isLeftClickOnCanvas(e) ||
- (deltaTime >= this.config.GAMEOVER_CLEAR_TIME &&
- Runner.keycodes.JUMP[keyCode]))) {
- this.handleGameOverClicks(e);
- }
- } else if (this.paused && isjumpKey) {
- // Reset the jump state
- this.tRex.reset();
- this.play();
- }
- },
- /**
- * Process gamepad connected event.
- * @param {Event} e
- */
- onGamepadConnected(e) {
- if (!this.pollingGamepads) {
- this.pollGamepadState();
- }
- },
- /**
- * rAF loop for gamepad polling.
- */
- pollGamepadState() {
- const gamepads = navigator.getGamepads();
- this.pollActiveGamepad(gamepads);
- this.pollingGamepads = true;
- requestAnimationFrame(this.pollGamepadState.bind(this));
- },
- /**
- * Polls for a gamepad with the jump button pressed. If one is found this
- * becomes the "active" gamepad and all others are ignored.
- * @param {!Array<Gamepad>} gamepads
- */
- pollForActiveGamepad(gamepads) {
- for (let i = 0; i < gamepads.length; ++i) {
- if (gamepads[i] && gamepads[i].buttons.length > 0 &&
- gamepads[i].buttons[0].pressed) {
- this.gamepadIndex = i;
- this.pollActiveGamepad(gamepads);
- return;
- }
- }
- },
- /**
- * Polls the chosen gamepad for button presses and generates KeyboardEvents
- * to integrate with the rest of the game logic.
- * @param {!Array<Gamepad>} gamepads
- */
- pollActiveGamepad(gamepads) {
- if (this.gamepadIndex === undefined) {
- this.pollForActiveGamepad(gamepads);
- return;
- }
- const gamepad = gamepads[this.gamepadIndex];
- if (!gamepad) {
- this.gamepadIndex = undefined;
- this.pollForActiveGamepad(gamepads);
- return;
- }
- // The gamepad specification defines the typical mapping of physical buttons
- // to button indicies: https://w3c.github.io/gamepad/#remapping
- this.pollGamepadButton(gamepad, 0, 38); // Jump
- if (gamepad.buttons.length >= 2) {
- this.pollGamepadButton(gamepad, 1, 40); // Duck
- }
- if (gamepad.buttons.length >= 10) {
- this.pollGamepadButton(gamepad, 9, 13); // Restart
- }
- this.previousGamepad = gamepad;
- },
- /**
- * Generates a key event based on a gamepad button.
- * @param {!Gamepad} gamepad
- * @param {number} buttonIndex
- * @param {number} keyCode
- */
- pollGamepadButton(gamepad, buttonIndex, keyCode) {
- const state = gamepad.buttons[buttonIndex].pressed;
- let previousState = false;
- if (this.previousGamepad) {
- previousState = this.previousGamepad.buttons[buttonIndex].pressed;
- }
- // Generate key events on the rising and falling edge of a button press.
- if (state !== previousState) {
- const e = new KeyboardEvent(state ? Runner.events.KEYDOWN
- : Runner.events.KEYUP,
- { keyCode: keyCode });
- document.dispatchEvent(e);
- }
- },
- /**
- * Handle interactions on the game over screen state.
- * A user is able to tap the high score twice to reset it.
- * @param {Event} e
- */
- handleGameOverClicks(e) {
- if (e.target != this.slowSpeedCheckbox) {
- e.preventDefault();
- if (this.distanceMeter.hasClickedOnHighScore(e) && this.highestScore) {
- if (this.distanceMeter.isHighScoreFlashing()) {
- // Subsequent click, reset the high score.
- this.saveHighScore(0, true);
- this.distanceMeter.resetHighScore();
- } else {
- // First click, flash the high score.
- this.distanceMeter.startHighScoreFlashing();
- }
- } else {
- this.distanceMeter.cancelHighScoreFlashing();
- this.restart();
- }
- }
- },
- /**
- * Returns whether the event was a left click on canvas.
- * On Windows right click is registered as a click.
- * @param {Event} e
- * @return {boolean}
- */
- isLeftClickOnCanvas(e) {
- return e.button != null && e.button < 2 &&
- e.type === Runner.events.POINTERUP &&
- (e.target === this.canvas ||
- (IS_MOBILE && Runner.audioCues && e.target === this.containerEl));
- },
- /**
- * RequestAnimationFrame wrapper.
- */
- scheduleNextUpdate() {
- if (!this.updatePending) {
- this.updatePending = true;
- this.raqId = requestAnimationFrame(this.update.bind(this));
- }
- },
- /**
- * Whether the game is running.
- * @return {boolean}
- */
- isRunning() {
- return !!this.raqId;
- },
- /**
- * Set the initial high score as stored in the user's profile.
- * @param {number} highScore
- */
- initializeHighScore(highScore) {
- this.syncHighestScore = true;
- highScore = Math.ceil(highScore);
- if (highScore < this.highestScore) {
- if (window.errorPageController) {
- errorPageController.updateEasterEggHighScore(this.highestScore);
- }
- return;
- }
- this.highestScore = highScore;
- this.distanceMeter.setHighScore(this.highestScore);
- },
- /**
- * Sets the current high score and saves to the profile if available.
- * @param {number} distanceRan Total distance ran.
- * @param {boolean=} opt_resetScore Whether to reset the score.
- */
- saveHighScore(distanceRan, opt_resetScore) {
- this.highestScore = Math.ceil(distanceRan);
- this.distanceMeter.setHighScore(this.highestScore);
- // Store the new high score in the profile.
- if (this.syncHighestScore && window.errorPageController) {
- if (opt_resetScore) {
- errorPageController.resetEasterEggHighScore();
- } else {
- errorPageController.updateEasterEggHighScore(this.highestScore);
- }
- }
- },
- /**
- * Game over state.
- */
- gameOver() {
- this.playSound(this.soundFx.HIT);
- vibrate(200);
- this.stop();
- this.crashed = true;
- this.distanceMeter.achievement = false;
- this.tRex.update(100, Trex.status.CRASHED);
- // Game over panel.
- if (!this.gameOverPanel) {
- const origSpriteDef = IS_HIDPI ?
- Runner.spriteDefinitionByType.original.HDPI :
- Runner.spriteDefinitionByType.original.LDPI;
- if (this.canvas) {
- if (Runner.isAltGameModeEnabled) {
- this.gameOverPanel = new GameOverPanel(
- this.canvas, origSpriteDef.TEXT_SPRITE, origSpriteDef.RESTART,
- this.dimensions, origSpriteDef.ALT_GAME_END,
- this.altGameModeActive);
- } else {
- this.gameOverPanel = new GameOverPanel(
- this.canvas, origSpriteDef.TEXT_SPRITE, origSpriteDef.RESTART,
- this.dimensions);
- }
- }
- }
- this.gameOverPanel.draw(this.altGameModeActive, this.tRex);
- // Update the high score.
- if (this.distanceRan > this.highestScore) {
- this.saveHighScore(this.distanceRan);
- }
- // Reset the time clock.
- this.time = getTimeStamp();
- if (Runner.audioCues) {
- this.generatedSoundFx.stopAll();
- announcePhrase(
- getA11yString(A11Y_STRINGS.gameOver)
- .replace(
- '$1',
- this.distanceMeter.getActualDistance(this.distanceRan)
- .toString()) +
- ' ' +
- getA11yString(A11Y_STRINGS.highScore)
- .replace(
- '$1',
- this.distanceMeter.getActualDistance(this.highestScore)
- .toString()));
- this.containerEl.setAttribute(
- 'title', getA11yString(A11Y_STRINGS.ariaLabel));
- }
- this.showSpeedToggle();
- this.disableSpeedToggle(false);
- },
- stop() {
- this.setPlayStatus(false);
- this.paused = true;
- cancelAnimationFrame(this.raqId);
- this.raqId = 0;
- this.generatedSoundFx.stopAll();
- },
- play() {
- if (!this.crashed) {
- this.setPlayStatus(true);
- this.paused = false;
- this.tRex.update(0, Trex.status.RUNNING);
- this.time = getTimeStamp();
- this.update();
- this.generatedSoundFx.background();
- }
- },
- restart() {
- if (!this.raqId) {
- this.playCount++;
- this.runningTime = 0;
- this.setPlayStatus(true);
- this.toggleSpeed();
- this.paused = false;
- this.crashed = false;
- this.distanceRan = 0;
- this.setSpeed(this.config.SPEED);
- this.time = getTimeStamp();
- this.containerEl.classList.remove(Runner.classes.CRASHED);
- this.clearCanvas();
- this.distanceMeter.reset();
- this.horizon.reset();
- this.tRex.reset();
- this.playSound(this.soundFx.BUTTON_PRESS);
- this.invert(true);
- this.flashTimer = null;
- this.update();
- this.gameOverPanel.reset();
- this.generatedSoundFx.background();
- this.containerEl.setAttribute('title', getA11yString(A11Y_STRINGS.jump));
- announcePhrase(getA11yString(A11Y_STRINGS.started));
- }
- },
- setPlayStatus(isPlaying) {
- if (this.touchController) {
- this.touchController.classList.toggle(HIDDEN_CLASS, !isPlaying);
- }
- this.playing = isPlaying;
- },
- /**
- * Whether the game should go into arcade mode.
- * @return {boolean}
- */
- isArcadeMode() {
- // In RTL languages the title is wrapped with the left to right mark
- // control characters ‪ and ‬ but are invisible.
- return IS_RTL ? document.title.indexOf(ARCADE_MODE_URL) == 1 :
- document.title === ARCADE_MODE_URL;
- },
- /**
- * Hides offline messaging for a fullscreen game only experience.
- */
- setArcadeMode() {
- document.body.classList.add(Runner.classes.ARCADE_MODE);
- this.setArcadeModeContainerScale();
- },
- /**
- * Sets the scaling for arcade mode.
- */
- setArcadeModeContainerScale() {
- const windowHeight = window.innerHeight;
- const scaleHeight = windowHeight / this.dimensions.HEIGHT;
- const scaleWidth = window.innerWidth / this.dimensions.WIDTH;
- const scale = Math.max(1, Math.min(scaleHeight, scaleWidth));
- const scaledCanvasHeight = this.dimensions.HEIGHT * scale;
- // Positions the game container at 10% of the available vertical window
- // height minus the game container height.
- const translateY = Math.ceil(Math.max(0, (windowHeight - scaledCanvasHeight -
- Runner.config.ARCADE_MODE_INITIAL_TOP_POSITION) *
- Runner.config.ARCADE_MODE_TOP_POSITION_PERCENT)) *
- window.devicePixelRatio;
- const cssScale = IS_RTL ? -scale + ',' + scale : scale;
- this.containerEl.style.transform =
- 'scale(' + cssScale + ') translateY(' + translateY + 'px)';
- },
- /**
- * Pause the game if the tab is not in focus.
- */
- onVisibilityChange(e) {
- if (document.hidden || document.webkitHidden || e.type === 'blur' ||
- document.visibilityState !== 'visible') {
- this.stop();
- } else if (!this.crashed) {
- this.tRex.reset();
- this.play();
- }
- },
- /**
- * Play a sound.
- * @param {AudioBuffer} soundBuffer
- */
- playSound(soundBuffer) {
- if (soundBuffer) {
- const sourceNode = this.audioContext.createBufferSource();
- sourceNode.buffer = soundBuffer;
- sourceNode.connect(this.audioContext.destination);
- sourceNode.start(0);
- }
- },
- /**
- * Inverts the current page / canvas colors.
- * @param {boolean} reset Whether to reset colors.
- */
- invert(reset) {
- const htmlEl = document.firstElementChild;
- if (reset) {
- htmlEl.classList.toggle(Runner.classes.INVERTED,
- false);
- this.invertTimer = 0;
- this.inverted = false;
- } else {
- this.inverted = htmlEl.classList.toggle(
- Runner.classes.INVERTED, this.invertTrigger);
- }
- },
- };
- /**
- * Updates the canvas size taking into
- * account the backing store pixel ratio and
- * the device pixel ratio.
- *
- * See article by Paul Lewis:
- * http://www.html5rocks.com/en/tutorials/canvas/hidpi/
- *
- * @param {HTMLCanvasElement} canvas
- * @param {number=} opt_width
- * @param {number=} opt_height
- * @return {boolean} Whether the canvas was scaled.
- */
- Runner.updateCanvasScaling = function(canvas, opt_width, opt_height) {
- const context =
- /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
- // Query the various pixel ratios
- const devicePixelRatio = Math.floor(window.devicePixelRatio) || 1;
- /** @suppress {missingProperties} */
- const backingStoreRatio =
- Math.floor(context.webkitBackingStorePixelRatio) || 1;
- const ratio = devicePixelRatio / backingStoreRatio;
- // Upscale the canvas if the two ratios don't match
- if (devicePixelRatio !== backingStoreRatio) {
- const oldWidth = opt_width || canvas.width;
- const oldHeight = opt_height || canvas.height;
- canvas.width = oldWidth * ratio;
- canvas.height = oldHeight * ratio;
- canvas.style.width = oldWidth + 'px';
- canvas.style.height = oldHeight + 'px';
- // Scale the context to counter the fact that we've manually scaled
- // our canvas element.
- context.scale(ratio, ratio);
- return true;
- } else if (devicePixelRatio === 1) {
- // Reset the canvas width / height. Fixes scaling bug when the page is
- // zoomed and the devicePixelRatio changes accordingly.
- canvas.style.width = canvas.width + 'px';
- canvas.style.height = canvas.height + 'px';
- }
- return false;
- };
- /**
- * Whether events are enabled.
- * @return {boolean}
- */
- Runner.isAltGameModeEnabled = function() {
- return loadTimeData && loadTimeData.valueExists('enableAltGameMode');
- };
- /**
- * Generated sound FX class for audio cues.
- * @constructor
- */
- function GeneratedSoundFx() {
- this.audioCues = false;
- this.context = null;
- this.panner = null;
- }
- GeneratedSoundFx.prototype = {
- init() {
- this.audioCues = true;
- if (!this.context) {
- // iOS only supports the webkit version.
- this.context = window.webkitAudioContext ? new webkitAudioContext() :
- new AudioContext();
- if (IS_IOS) {
- this.context.onstatechange = (function() {
- if (this.context.state != 'running') {
- this.context.resume();
- }
- }).bind(this);
- this.context.resume();
- }
- this.panner = this.context.createStereoPanner ?
- this.context.createStereoPanner() :
- null;
- }
- },
- stopAll() {
- this.cancelFootSteps();
- },
- /**
- * Play oscillators at certain frequency and for a certain time.
- * @param {number} frequency
- * @param {number} startTime
- * @param {number} duration
- * @param {?number=} opt_vol
- * @param {number=} opt_pan
- */
- playNote(frequency, startTime, duration, opt_vol, opt_pan) {
- const osc1 = this.context.createOscillator();
- const osc2 = this.context.createOscillator();
- const volume = this.context.createGain();
- // Set oscillator wave type
- osc1.type = 'triangle';
- osc2.type = 'triangle';
- volume.gain.value = 0.1;
- // Set up node routing
- if (this.panner) {
- this.panner.pan.value = opt_pan || 0;
- osc1.connect(volume).connect(this.panner);
- osc2.connect(volume).connect(this.panner);
- this.panner.connect(this.context.destination);
- } else {
- osc1.connect(volume);
- osc2.connect(volume);
- volume.connect(this.context.destination);
- }
- // Detune oscillators for chorus effect
- osc1.frequency.value = frequency + 1;
- osc2.frequency.value = frequency - 2;
- // Fade out
- volume.gain.setValueAtTime(opt_vol || 0.01, startTime + duration - 0.05);
- volume.gain.linearRampToValueAtTime(0.00001, startTime + duration);
- // Start oscillators
- osc1.start(startTime);
- osc2.start(startTime);
- // Stop oscillators
- osc1.stop(startTime + duration);
- osc2.stop(startTime + duration);
- },
- background() {
- if (this.audioCues) {
- const now = this.context.currentTime;
- this.playNote(493.883, now, 0.116);
- this.playNote(659.255, now + 0.116, 0.232);
- this.loopFootSteps();
- }
- },
- loopFootSteps() {
- if (this.audioCues && !this.bgSoundIntervalId) {
- this.bgSoundIntervalId = setInterval(function() {
- this.playNote(73.42, this.context.currentTime, 0.05, 0.16);
- this.playNote(69.30, this.context.currentTime + 0.116, 0.116, 0.16);
- }.bind(this), 280);
- }
- },
- cancelFootSteps() {
- if (this.audioCues && this.bgSoundIntervalId) {
- clearInterval(this.bgSoundIntervalId);
- this.bgSoundIntervalId = null;
- this.playNote(103.83, this.context.currentTime, 0.232, 0.02);
- this.playNote(116.54, this.context.currentTime + 0.116, 0.232, 0.02);
- }
- },
- collect() {
- if (this.audioCues) {
- this.cancelFootSteps();
- const now = this.context.currentTime;
- this.playNote(830.61, now, 0.116);
- this.playNote(1318.51, now + 0.116, 0.232);
- }
- },
- jump() {
- if (this.audioCues) {
- const now = this.context.currentTime;
- this.playNote(659.25, now, 0.116, 0.3, -0.6);
- this.playNote(880, now + 0.116, 0.232, 0.3, -0.6);
- }
- },
- };
- /**
- * Speak a phrase using Speech Synthesis API for a11y.
- * @param {string} phrase Sentence to speak.
- */
- function speakPhrase(phrase) {
- if ('speechSynthesis' in window) {
- const msg = new SpeechSynthesisUtterance(phrase);
- const voices = window.speechSynthesis.getVoices();
- msg.text = phrase;
- speechSynthesis.speak(msg);
- }
- }
- /**
- * For screen readers make an announcement to the live region.
- * @param {string} phrase Sentence to speak.
- */
- function announcePhrase(phrase) {
- if (Runner.a11yStatusEl) {
- Runner.a11yStatusEl.textContent = '';
- Runner.a11yStatusEl.textContent = phrase;
- }
- }
- /**
- * Returns a string from loadTimeData data object.
- * @param {string} stringName
- * @return {string}
- */
- function getA11yString(stringName) {
- return loadTimeData && loadTimeData.valueExists(stringName) ?
- loadTimeData.getString(stringName) :
- '';
- }
- /**
- * Get random number.
- * @param {number} min
- * @param {number} max
- */
- function getRandomNum(min, max) {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- /**
- * Vibrate on mobile devices.
- * @param {number} duration Duration of the vibration in milliseconds.
- */
- function vibrate(duration) {
- if (IS_MOBILE && window.navigator.vibrate) {
- window.navigator.vibrate(duration);
- }
- }
- /**
- * Create canvas element.
- * @param {Element} container Element to append canvas to.
- * @param {number} width
- * @param {number} height
- * @param {string=} opt_classname
- * @return {HTMLCanvasElement}
- */
- function createCanvas(container, width, height, opt_classname) {
- const canvas =
- /** @type {!HTMLCanvasElement} */ (document.createElement('canvas'));
- canvas.className = opt_classname ? Runner.classes.CANVAS + ' ' +
- opt_classname : Runner.classes.CANVAS;
- canvas.width = width;
- canvas.height = height;
- container.appendChild(canvas);
- return canvas;
- }
- /**
- * Decodes the base 64 audio to ArrayBuffer used by Web Audio.
- * @param {string} base64String
- */
- function decodeBase64ToArrayBuffer(base64String) {
- const len = (base64String.length / 4) * 3;
- const str = atob(base64String);
- const arrayBuffer = new ArrayBuffer(len);
- const bytes = new Uint8Array(arrayBuffer);
- for (let i = 0; i < len; i++) {
- bytes[i] = str.charCodeAt(i);
- }
- return bytes.buffer;
- }
- /**
- * Return the current timestamp.
- * @return {number}
- */
- function getTimeStamp() {
- return IS_IOS ? new Date().getTime() : performance.now();
- }
- //******************************************************************************
- /**
- * Game over panel.
- * @param {!HTMLCanvasElement} canvas
- * @param {Object} textImgPos
- * @param {Object} restartImgPos
- * @param {!Object} dimensions Canvas dimensions.
- * @param {Object=} opt_altGameEndImgPos
- * @param {boolean=} opt_altGameActive
- * @constructor
- */
- function GameOverPanel(
- canvas, textImgPos, restartImgPos, dimensions, opt_altGameEndImgPos,
- opt_altGameActive) {
- this.canvas = canvas;
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
- this.canvasDimensions = dimensions;
- this.textImgPos = textImgPos;
- this.restartImgPos = restartImgPos;
- this.altGameEndImgPos = opt_altGameEndImgPos;
- this.altGameModeActive = opt_altGameActive;
- // Retry animation.
- this.frameTimeStamp = 0;
- this.animTimer = 0;
- this.currentFrame = 0;
- this.gameOverRafId = null;
- this.flashTimer = 0;
- this.flashCounter = 0;
- this.originalText = true;
- }
- GameOverPanel.RESTART_ANIM_DURATION = 875;
- GameOverPanel.LOGO_PAUSE_DURATION = 875;
- GameOverPanel.FLASH_ITERATIONS = 5;
- /**
- * Animation frames spec.
- */
- GameOverPanel.animConfig = {
- frames: [0, 36, 72, 108, 144, 180, 216, 252],
- msPerFrame: GameOverPanel.RESTART_ANIM_DURATION / 8,
- };
- /**
- * Dimensions used in the panel.
- * @enum {number}
- */
- GameOverPanel.dimensions = {
- TEXT_X: 0,
- TEXT_Y: 13,
- TEXT_WIDTH: 191,
- TEXT_HEIGHT: 11,
- RESTART_WIDTH: 36,
- RESTART_HEIGHT: 32,
- };
- GameOverPanel.prototype = {
- /**
- * Update the panel dimensions.
- * @param {number} width New canvas width.
- * @param {number} opt_height Optional new canvas height.
- */
- updateDimensions(width, opt_height) {
- this.canvasDimensions.WIDTH = width;
- if (opt_height) {
- this.canvasDimensions.HEIGHT = opt_height;
- }
- this.currentFrame = GameOverPanel.animConfig.frames.length - 1;
- },
- drawGameOverText(dimensions, opt_useAltText) {
- const centerX = this.canvasDimensions.WIDTH / 2;
- let textSourceX = dimensions.TEXT_X;
- let textSourceY = dimensions.TEXT_Y;
- let textSourceWidth = dimensions.TEXT_WIDTH;
- let textSourceHeight = dimensions.TEXT_HEIGHT;
- const textTargetX = Math.round(centerX - (dimensions.TEXT_WIDTH / 2));
- const textTargetY = Math.round((this.canvasDimensions.HEIGHT - 25) / 3);
- const textTargetWidth = dimensions.TEXT_WIDTH;
- const textTargetHeight = dimensions.TEXT_HEIGHT;
- if (IS_HIDPI) {
- textSourceY *= 2;
- textSourceX *= 2;
- textSourceWidth *= 2;
- textSourceHeight *= 2;
- }
- if (!opt_useAltText) {
- textSourceX += this.textImgPos.x;
- textSourceY += this.textImgPos.y;
- }
- const spriteSource =
- opt_useAltText ? Runner.altCommonImageSprite : Runner.origImageSprite;
- this.canvasCtx.save();
- if (IS_RTL) {
- this.canvasCtx.translate(this.canvasDimensions.WIDTH, 0);
- this.canvasCtx.scale(-1, 1);
- }
- // Game over text from sprite.
- this.canvasCtx.drawImage(
- spriteSource, textSourceX, textSourceY, textSourceWidth,
- textSourceHeight, textTargetX, textTargetY, textTargetWidth,
- textTargetHeight);
- this.canvasCtx.restore();
- },
- /**
- * Draw additional adornments for alternative game types.
- */
- drawAltGameElements(tRex) {
- // Additional adornments.
- if (this.altGameModeActive && Runner.spriteDefinition.ALT_GAME_END_CONFIG) {
- const altGameEndConfig = Runner.spriteDefinition.ALT_GAME_END_CONFIG;
- let altGameEndSourceWidth = altGameEndConfig.WIDTH;
- let altGameEndSourceHeight = altGameEndConfig.HEIGHT;
- const altGameEndTargetX = tRex.xPos + altGameEndConfig.X_OFFSET;
- const altGameEndTargetY = tRex.yPos + altGameEndConfig.Y_OFFSET;
- if (IS_HIDPI) {
- altGameEndSourceWidth *= 2;
- altGameEndSourceHeight *= 2;
- }
- this.canvasCtx.drawImage(
- Runner.altCommonImageSprite, this.altGameEndImgPos.x,
- this.altGameEndImgPos.y, altGameEndSourceWidth,
- altGameEndSourceHeight, altGameEndTargetX, altGameEndTargetY,
- altGameEndConfig.WIDTH, altGameEndConfig.HEIGHT);
- }
- },
- /**
- * Draw restart button.
- */
- drawRestartButton() {
- const dimensions = GameOverPanel.dimensions;
- let framePosX = GameOverPanel.animConfig.frames[this.currentFrame];
- let restartSourceWidth = dimensions.RESTART_WIDTH;
- let restartSourceHeight = dimensions.RESTART_HEIGHT;
- const restartTargetX =
- (this.canvasDimensions.WIDTH / 2) - (dimensions.RESTART_WIDTH / 2);
- const restartTargetY = this.canvasDimensions.HEIGHT / 2;
- if (IS_HIDPI) {
- restartSourceWidth *= 2;
- restartSourceHeight *= 2;
- framePosX *= 2;
- }
- this.canvasCtx.save();
- if (IS_RTL) {
- this.canvasCtx.translate(this.canvasDimensions.WIDTH, 0);
- this.canvasCtx.scale(-1, 1);
- }
- this.canvasCtx.drawImage(
- Runner.origImageSprite, this.restartImgPos.x + framePosX,
- this.restartImgPos.y, restartSourceWidth, restartSourceHeight,
- restartTargetX, restartTargetY, dimensions.RESTART_WIDTH,
- dimensions.RESTART_HEIGHT);
- this.canvasCtx.restore();
- },
- /**
- * Draw the panel.
- * @param {boolean} opt_altGameModeActive
- * @param {!Trex} opt_tRex
- */
- draw(opt_altGameModeActive, opt_tRex) {
- if (opt_altGameModeActive) {
- this.altGameModeActive = opt_altGameModeActive;
- }
- this.drawGameOverText(GameOverPanel.dimensions, false);
- this.drawRestartButton();
- this.drawAltGameElements(opt_tRex);
- this.update();
- },
- /**
- * Update animation frames.
- */
- update() {
- const now = getTimeStamp();
- const deltaTime = now - (this.frameTimeStamp || now);
- this.frameTimeStamp = now;
- this.animTimer += deltaTime;
- this.flashTimer += deltaTime;
- // Restart Button
- if (this.currentFrame == 0 &&
- this.animTimer > GameOverPanel.LOGO_PAUSE_DURATION) {
- this.animTimer = 0;
- this.currentFrame++;
- this.drawRestartButton();
- } else if (
- this.currentFrame > 0 &&
- this.currentFrame < GameOverPanel.animConfig.frames.length) {
- if (this.animTimer >= GameOverPanel.animConfig.msPerFrame) {
- this.currentFrame++;
- this.drawRestartButton();
- }
- } else if (
- !this.altGameModeActive &&
- this.currentFrame == GameOverPanel.animConfig.frames.length) {
- this.reset();
- return;
- }
- // Game over text
- if (this.altGameModeActive &&
- Runner.spriteDefinitionByType.original.ALT_GAME_OVER_TEXT_CONFIG) {
- const altTextConfig =
- Runner.spriteDefinitionByType.original.ALT_GAME_OVER_TEXT_CONFIG;
- if (this.flashCounter < GameOverPanel.FLASH_ITERATIONS &&
- this.flashTimer > altTextConfig.FLASH_DURATION) {
- this.flashTimer = 0;
- this.originalText = !this.originalText;
- this.clearGameOverTextBounds();
- if (this.originalText) {
- this.drawGameOverText(GameOverPanel.dimensions, false);
- this.flashCounter++;
- } else {
- this.drawGameOverText(altTextConfig, true);
- }
- } else if (this.flashCounter >= GameOverPanel.FLASH_ITERATIONS) {
- this.reset();
- return;
- }
- }
- this.gameOverRafId = requestAnimationFrame(this.update.bind(this));
- },
- /**
- * Clear game over text.
- */
- clearGameOverTextBounds() {
- this.canvasCtx.save();
- this.canvasCtx.clearRect(
- Math.round(
- this.canvasDimensions.WIDTH / 2 -
- (GameOverPanel.dimensions.TEXT_WIDTH / 2)),
- Math.round((this.canvasDimensions.HEIGHT - 25) / 3),
- GameOverPanel.dimensions.TEXT_WIDTH,
- GameOverPanel.dimensions.TEXT_HEIGHT + 4);
- this.canvasCtx.restore();
- },
- reset() {
- if (this.gameOverRafId) {
- cancelAnimationFrame(this.gameOverRafId);
- this.gameOverRafId = null;
- }
- this.animTimer = 0;
- this.frameTimeStamp = 0;
- this.currentFrame = 0;
- this.flashTimer = 0;
- this.flashCounter = 0;
- this.originalText = true;
- },
- };
- //******************************************************************************
- /**
- * Check for a collision.
- * @param {!Obstacle} obstacle
- * @param {!Trex} tRex T-rex object.
- * @param {CanvasRenderingContext2D=} opt_canvasCtx Optional canvas context for
- * drawing collision boxes.
- * @return {Array<CollisionBox>|undefined}
- */
- function checkForCollision(obstacle, tRex, opt_canvasCtx) {
- const obstacleBoxXPos = Runner.defaultDimensions.WIDTH + obstacle.xPos;
- // Adjustments are made to the bounding box as there is a 1 pixel white
- // border around the t-rex and obstacles.
- const tRexBox = new CollisionBox(
- tRex.xPos + 1,
- tRex.yPos + 1,
- tRex.config.WIDTH - 2,
- tRex.config.HEIGHT - 2);
- const obstacleBox = new CollisionBox(
- obstacle.xPos + 1,
- obstacle.yPos + 1,
- obstacle.typeConfig.width * obstacle.size - 2,
- obstacle.typeConfig.height - 2);
- // Debug outer box
- if (opt_canvasCtx) {
- drawCollisionBoxes(opt_canvasCtx, tRexBox, obstacleBox);
- }
- // Simple outer bounds check.
- if (boxCompare(tRexBox, obstacleBox)) {
- const collisionBoxes = obstacle.collisionBoxes;
- let tRexCollisionBoxes = [];
- if (Runner.isAltGameModeEnabled()) {
- tRexCollisionBoxes = Runner.spriteDefinition.TREX.COLLISION_BOXES;
- } else {
- tRexCollisionBoxes = tRex.ducking ? Trex.collisionBoxes.DUCKING :
- Trex.collisionBoxes.RUNNING;
- }
- // Detailed axis aligned box check.
- for (let t = 0; t < tRexCollisionBoxes.length; t++) {
- for (let i = 0; i < collisionBoxes.length; i++) {
- // Adjust the box to actual positions.
- const adjTrexBox =
- createAdjustedCollisionBox(tRexCollisionBoxes[t], tRexBox);
- const adjObstacleBox =
- createAdjustedCollisionBox(collisionBoxes[i], obstacleBox);
- const crashed = boxCompare(adjTrexBox, adjObstacleBox);
- // Draw boxes for debug.
- if (opt_canvasCtx) {
- drawCollisionBoxes(opt_canvasCtx, adjTrexBox, adjObstacleBox);
- }
- if (crashed) {
- return [adjTrexBox, adjObstacleBox];
- }
- }
- }
- }
- }
- /**
- * Adjust the collision box.
- * @param {!CollisionBox} box The original box.
- * @param {!CollisionBox} adjustment Adjustment box.
- * @return {CollisionBox} The adjusted collision box object.
- */
- function createAdjustedCollisionBox(box, adjustment) {
- return new CollisionBox(
- box.x + adjustment.x,
- box.y + adjustment.y,
- box.width,
- box.height);
- }
- /**
- * Draw the collision boxes for debug.
- */
- function drawCollisionBoxes(canvasCtx, tRexBox, obstacleBox) {
- canvasCtx.save();
- canvasCtx.strokeStyle = '#f00';
- canvasCtx.strokeRect(tRexBox.x, tRexBox.y, tRexBox.width, tRexBox.height);
- canvasCtx.strokeStyle = '#0f0';
- canvasCtx.strokeRect(obstacleBox.x, obstacleBox.y,
- obstacleBox.width, obstacleBox.height);
- canvasCtx.restore();
- }
- /**
- * Compare two collision boxes for a collision.
- * @param {CollisionBox} tRexBox
- * @param {CollisionBox} obstacleBox
- * @return {boolean} Whether the boxes intersected.
- */
- function boxCompare(tRexBox, obstacleBox) {
- let crashed = false;
- const tRexBoxX = tRexBox.x;
- const tRexBoxY = tRexBox.y;
- const obstacleBoxX = obstacleBox.x;
- const obstacleBoxY = obstacleBox.y;
- // Axis-Aligned Bounding Box method.
- if (tRexBox.x < obstacleBoxX + obstacleBox.width &&
- tRexBox.x + tRexBox.width > obstacleBoxX &&
- tRexBox.y < obstacleBox.y + obstacleBox.height &&
- tRexBox.height + tRexBox.y > obstacleBox.y) {
- crashed = true;
- }
- return crashed;
- }
- //******************************************************************************
- /**
- * Collision box object.
- * @param {number} x X position.
- * @param {number} y Y Position.
- * @param {number} w Width.
- * @param {number} h Height.
- * @constructor
- */
- function CollisionBox(x, y, w, h) {
- this.x = x;
- this.y = y;
- this.width = w;
- this.height = h;
- }
- //******************************************************************************
- /**
- * Obstacle.
- * @param {CanvasRenderingContext2D} canvasCtx
- * @param {ObstacleType} type
- * @param {Object} spriteImgPos Obstacle position in sprite.
- * @param {Object} dimensions
- * @param {number} gapCoefficient Mutipler in determining the gap.
- * @param {number} speed
- * @param {number=} opt_xOffset
- * @param {boolean=} opt_isAltGameMode
- * @constructor
- */
- function Obstacle(
- canvasCtx, type, spriteImgPos, dimensions, gapCoefficient, speed,
- opt_xOffset, opt_isAltGameMode) {
- this.canvasCtx = canvasCtx;
- this.spritePos = spriteImgPos;
- this.typeConfig = type;
- this.gapCoefficient = Runner.slowDown ? gapCoefficient * 2 : gapCoefficient;
- this.size = getRandomNum(1, Obstacle.MAX_OBSTACLE_LENGTH);
- this.dimensions = dimensions;
- this.remove = false;
- this.xPos = dimensions.WIDTH + (opt_xOffset || 0);
- this.yPos = 0;
- this.width = 0;
- this.collisionBoxes = [];
- this.gap = 0;
- this.speedOffset = 0;
- this.altGameModeActive = opt_isAltGameMode;
- this.imageSprite = this.typeConfig.type == 'COLLECTABLE' ?
- Runner.altCommonImageSprite :
- this.altGameModeActive ? Runner.altGameImageSprite : Runner.imageSprite;
- // For animated obstacles.
- this.currentFrame = 0;
- this.timer = 0;
- this.init(speed);
- }
- /**
- * Coefficient for calculating the maximum gap.
- */
- Obstacle.MAX_GAP_COEFFICIENT = 1.5;
- /**
- * Maximum obstacle grouping count.
- */
- Obstacle.MAX_OBSTACLE_LENGTH = 3;
- Obstacle.prototype = {
- /**
- * Initialise the DOM for the obstacle.
- * @param {number} speed
- */
- init(speed) {
- this.cloneCollisionBoxes();
- // Only allow sizing if we're at the right speed.
- if (this.size > 1 && this.typeConfig.multipleSpeed > speed) {
- this.size = 1;
- }
- this.width = this.typeConfig.width * this.size;
- // Check if obstacle can be positioned at various heights.
- if (Array.isArray(this.typeConfig.yPos)) {
- const yPosConfig =
- IS_MOBILE ? this.typeConfig.yPosMobile : this.typeConfig.yPos;
- this.yPos = yPosConfig[getRandomNum(0, yPosConfig.length - 1)];
- } else {
- this.yPos = this.typeConfig.yPos;
- }
- this.draw();
- // Make collision box adjustments,
- // Central box is adjusted to the size as one box.
- // ____ ______ ________
- // _| |-| _| |-| _| |-|
- // | |<->| | | |<--->| | | |<----->| |
- // | | 1 | | | | 2 | | | | 3 | |
- // |_|___|_| |_|_____|_| |_|_______|_|
- //
- if (this.size > 1) {
- this.collisionBoxes[1].width = this.width - this.collisionBoxes[0].width -
- this.collisionBoxes[2].width;
- this.collisionBoxes[2].x = this.width - this.collisionBoxes[2].width;
- }
- // For obstacles that go at a different speed from the horizon.
- if (this.typeConfig.speedOffset) {
- this.speedOffset = Math.random() > 0.5 ? this.typeConfig.speedOffset :
- -this.typeConfig.speedOffset;
- }
- this.gap = this.getGap(this.gapCoefficient, speed);
- // Increase gap for audio cues enabled.
- if (Runner.audioCues) {
- this.gap *= 2;
- }
- },
- /**
- * Draw and crop based on size.
- */
- draw() {
- let sourceWidth = this.typeConfig.width;
- let sourceHeight = this.typeConfig.height;
- if (IS_HIDPI) {
- sourceWidth = sourceWidth * 2;
- sourceHeight = sourceHeight * 2;
- }
- // X position in sprite.
- let sourceX =
- (sourceWidth * this.size) * (0.5 * (this.size - 1)) + this.spritePos.x;
- // Animation frames.
- if (this.currentFrame > 0) {
- sourceX += sourceWidth * this.currentFrame;
- }
- this.canvasCtx.drawImage(
- this.imageSprite, sourceX, this.spritePos.y, sourceWidth * this.size,
- sourceHeight, this.xPos, this.yPos, this.typeConfig.width * this.size,
- this.typeConfig.height);
- },
- /**
- * Obstacle frame update.
- * @param {number} deltaTime
- * @param {number} speed
- */
- update(deltaTime, speed) {
- if (!this.remove) {
- if (this.typeConfig.speedOffset) {
- speed += this.speedOffset;
- }
- this.xPos -= Math.floor((speed * FPS / 1000) * deltaTime);
- // Update frame
- if (this.typeConfig.numFrames) {
- this.timer += deltaTime;
- if (this.timer >= this.typeConfig.frameRate) {
- this.currentFrame =
- this.currentFrame === this.typeConfig.numFrames - 1 ?
- 0 :
- this.currentFrame + 1;
- this.timer = 0;
- }
- }
- this.draw();
- if (!this.isVisible()) {
- this.remove = true;
- }
- }
- },
- /**
- * Calculate a random gap size.
- * - Minimum gap gets wider as speed increses
- * @param {number} gapCoefficient
- * @param {number} speed
- * @return {number} The gap size.
- */
- getGap(gapCoefficient, speed) {
- const minGap = Math.round(
- this.width * speed + this.typeConfig.minGap * gapCoefficient);
- const maxGap = Math.round(minGap * Obstacle.MAX_GAP_COEFFICIENT);
- return getRandomNum(minGap, maxGap);
- },
- /**
- * Check if obstacle is visible.
- * @return {boolean} Whether the obstacle is in the game area.
- */
- isVisible() {
- return this.xPos + this.width > 0;
- },
- /**
- * Make a copy of the collision boxes, since these will change based on
- * obstacle type and size.
- */
- cloneCollisionBoxes() {
- const collisionBoxes = this.typeConfig.collisionBoxes;
- for (let i = collisionBoxes.length - 1; i >= 0; i--) {
- this.collisionBoxes[i] = new CollisionBox(
- collisionBoxes[i].x, collisionBoxes[i].y, collisionBoxes[i].width,
- collisionBoxes[i].height);
- }
- },
- };
- //******************************************************************************
- /**
- * T-rex game character.
- * @param {HTMLCanvasElement} canvas
- * @param {Object} spritePos Positioning within image sprite.
- * @constructor
- */
- function Trex(canvas, spritePos) {
- this.canvas = canvas;
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
- this.spritePos = spritePos;
- this.xPos = 0;
- this.yPos = 0;
- this.xInitialPos = 0;
- // Position when on the ground.
- this.groundYPos = 0;
- this.currentFrame = 0;
- this.currentAnimFrames = [];
- this.blinkDelay = 0;
- this.blinkCount = 0;
- this.animStartTime = 0;
- this.timer = 0;
- this.msPerFrame = 1000 / FPS;
- this.config = Object.assign(Trex.config, Trex.normalJumpConfig);
- // Current status.
- this.status = Trex.status.WAITING;
- this.jumping = false;
- this.ducking = false;
- this.jumpVelocity = 0;
- this.reachedMinHeight = false;
- this.speedDrop = false;
- this.jumpCount = 0;
- this.jumpspotX = 0;
- this.altGameModeEnabled = false;
- this.flashing = false;
- this.init();
- }
- /**
- * T-rex player config.
- */
- Trex.config = {
- DROP_VELOCITY: -5,
- FLASH_OFF: 175,
- FLASH_ON: 100,
- HEIGHT: 47,
- HEIGHT_DUCK: 25,
- INTRO_DURATION: 1500,
- SPEED_DROP_COEFFICIENT: 3,
- SPRITE_WIDTH: 262,
- START_X_POS: 50,
- WIDTH: 44,
- WIDTH_DUCK: 59,
- };
- Trex.slowJumpConfig = {
- GRAVITY: 0.25,
- MAX_JUMP_HEIGHT: 50,
- MIN_JUMP_HEIGHT: 45,
- INITIAL_JUMP_VELOCITY: -20,
- };
- Trex.normalJumpConfig = {
- GRAVITY: 0.6,
- MAX_JUMP_HEIGHT: 30,
- MIN_JUMP_HEIGHT: 30,
- INITIAL_JUMP_VELOCITY: -10,
- };
- /**
- * Used in collision detection.
- * @enum {Array<CollisionBox>}
- */
- Trex.collisionBoxes = {
- DUCKING: [new CollisionBox(1, 18, 55, 25)],
- RUNNING: [
- new CollisionBox(22, 0, 17, 16),
- new CollisionBox(1, 18, 30, 9),
- new CollisionBox(10, 35, 14, 8),
- new CollisionBox(1, 24, 29, 5),
- new CollisionBox(5, 30, 21, 4),
- new CollisionBox(9, 34, 15, 4),
- ],
- };
- /**
- * Animation states.
- * @enum {string}
- */
- Trex.status = {
- CRASHED: 'CRASHED',
- DUCKING: 'DUCKING',
- JUMPING: 'JUMPING',
- RUNNING: 'RUNNING',
- WAITING: 'WAITING',
- };
- /**
- * Blinking coefficient.
- * @const
- */
- Trex.BLINK_TIMING = 7000;
- /**
- * Animation config for different states.
- * @enum {Object}
- */
- Trex.animFrames = {
- WAITING: {
- frames: [44, 0],
- msPerFrame: 1000 / 3,
- },
- RUNNING: {
- frames: [88, 132],
- msPerFrame: 1000 / 12,
- },
- CRASHED: {
- frames: [220],
- msPerFrame: 1000 / 60,
- },
- JUMPING: {
- frames: [0],
- msPerFrame: 1000 / 60,
- },
- DUCKING: {
- frames: [264, 323],
- msPerFrame: 1000 / 8,
- },
- };
- Trex.prototype = {
- /**
- * T-rex player initaliser.
- * Sets the t-rex to blink at random intervals.
- */
- init() {
- this.groundYPos = Runner.defaultDimensions.HEIGHT - this.config.HEIGHT -
- Runner.config.BOTTOM_PAD;
- this.yPos = this.groundYPos;
- this.minJumpHeight = this.groundYPos - this.config.MIN_JUMP_HEIGHT;
- this.draw(0, 0);
- this.update(0, Trex.status.WAITING);
- },
- /**
- * Assign the appropriate jump parameters based on the game speed.
- */
- enableSlowConfig: function() {
- const jumpConfig =
- Runner.slowDown ? Trex.slowJumpConfig : Trex.normalJumpConfig;
- Trex.config = Object.assign(Trex.config, jumpConfig);
- this.adjustAltGameConfigForSlowSpeed();
- },
- /**
- * Enables the alternative game. Redefines the dino config.
- * @param {Object} spritePos New positioning within image sprite.
- */
- enableAltGameMode: function(spritePos) {
- this.altGameModeEnabled = true;
- this.spritePos = spritePos;
- const spriteDefinition = Runner.spriteDefinition['TREX'];
- // Update animation frames.
- Trex.animFrames.RUNNING.frames =
- [spriteDefinition.RUNNING_1.x, spriteDefinition.RUNNING_2.x];
- Trex.animFrames.CRASHED.frames = [spriteDefinition.CRASHED.x];
- if (typeof spriteDefinition.JUMPING.x == 'object') {
- Trex.animFrames.JUMPING.frames = spriteDefinition.JUMPING.x;
- } else {
- Trex.animFrames.JUMPING.frames = [spriteDefinition.JUMPING.x];
- }
- Trex.animFrames.DUCKING.frames =
- [spriteDefinition.RUNNING_1.x, spriteDefinition.RUNNING_2.x];
- // Update Trex config
- Trex.config.GRAVITY = spriteDefinition.GRAVITY || Trex.config.GRAVITY;
- Trex.config.HEIGHT = spriteDefinition.RUNNING_1.h,
- Trex.config.INITIAL_JUMP_VELOCITY = spriteDefinition.INITIAL_JUMP_VELOCITY;
- Trex.config.MAX_JUMP_HEIGHT = spriteDefinition.MAX_JUMP_HEIGHT;
- Trex.config.MIN_JUMP_HEIGHT = spriteDefinition.MIN_JUMP_HEIGHT;
- Trex.config.WIDTH = spriteDefinition.RUNNING_1.w;
- Trex.config.WIDTH_JUMP = spriteDefinition.JUMPING.w;
- Trex.config.INVERT_JUMP = spriteDefinition.INVERT_JUMP;
- this.adjustAltGameConfigForSlowSpeed(spriteDefinition.GRAVITY);
- this.config = Trex.config;
- // Adjust bottom horizon placement.
- this.groundYPos = Runner.defaultDimensions.HEIGHT - this.config.HEIGHT -
- Runner.spriteDefinition['BOTTOM_PAD'];
- this.yPos = this.groundYPos;
- this.reset();
- },
- /**
- * Slow speeds adjustments for the alt game modes.
- * @param {number=} opt_gravityValue
- */
- adjustAltGameConfigForSlowSpeed: function(opt_gravityValue) {
- if (Runner.slowDown) {
- if (opt_gravityValue) {
- Trex.config.GRAVITY = opt_gravityValue / 1.5;
- }
- Trex.config.MIN_JUMP_HEIGHT *= 1.5;
- Trex.config.MAX_JUMP_HEIGHT *= 1.5;
- Trex.config.INITIAL_JUMP_VELOCITY =
- Trex.config.INITIAL_JUMP_VELOCITY * 1.5;
- }
- },
- /**
- * Setter whether dino is flashing.
- * @param {boolean} status
- */
- setFlashing: function(status) {
- this.flashing = status;
- },
- /**
- * Setter for the jump velocity.
- * The approriate drop velocity is also set.
- * @param {number} setting
- */
- setJumpVelocity(setting) {
- this.config.INITIAL_JUMP_VELOCITY = -setting;
- this.config.DROP_VELOCITY = -setting / 2;
- },
- /**
- * Set the animation status.
- * @param {!number} deltaTime
- * @param {Trex.status=} opt_status Optional status to switch to.
- */
- update(deltaTime, opt_status) {
- this.timer += deltaTime;
- // Update the status.
- if (opt_status) {
- this.status = opt_status;
- this.currentFrame = 0;
- this.msPerFrame = Trex.animFrames[opt_status].msPerFrame;
- this.currentAnimFrames = Trex.animFrames[opt_status].frames;
- if (opt_status === Trex.status.WAITING) {
- this.animStartTime = getTimeStamp();
- this.setBlinkDelay();
- }
- }
- // Game intro animation, T-rex moves in from the left.
- if (this.playingIntro && this.xPos < this.config.START_X_POS) {
- this.xPos += Math.round((this.config.START_X_POS /
- this.config.INTRO_DURATION) * deltaTime);
- this.xInitialPos = this.xPos;
- }
- if (this.status === Trex.status.WAITING) {
- this.blink(getTimeStamp());
- } else {
- this.draw(this.currentAnimFrames[this.currentFrame], 0);
- }
- // Update the frame position.
- if (!this.flashing && this.timer >= this.msPerFrame) {
- this.currentFrame = this.currentFrame ==
- this.currentAnimFrames.length - 1 ? 0 : this.currentFrame + 1;
- this.timer = 0;
- }
- if (!this.altGameModeEnabled) {
- // Speed drop becomes duck if the down key is still being pressed.
- if (this.speedDrop && this.yPos === this.groundYPos) {
- this.speedDrop = false;
- this.setDuck(true);
- }
- }
- },
- /**
- * Draw the t-rex to a particular position.
- * @param {number} x
- * @param {number} y
- */
- draw(x, y) {
- let sourceX = x;
- let sourceY = y;
- let sourceWidth = this.ducking && this.status !== Trex.status.CRASHED ?
- this.config.WIDTH_DUCK :
- this.config.WIDTH;
- let sourceHeight = this.config.HEIGHT;
- const outputHeight = sourceHeight;
- let jumpOffset = Runner.spriteDefinition.TREX.JUMPING.xOffset;
- // Width of sprite changes on jump.
- if (this.altGameModeEnabled && this.jumping &&
- this.status !== Trex.status.CRASHED) {
- sourceWidth = this.config.WIDTH_JUMP;
- }
- if (IS_HIDPI) {
- sourceX *= 2;
- sourceY *= 2;
- sourceWidth *= 2;
- sourceHeight *= 2;
- jumpOffset *= 2;
- }
- // Adjustments for sprite sheet position.
- sourceX += this.spritePos.x;
- sourceY += this.spritePos.y;
- // Flashing.
- if (this.flashing) {
- if (this.timer < this.config.FLASH_ON) {
- this.canvasCtx.globalAlpha = 0.5;
- } else if (this.timer > this.config.FLASH_OFF) {
- this.timer = 0;
- }
- }
- // Ducking.
- if (!this.altGameModeEnabled && this.ducking &&
- this.status !== Trex.status.CRASHED) {
- this.canvasCtx.drawImage(Runner.imageSprite, sourceX, sourceY,
- sourceWidth, sourceHeight,
- this.xPos, this.yPos,
- this.config.WIDTH_DUCK, outputHeight);
- } else if (
- this.altGameModeEnabled && this.jumping &&
- this.status !== Trex.status.CRASHED) {
- // Jumping with adjustments.
- this.canvasCtx.drawImage(
- Runner.imageSprite, sourceX, sourceY, sourceWidth, sourceHeight,
- this.xPos - jumpOffset, this.yPos, this.config.WIDTH_JUMP,
- outputHeight);
- } else {
- // Crashed whilst ducking. Trex is standing up so needs adjustment.
- if (this.ducking && this.status === Trex.status.CRASHED) {
- this.xPos++;
- }
- // Standing / running
- this.canvasCtx.drawImage(Runner.imageSprite, sourceX, sourceY,
- sourceWidth, sourceHeight,
- this.xPos, this.yPos,
- this.config.WIDTH, outputHeight);
- }
- this.canvasCtx.globalAlpha = 1;
- },
- /**
- * Sets a random time for the blink to happen.
- */
- setBlinkDelay() {
- this.blinkDelay = Math.ceil(Math.random() * Trex.BLINK_TIMING);
- },
- /**
- * Make t-rex blink at random intervals.
- * @param {number} time Current time in milliseconds.
- */
- blink(time) {
- const deltaTime = time - this.animStartTime;
- if (deltaTime >= this.blinkDelay) {
- this.draw(this.currentAnimFrames[this.currentFrame], 0);
- if (this.currentFrame === 1) {
- // Set new random delay to blink.
- this.setBlinkDelay();
- this.animStartTime = time;
- this.blinkCount++;
- }
- }
- },
- /**
- * Initialise a jump.
- * @param {number} speed
- */
- startJump(speed) {
- if (!this.jumping) {
- this.update(0, Trex.status.JUMPING);
- // Tweak the jump velocity based on the speed.
- this.jumpVelocity = this.config.INITIAL_JUMP_VELOCITY - (speed / 10);
- this.jumping = true;
- this.reachedMinHeight = false;
- this.speedDrop = false;
- if (this.config.INVERT_JUMP) {
- this.minJumpHeight = this.groundYPos + this.config.MIN_JUMP_HEIGHT;
- }
- }
- },
- /**
- * Jump is complete, falling down.
- */
- endJump() {
- if (this.reachedMinHeight &&
- this.jumpVelocity < this.config.DROP_VELOCITY) {
- this.jumpVelocity = this.config.DROP_VELOCITY;
- }
- },
- /**
- * Update frame for a jump.
- * @param {number} deltaTime
- */
- updateJump(deltaTime) {
- const msPerFrame = Trex.animFrames[this.status].msPerFrame;
- const framesElapsed = deltaTime / msPerFrame;
- // Speed drop makes Trex fall faster.
- if (this.speedDrop) {
- this.yPos += Math.round(this.jumpVelocity *
- this.config.SPEED_DROP_COEFFICIENT * framesElapsed);
- } else if (this.config.INVERT_JUMP) {
- this.yPos -= Math.round(this.jumpVelocity * framesElapsed);
- } else {
- this.yPos += Math.round(this.jumpVelocity * framesElapsed);
- }
- this.jumpVelocity += this.config.GRAVITY * framesElapsed;
- // Minimum height has been reached.
- if (this.config.INVERT_JUMP && (this.yPos > this.minJumpHeight) ||
- !this.config.INVERT_JUMP && (this.yPos < this.minJumpHeight) ||
- this.speedDrop) {
- this.reachedMinHeight = true;
- }
- // Reached max height.
- if (this.config.INVERT_JUMP && (this.yPos > -this.config.MAX_JUMP_HEIGHT) ||
- !this.config.INVERT_JUMP && (this.yPos < this.config.MAX_JUMP_HEIGHT) ||
- this.speedDrop) {
- this.endJump();
- }
- // Back down at ground level. Jump completed.
- if ((this.config.INVERT_JUMP && this.yPos) < this.groundYPos ||
- (!this.config.INVERT_JUMP && this.yPos) > this.groundYPos) {
- this.reset();
- this.jumpCount++;
- if (Runner.audioCues) {
- Runner.generatedSoundFx.loopFootSteps();
- }
- }
- },
- /**
- * Set the speed drop. Immediately cancels the current jump.
- */
- setSpeedDrop() {
- this.speedDrop = true;
- this.jumpVelocity = 1;
- },
- /**
- * @param {boolean} isDucking
- */
- setDuck(isDucking) {
- if (isDucking && this.status !== Trex.status.DUCKING) {
- this.update(0, Trex.status.DUCKING);
- this.ducking = true;
- } else if (this.status === Trex.status.DUCKING) {
- this.update(0, Trex.status.RUNNING);
- this.ducking = false;
- }
- },
- /**
- * Reset the t-rex to running at start of game.
- */
- reset() {
- this.xPos = this.xInitialPos;
- this.yPos = this.groundYPos;
- this.jumpVelocity = 0;
- this.jumping = false;
- this.ducking = false;
- this.update(0, Trex.status.RUNNING);
- this.midair = false;
- this.speedDrop = false;
- this.jumpCount = 0;
- },
- };
- //******************************************************************************
- /**
- * Handles displaying the distance meter.
- * @param {!HTMLCanvasElement} canvas
- * @param {Object} spritePos Image position in sprite.
- * @param {number} canvasWidth
- * @constructor
- */
- function DistanceMeter(canvas, spritePos, canvasWidth) {
- this.canvas = canvas;
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
- this.image = Runner.imageSprite;
- this.spritePos = spritePos;
- this.x = 0;
- this.y = 5;
- this.currentDistance = 0;
- this.maxScore = 0;
- this.highScore = '0';
- this.container = null;
- this.digits = [];
- this.achievement = false;
- this.defaultString = '';
- this.flashTimer = 0;
- this.flashIterations = 0;
- this.invertTrigger = false;
- this.flashingRafId = null;
- this.highScoreBounds = {};
- this.highScoreFlashing = false;
- this.config = DistanceMeter.config;
- this.maxScoreUnits = this.config.MAX_DISTANCE_UNITS;
- this.canvasWidth = canvasWidth;
- this.init(canvasWidth);
- }
- /**
- * @enum {number}
- */
- DistanceMeter.dimensions = {
- WIDTH: 10,
- HEIGHT: 13,
- DEST_WIDTH: 11,
- };
- /**
- * Y positioning of the digits in the sprite sheet.
- * X position is always 0.
- * @type {Array<number>}
- */
- DistanceMeter.yPos = [0, 13, 27, 40, 53, 67, 80, 93, 107, 120];
- /**
- * Distance meter config.
- * @enum {number}
- */
- DistanceMeter.config = {
- // Number of digits.
- MAX_DISTANCE_UNITS: 5,
- // Distance that causes achievement animation.
- ACHIEVEMENT_DISTANCE: 100,
- // Used for conversion from pixel distance to a scaled unit.
- COEFFICIENT: 0.025,
- // Flash duration in milliseconds.
- FLASH_DURATION: 1000 / 4,
- // Flash iterations for achievement animation.
- FLASH_ITERATIONS: 3,
- // Padding around the high score hit area.
- HIGH_SCORE_HIT_AREA_PADDING: 4,
- };
- DistanceMeter.prototype = {
- /**
- * Initialise the distance meter to '00000'.
- * @param {number} width Canvas width in px.
- */
- init(width) {
- let maxDistanceStr = '';
- this.calcXPos(width);
- this.maxScore = this.maxScoreUnits;
- for (let i = 0; i < this.maxScoreUnits; i++) {
- this.draw(i, 0);
- this.defaultString += '0';
- maxDistanceStr += '9';
- }
- this.maxScore = parseInt(maxDistanceStr, 10);
- },
- /**
- * Calculate the xPos in the canvas.
- * @param {number} canvasWidth
- */
- calcXPos(canvasWidth) {
- this.x = canvasWidth - (DistanceMeter.dimensions.DEST_WIDTH *
- (this.maxScoreUnits + 1));
- },
- /**
- * Draw a digit to canvas.
- * @param {number} digitPos Position of the digit.
- * @param {number} value Digit value 0-9.
- * @param {boolean=} opt_highScore Whether drawing the high score.
- */
- draw(digitPos, value, opt_highScore) {
- let sourceWidth = DistanceMeter.dimensions.WIDTH;
- let sourceHeight = DistanceMeter.dimensions.HEIGHT;
- let sourceX = DistanceMeter.dimensions.WIDTH * value;
- let sourceY = 0;
- const targetX = digitPos * DistanceMeter.dimensions.DEST_WIDTH;
- const targetY = this.y;
- const targetWidth = DistanceMeter.dimensions.WIDTH;
- const targetHeight = DistanceMeter.dimensions.HEIGHT;
- // For high DPI we 2x source values.
- if (IS_HIDPI) {
- sourceWidth *= 2;
- sourceHeight *= 2;
- sourceX *= 2;
- }
- sourceX += this.spritePos.x;
- sourceY += this.spritePos.y;
- this.canvasCtx.save();
- if (IS_RTL) {
- if (opt_highScore) {
- this.canvasCtx.translate(
- this.canvasWidth -
- (DistanceMeter.dimensions.WIDTH * (this.maxScoreUnits + 3)),
- this.y);
- } else {
- this.canvasCtx.translate(
- this.canvasWidth - DistanceMeter.dimensions.WIDTH, this.y);
- }
- this.canvasCtx.scale(-1, 1);
- } else {
- const highScoreX =
- this.x - (this.maxScoreUnits * 2) * DistanceMeter.dimensions.WIDTH;
- if (opt_highScore) {
- this.canvasCtx.translate(highScoreX, this.y);
- } else {
- this.canvasCtx.translate(this.x, this.y);
- }
- }
- this.canvasCtx.drawImage(
- this.image,
- sourceX,
- sourceY,
- sourceWidth,
- sourceHeight,
- targetX,
- targetY,
- targetWidth,
- targetHeight,
- );
- this.canvasCtx.restore();
- },
- /**
- * Covert pixel distance to a 'real' distance.
- * @param {number} distance Pixel distance ran.
- * @return {number} The 'real' distance ran.
- */
- getActualDistance(distance) {
- return distance ? Math.round(distance * this.config.COEFFICIENT) : 0;
- },
- /**
- * Update the distance meter.
- * @param {number} distance
- * @param {number} deltaTime
- * @return {boolean} Whether the acheivement sound fx should be played.
- */
- update(deltaTime, distance) {
- let paint = true;
- let playSound = false;
- if (!this.achievement) {
- distance = this.getActualDistance(distance);
- // Score has gone beyond the initial digit count.
- if (distance > this.maxScore && this.maxScoreUnits ==
- this.config.MAX_DISTANCE_UNITS) {
- this.maxScoreUnits++;
- this.maxScore = parseInt(this.maxScore + '9', 10);
- } else {
- this.distance = 0;
- }
- if (distance > 0) {
- // Achievement unlocked.
- if (distance % this.config.ACHIEVEMENT_DISTANCE === 0) {
- // Flash score and play sound.
- this.achievement = true;
- this.flashTimer = 0;
- playSound = true;
- }
- // Create a string representation of the distance with leading 0.
- const distanceStr = (this.defaultString +
- distance).substr(-this.maxScoreUnits);
- this.digits = distanceStr.split('');
- } else {
- this.digits = this.defaultString.split('');
- }
- } else {
- // Control flashing of the score on reaching acheivement.
- if (this.flashIterations <= this.config.FLASH_ITERATIONS) {
- this.flashTimer += deltaTime;
- if (this.flashTimer < this.config.FLASH_DURATION) {
- paint = false;
- } else if (this.flashTimer > this.config.FLASH_DURATION * 2) {
- this.flashTimer = 0;
- this.flashIterations++;
- }
- } else {
- this.achievement = false;
- this.flashIterations = 0;
- this.flashTimer = 0;
- }
- }
- // Draw the digits if not flashing.
- if (paint) {
- for (let i = this.digits.length - 1; i >= 0; i--) {
- this.draw(i, parseInt(this.digits[i], 10));
- }
- }
- this.drawHighScore();
- return playSound;
- },
- /**
- * Draw the high score.
- */
- drawHighScore() {
- if (parseInt(this.highScore, 10) > 0) {
- this.canvasCtx.save();
- this.canvasCtx.globalAlpha = .8;
- for (let i = this.highScore.length - 1; i >= 0; i--) {
- this.draw(i, parseInt(this.highScore[i], 10), true);
- }
- this.canvasCtx.restore();
- }
- },
- /**
- * Set the highscore as a array string.
- * Position of char in the sprite: H - 10, I - 11.
- * @param {number} distance Distance ran in pixels.
- */
- setHighScore(distance) {
- distance = this.getActualDistance(distance);
- const highScoreStr = (this.defaultString +
- distance).substr(-this.maxScoreUnits);
- this.highScore = ['10', '11', ''].concat(highScoreStr.split(''));
- },
- /**
- * Whether a clicked is in the high score area.
- * @param {Event} e Event object.
- * @return {boolean} Whether the click was in the high score bounds.
- */
- hasClickedOnHighScore(e) {
- let x = 0;
- let y = 0;
- if (e.touches) {
- // Bounds for touch differ from pointer.
- const canvasBounds = this.canvas.getBoundingClientRect();
- x = e.touches[0].clientX - canvasBounds.left;
- y = e.touches[0].clientY - canvasBounds.top;
- } else {
- x = e.offsetX;
- y = e.offsetY;
- }
- this.highScoreBounds = this.getHighScoreBounds();
- return x >= this.highScoreBounds.x && x <=
- this.highScoreBounds.x + this.highScoreBounds.width &&
- y >= this.highScoreBounds.y && y <=
- this.highScoreBounds.y + this.highScoreBounds.height;
- },
- /**
- * Get the bounding box for the high score.
- * @return {Object} Object with x, y, width and height properties.
- */
- getHighScoreBounds() {
- return {
- x: (this.x - (this.maxScoreUnits * 2) * DistanceMeter.dimensions.WIDTH) -
- DistanceMeter.config.HIGH_SCORE_HIT_AREA_PADDING,
- y: this.y,
- width: DistanceMeter.dimensions.WIDTH * (this.highScore.length + 1) +
- DistanceMeter.config.HIGH_SCORE_HIT_AREA_PADDING,
- height: DistanceMeter.dimensions.HEIGHT +
- (DistanceMeter.config.HIGH_SCORE_HIT_AREA_PADDING * 2),
- };
- },
- /**
- * Animate flashing the high score to indicate ready for resetting.
- * The flashing stops following this.config.FLASH_ITERATIONS x 2 flashes.
- */
- flashHighScore() {
- const now = getTimeStamp();
- const deltaTime = now - (this.frameTimeStamp || now);
- let paint = true;
- this.frameTimeStamp = now;
- // Reached the max number of flashes.
- if (this.flashIterations > this.config.FLASH_ITERATIONS * 2) {
- this.cancelHighScoreFlashing();
- return;
- }
- this.flashTimer += deltaTime;
- if (this.flashTimer < this.config.FLASH_DURATION) {
- paint = false;
- } else if (this.flashTimer > this.config.FLASH_DURATION * 2) {
- this.flashTimer = 0;
- this.flashIterations++;
- }
- if (paint) {
- this.drawHighScore();
- } else {
- this.clearHighScoreBounds();
- }
- // Frame update.
- this.flashingRafId =
- requestAnimationFrame(this.flashHighScore.bind(this));
- },
- /**
- * Draw empty rectangle over high score.
- */
- clearHighScoreBounds() {
- this.canvasCtx.save();
- this.canvasCtx.fillStyle = '#fff';
- this.canvasCtx.rect(this.highScoreBounds.x, this.highScoreBounds.y,
- this.highScoreBounds.width, this.highScoreBounds.height);
- this.canvasCtx.fill();
- this.canvasCtx.restore();
- },
- /**
- * Starts the flashing of the high score.
- */
- startHighScoreFlashing() {
- this.highScoreFlashing = true;
- this.flashHighScore();
- },
- /**
- * Whether high score is flashing.
- * @return {boolean}
- */
- isHighScoreFlashing() {
- return this.highScoreFlashing;
- },
- /**
- * Stop flashing the high score.
- */
- cancelHighScoreFlashing() {
- if (this.flashingRafId) {
- cancelAnimationFrame(this.flashingRafId);
- }
- this.flashIterations = 0;
- this.flashTimer = 0;
- this.highScoreFlashing = false;
- this.clearHighScoreBounds();
- this.drawHighScore();
- },
- /**
- * Clear the high score.
- */
- resetHighScore() {
- this.setHighScore(0);
- this.cancelHighScoreFlashing();
- },
- /**
- * Reset the distance meter back to '00000'.
- */
- reset() {
- this.update(0, 0);
- this.achievement = false;
- },
- };
- //******************************************************************************
- /**
- * Cloud background item.
- * Similar to an obstacle object but without collision boxes.
- * @param {HTMLCanvasElement} canvas Canvas element.
- * @param {Object} spritePos Position of image in sprite.
- * @param {number} containerWidth
- * @constructor
- */
- function Cloud(canvas, spritePos, containerWidth) {
- this.canvas = canvas;
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (this.canvas.getContext('2d'));
- this.spritePos = spritePos;
- this.containerWidth = containerWidth;
- this.xPos = containerWidth;
- this.yPos = 0;
- this.remove = false;
- this.gap =
- getRandomNum(Cloud.config.MIN_CLOUD_GAP, Cloud.config.MAX_CLOUD_GAP);
- this.init();
- }
- /**
- * Cloud object config.
- * @enum {number}
- */
- Cloud.config = {
- HEIGHT: 14,
- MAX_CLOUD_GAP: 400,
- MAX_SKY_LEVEL: 30,
- MIN_CLOUD_GAP: 100,
- MIN_SKY_LEVEL: 71,
- WIDTH: 46,
- };
- Cloud.prototype = {
- /**
- * Initialise the cloud. Sets the Cloud height.
- */
- init() {
- this.yPos = getRandomNum(Cloud.config.MAX_SKY_LEVEL,
- Cloud.config.MIN_SKY_LEVEL);
- this.draw();
- },
- /**
- * Draw the cloud.
- */
- draw() {
- this.canvasCtx.save();
- let sourceWidth = Cloud.config.WIDTH;
- let sourceHeight = Cloud.config.HEIGHT;
- const outputWidth = sourceWidth;
- const outputHeight = sourceHeight;
- if (IS_HIDPI) {
- sourceWidth = sourceWidth * 2;
- sourceHeight = sourceHeight * 2;
- }
- this.canvasCtx.drawImage(Runner.imageSprite, this.spritePos.x,
- this.spritePos.y,
- sourceWidth, sourceHeight,
- this.xPos, this.yPos,
- outputWidth, outputHeight);
- this.canvasCtx.restore();
- },
- /**
- * Update the cloud position.
- * @param {number} speed
- */
- update(speed) {
- if (!this.remove) {
- this.xPos -= Math.ceil(speed);
- this.draw();
- // Mark as removeable if no longer in the canvas.
- if (!this.isVisible()) {
- this.remove = true;
- }
- }
- },
- /**
- * Check if the cloud is visible on the stage.
- * @return {boolean}
- */
- isVisible() {
- return this.xPos + Cloud.config.WIDTH > 0;
- },
- };
- /**
- * Background item.
- * Similar to cloud, without random y position.
- * @param {HTMLCanvasElement} canvas Canvas element.
- * @param {Object} spritePos Position of image in sprite.
- * @param {number} containerWidth
- * @param {string} type Element type.
- * @constructor
- */
- function BackgroundEl(canvas, spritePos, containerWidth, type) {
- this.canvas = canvas;
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (this.canvas.getContext('2d'));
- this.spritePos = spritePos;
- this.containerWidth = containerWidth;
- this.xPos = containerWidth;
- this.yPos = 0;
- this.remove = false;
- this.type = type;
- this.gap =
- getRandomNum(BackgroundEl.config.MIN_GAP, BackgroundEl.config.MAX_GAP);
- this.animTimer = 0;
- this.switchFrames = false;
- this.spriteConfig = {};
- this.init();
- }
- /**
- * Background element object config.
- * Real values assigned when game type changes.
- * @enum {number}
- */
- BackgroundEl.config = {
- MAX_BG_ELS: 0,
- MAX_GAP: 0,
- MIN_GAP: 0,
- POS: 0,
- SPEED: 0,
- Y_POS: 0,
- MS_PER_FRAME: 0, // only needed when BACKGROUND_EL.FIXED is true
- };
- BackgroundEl.prototype = {
- /**
- * Initialise the element setting the y position.
- */
- init() {
- this.spriteConfig = Runner.spriteDefinition.BACKGROUND_EL[this.type];
- if (this.spriteConfig.FIXED) {
- this.xPos = this.spriteConfig.FIXED_X_POS;
- }
- this.yPos = BackgroundEl.config.Y_POS - this.spriteConfig.HEIGHT +
- this.spriteConfig.OFFSET;
- this.draw();
- },
- /**
- * Draw the element.
- */
- draw() {
- this.canvasCtx.save();
- let sourceWidth = this.spriteConfig.WIDTH;
- let sourceHeight = this.spriteConfig.HEIGHT;
- let sourceX = this.spriteConfig.X_POS;
- const outputWidth = sourceWidth;
- const outputHeight = sourceHeight;
- if (IS_HIDPI) {
- sourceWidth *= 2;
- sourceHeight *= 2;
- sourceX *= 2;
- }
- this.canvasCtx.drawImage(
- Runner.imageSprite, sourceX, this.spritePos.y, sourceWidth,
- sourceHeight, this.xPos, this.yPos, outputWidth, outputHeight);
- this.canvasCtx.restore();
- },
- /**
- * Update the background element position.
- * @param {number} speed
- */
- update(speed) {
- if (!this.remove) {
- if (this.spriteConfig.FIXED) {
- this.animTimer += speed;
- if (this.animTimer > BackgroundEl.config.MS_PER_FRAME) {
- this.animTimer = 0;
- this.switchFrames = !this.switchFrames;
- }
- if (this.spriteConfig.FIXED_Y_POS_1 &&
- this.spriteConfig.FIXED_Y_POS_2) {
- this.yPos = this.switchFrames ? this.spriteConfig.FIXED_Y_POS_1 :
- this.spriteConfig.FIXED_Y_POS_2;
- }
- } else {
- // Fixed speed, regardless of actual game speed.
- this.xPos -= BackgroundEl.config.SPEED;
- }
- this.draw();
- // Mark as removable if no longer in the canvas.
- if (!this.isVisible()) {
- this.remove = true;
- }
- }
- },
- /**
- * Check if the element is visible on the stage.
- * @return {boolean}
- */
- isVisible() {
- return this.xPos + this.spriteConfig.WIDTH > 0;
- },
- };
- //******************************************************************************
- /**
- * Nightmode shows a moon and stars on the horizon.
- * @param {HTMLCanvasElement} canvas
- * @param {number} spritePos
- * @param {number} containerWidth
- * @constructor
- */
- function NightMode(canvas, spritePos, containerWidth) {
- this.spritePos = spritePos;
- this.canvas = canvas;
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
- this.xPos = containerWidth - 50;
- this.yPos = 30;
- this.currentPhase = 0;
- this.opacity = 0;
- this.containerWidth = containerWidth;
- this.stars = [];
- this.drawStars = false;
- this.placeStars();
- }
- /**
- * @enum {number}
- */
- NightMode.config = {
- FADE_SPEED: 0.035,
- HEIGHT: 40,
- MOON_SPEED: 0.25,
- NUM_STARS: 2,
- STAR_SIZE: 9,
- STAR_SPEED: 0.3,
- STAR_MAX_Y: 70,
- WIDTH: 20,
- };
- NightMode.phases = [140, 120, 100, 60, 40, 20, 0];
- NightMode.prototype = {
- /**
- * Update moving moon, changing phases.
- * @param {boolean} activated Whether night mode is activated.
- */
- update(activated) {
- // Moon phase.
- if (activated && this.opacity === 0) {
- this.currentPhase++;
- if (this.currentPhase >= NightMode.phases.length) {
- this.currentPhase = 0;
- }
- }
- // Fade in / out.
- if (activated && (this.opacity < 1 || this.opacity === 0)) {
- this.opacity += NightMode.config.FADE_SPEED;
- } else if (this.opacity > 0) {
- this.opacity -= NightMode.config.FADE_SPEED;
- }
- // Set moon positioning.
- if (this.opacity > 0) {
- this.xPos = this.updateXPos(this.xPos, NightMode.config.MOON_SPEED);
- // Update stars.
- if (this.drawStars) {
- for (let i = 0; i < NightMode.config.NUM_STARS; i++) {
- this.stars[i].x =
- this.updateXPos(this.stars[i].x, NightMode.config.STAR_SPEED);
- }
- }
- this.draw();
- } else {
- this.opacity = 0;
- this.placeStars();
- }
- this.drawStars = true;
- },
- updateXPos(currentPos, speed) {
- if (currentPos < -NightMode.config.WIDTH) {
- currentPos = this.containerWidth;
- } else {
- currentPos -= speed;
- }
- return currentPos;
- },
- draw() {
- let moonSourceWidth = this.currentPhase === 3 ? NightMode.config.WIDTH * 2 :
- NightMode.config.WIDTH;
- let moonSourceHeight = NightMode.config.HEIGHT;
- let moonSourceX = this.spritePos.x + NightMode.phases[this.currentPhase];
- const moonOutputWidth = moonSourceWidth;
- let starSize = NightMode.config.STAR_SIZE;
- let starSourceX = Runner.spriteDefinitionByType.original.LDPI.STAR.x;
- if (IS_HIDPI) {
- moonSourceWidth *= 2;
- moonSourceHeight *= 2;
- moonSourceX = this.spritePos.x +
- (NightMode.phases[this.currentPhase] * 2);
- starSize *= 2;
- starSourceX = Runner.spriteDefinitionByType.original.HDPI.STAR.x;
- }
- this.canvasCtx.save();
- this.canvasCtx.globalAlpha = this.opacity;
- // Stars.
- if (this.drawStars) {
- for (let i = 0; i < NightMode.config.NUM_STARS; i++) {
- this.canvasCtx.drawImage(
- Runner.origImageSprite, starSourceX, this.stars[i].sourceY,
- starSize, starSize, Math.round(this.stars[i].x), this.stars[i].y,
- NightMode.config.STAR_SIZE, NightMode.config.STAR_SIZE);
- }
- }
- // Moon.
- this.canvasCtx.drawImage(
- Runner.origImageSprite, moonSourceX, this.spritePos.y, moonSourceWidth,
- moonSourceHeight, Math.round(this.xPos), this.yPos, moonOutputWidth,
- NightMode.config.HEIGHT);
- this.canvasCtx.globalAlpha = 1;
- this.canvasCtx.restore();
- },
- // Do star placement.
- placeStars() {
- const segmentSize = Math.round(this.containerWidth /
- NightMode.config.NUM_STARS);
- for (let i = 0; i < NightMode.config.NUM_STARS; i++) {
- this.stars[i] = {};
- this.stars[i].x = getRandomNum(segmentSize * i, segmentSize * (i + 1));
- this.stars[i].y = getRandomNum(0, NightMode.config.STAR_MAX_Y);
- if (IS_HIDPI) {
- this.stars[i].sourceY =
- Runner.spriteDefinitionByType.original.HDPI.STAR.y +
- NightMode.config.STAR_SIZE * 2 * i;
- } else {
- this.stars[i].sourceY =
- Runner.spriteDefinitionByType.original.LDPI.STAR.y +
- NightMode.config.STAR_SIZE * i;
- }
- }
- },
- reset() {
- this.currentPhase = 0;
- this.opacity = 0;
- this.update(false);
- },
- };
- //******************************************************************************
- /**
- * Horizon Line.
- * Consists of two connecting lines. Randomly assigns a flat / bumpy horizon.
- * @param {HTMLCanvasElement} canvas
- * @param {Object} lineConfig Configuration object.
- * @constructor
- */
- function HorizonLine(canvas, lineConfig) {
- let sourceX = lineConfig.SOURCE_X;
- let sourceY = lineConfig.SOURCE_Y;
- if (IS_HIDPI) {
- sourceX *= 2;
- sourceY *= 2;
- }
- this.spritePos = {x: sourceX, y: sourceY};
- this.canvas = canvas;
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
- this.sourceDimensions = {};
- this.dimensions = lineConfig;
- this.sourceXPos = [this.spritePos.x, this.spritePos.x +
- this.dimensions.WIDTH];
- this.xPos = [];
- this.yPos = 0;
- this.bumpThreshold = 0.5;
- this.setSourceDimensions(lineConfig);
- this.draw();
- }
- /**
- * Horizon line dimensions.
- * @enum {number}
- */
- HorizonLine.dimensions = {
- WIDTH: 600,
- HEIGHT: 12,
- YPOS: 127,
- };
- HorizonLine.prototype = {
- /**
- * Set the source dimensions of the horizon line.
- */
- setSourceDimensions(newDimensions) {
- for (const dimension in newDimensions) {
- if (dimension !== 'SOURCE_X' && dimension !== 'SOURCE_Y') {
- if (IS_HIDPI) {
- if (dimension !== 'YPOS') {
- this.sourceDimensions[dimension] = newDimensions[dimension] * 2;
- }
- } else {
- this.sourceDimensions[dimension] = newDimensions[dimension];
- }
- this.dimensions[dimension] = newDimensions[dimension];
- }
- }
- this.xPos = [0, newDimensions.WIDTH];
- this.yPos = newDimensions.YPOS;
- },
- /**
- * Return the crop x position of a type.
- */
- getRandomType() {
- return Math.random() > this.bumpThreshold ? this.dimensions.WIDTH : 0;
- },
- /**
- * Draw the horizon line.
- */
- draw() {
- this.canvasCtx.drawImage(Runner.imageSprite, this.sourceXPos[0],
- this.spritePos.y,
- this.sourceDimensions.WIDTH, this.sourceDimensions.HEIGHT,
- this.xPos[0], this.yPos,
- this.dimensions.WIDTH, this.dimensions.HEIGHT);
- this.canvasCtx.drawImage(Runner.imageSprite, this.sourceXPos[1],
- this.spritePos.y,
- this.sourceDimensions.WIDTH, this.sourceDimensions.HEIGHT,
- this.xPos[1], this.yPos,
- this.dimensions.WIDTH, this.dimensions.HEIGHT);
- },
- /**
- * Update the x position of an indivdual piece of the line.
- * @param {number} pos Line position.
- * @param {number} increment
- */
- updateXPos(pos, increment) {
- const line1 = pos;
- const line2 = pos === 0 ? 1 : 0;
- this.xPos[line1] -= increment;
- this.xPos[line2] = this.xPos[line1] + this.dimensions.WIDTH;
- if (this.xPos[line1] <= -this.dimensions.WIDTH) {
- this.xPos[line1] += this.dimensions.WIDTH * 2;
- this.xPos[line2] = this.xPos[line1] - this.dimensions.WIDTH;
- this.sourceXPos[line1] = this.getRandomType() + this.spritePos.x;
- }
- },
- /**
- * Update the horizon line.
- * @param {number} deltaTime
- * @param {number} speed
- */
- update(deltaTime, speed) {
- const increment = Math.floor(speed * (FPS / 1000) * deltaTime);
- if (this.xPos[0] <= 0) {
- this.updateXPos(0, increment);
- } else {
- this.updateXPos(1, increment);
- }
- this.draw();
- },
- /**
- * Reset horizon to the starting position.
- */
- reset() {
- this.xPos[0] = 0;
- this.xPos[1] = this.dimensions.WIDTH;
- },
- };
- //******************************************************************************
- /**
- * Horizon background class.
- * @param {HTMLCanvasElement} canvas
- * @param {Object} spritePos Sprite positioning.
- * @param {Object} dimensions Canvas dimensions.
- * @param {number} gapCoefficient
- * @constructor
- */
- function Horizon(canvas, spritePos, dimensions, gapCoefficient) {
- this.canvas = canvas;
- this.canvasCtx =
- /** @type {CanvasRenderingContext2D} */ (this.canvas.getContext('2d'));
- this.config = Horizon.config;
- this.dimensions = dimensions;
- this.gapCoefficient = gapCoefficient;
- this.obstacles = [];
- this.obstacleHistory = [];
- this.horizonOffsets = [0, 0];
- this.cloudFrequency = this.config.CLOUD_FREQUENCY;
- this.spritePos = spritePos;
- this.nightMode = null;
- this.altGameModeActive = false;
- // Cloud
- this.clouds = [];
- this.cloudSpeed = this.config.BG_CLOUD_SPEED;
- // Background elements
- this.backgroundEls = [];
- this.lastEl = null;
- this.backgroundSpeed = this.config.BG_CLOUD_SPEED;
- // Horizon
- this.horizonLine = null;
- this.horizonLines = [];
- this.init();
- }
- /**
- * Horizon config.
- * @enum {number}
- */
- Horizon.config = {
- BG_CLOUD_SPEED: 0.2,
- BUMPY_THRESHOLD: .3,
- CLOUD_FREQUENCY: .5,
- HORIZON_HEIGHT: 16,
- MAX_CLOUDS: 6,
- };
- Horizon.prototype = {
- /**
- * Initialise the horizon. Just add the line and a cloud. No obstacles.
- */
- init() {
- Obstacle.types = Runner.spriteDefinitionByType.original.OBSTACLES;
- this.addCloud();
- // Multiple Horizon lines
- for (let i = 0; i < Runner.spriteDefinition.LINES.length; i++) {
- this.horizonLines.push(
- new HorizonLine(this.canvas, Runner.spriteDefinition.LINES[i]));
- }
- this.nightMode = new NightMode(this.canvas, this.spritePos.MOON,
- this.dimensions.WIDTH);
- },
- /**
- * Update obstacle definitions based on the speed of the game.
- */
- adjustObstacleSpeed: function() {
- for (let i = 0; i < Obstacle.types.length; i++) {
- if (Runner.slowDown) {
- Obstacle.types[i].multipleSpeed = Obstacle.types[i].multipleSpeed / 2;
- Obstacle.types[i].minGap *= 1.5;
- Obstacle.types[i].minSpeed = Obstacle.types[i].minSpeed / 2;
- // Convert variable y position obstacles to fixed.
- if (typeof (Obstacle.types[i].yPos) == 'object') {
- Obstacle.types[i].yPos = Obstacle.types[i].yPos[0];
- Obstacle.types[i].yPosMobile = Obstacle.types[i].yPos[0];
- }
- }
- }
- },
- /**
- * Update sprites to correspond to change in sprite sheet.
- * @param {number} spritePos
- */
- enableAltGameMode: function(spritePos) {
- // Clear existing horizon objects.
- this.clouds = [];
- this.backgroundEls = [];
- this.altGameModeActive = true;
- this.spritePos = spritePos;
- Obstacle.types = Runner.spriteDefinition.OBSTACLES;
- this.adjustObstacleSpeed();
- Obstacle.MAX_GAP_COEFFICIENT = Runner.spriteDefinition.MAX_GAP_COEFFICIENT;
- Obstacle.MAX_OBSTACLE_LENGTH = Runner.spriteDefinition.MAX_OBSTACLE_LENGTH;
- BackgroundEl.config = Runner.spriteDefinition.BACKGROUND_EL_CONFIG;
- this.horizonLines = [];
- for (let i = 0; i < Runner.spriteDefinition.LINES.length; i++) {
- this.horizonLines.push(
- new HorizonLine(this.canvas, Runner.spriteDefinition.LINES[i]));
- }
- this.reset();
- },
- /**
- * @param {number} deltaTime
- * @param {number} currentSpeed
- * @param {boolean} updateObstacles Used as an override to prevent
- * the obstacles from being updated / added. This happens in the
- * ease in section.
- * @param {boolean} showNightMode Night mode activated.
- */
- update(deltaTime, currentSpeed, updateObstacles, showNightMode) {
- this.runningTime += deltaTime;
- if (this.altGameModeActive) {
- this.updateBackgroundEls(deltaTime, currentSpeed);
- }
- for (let i = 0; i < this.horizonLines.length; i++) {
- this.horizonLines[i].update(deltaTime, currentSpeed);
- }
- if (!this.altGameModeActive || Runner.spriteDefinition.HAS_CLOUDS) {
- this.nightMode.update(showNightMode);
- this.updateClouds(deltaTime, currentSpeed);
- }
- if (updateObstacles) {
- this.updateObstacles(deltaTime, currentSpeed);
- }
- },
- /**
- * Update background element positions. Also handles creating new elements.
- * @param {number} elSpeed
- * @param {Array<Object>} bgElArray
- * @param {number} maxBgEl
- * @param {Function} bgElAddFunction
- * @param {number} frequency
- */
- updateBackgroundEl(elSpeed, bgElArray, maxBgEl, bgElAddFunction, frequency) {
- const numElements = bgElArray.length;
- if (numElements) {
- for (let i = numElements - 1; i >= 0; i--) {
- bgElArray[i].update(elSpeed);
- }
- const lastEl = bgElArray[numElements - 1];
- // Check for adding a new element.
- if (numElements < maxBgEl &&
- (this.dimensions.WIDTH - lastEl.xPos) > lastEl.gap &&
- frequency > Math.random()) {
- bgElAddFunction();
- }
- } else {
- bgElAddFunction();
- }
- },
- /**
- * Update the cloud positions.
- * @param {number} deltaTime
- * @param {number} speed
- */
- updateClouds(deltaTime, speed) {
- const elSpeed = this.cloudSpeed / 1000 * deltaTime * speed;
- this.updateBackgroundEl(
- elSpeed, this.clouds, this.config.MAX_CLOUDS, this.addCloud.bind(this),
- this.cloudFrequency);
- // Remove expired elements.
- this.clouds = this.clouds.filter((obj) => !obj.remove);
- },
- /**
- * Update the background element positions.
- * @param {number} deltaTime
- * @param {number} speed
- */
- updateBackgroundEls(deltaTime, speed) {
- this.updateBackgroundEl(
- deltaTime, this.backgroundEls, BackgroundEl.config.MAX_BG_ELS,
- this.addBackgroundEl.bind(this), this.cloudFrequency);
- // Remove expired elements.
- this.backgroundEls = this.backgroundEls.filter((obj) => !obj.remove);
- },
- /**
- * Update the obstacle positions.
- * @param {number} deltaTime
- * @param {number} currentSpeed
- */
- updateObstacles(deltaTime, currentSpeed) {
- const updatedObstacles = this.obstacles.slice(0);
- for (let i = 0; i < this.obstacles.length; i++) {
- const obstacle = this.obstacles[i];
- obstacle.update(deltaTime, currentSpeed);
- // Clean up existing obstacles.
- if (obstacle.remove) {
- updatedObstacles.shift();
- }
- }
- this.obstacles = updatedObstacles;
- if (this.obstacles.length > 0) {
- const lastObstacle = this.obstacles[this.obstacles.length - 1];
- if (lastObstacle && !lastObstacle.followingObstacleCreated &&
- lastObstacle.isVisible() &&
- (lastObstacle.xPos + lastObstacle.width + lastObstacle.gap) <
- this.dimensions.WIDTH) {
- this.addNewObstacle(currentSpeed);
- lastObstacle.followingObstacleCreated = true;
- }
- } else {
- // Create new obstacles.
- this.addNewObstacle(currentSpeed);
- }
- },
- removeFirstObstacle() {
- this.obstacles.shift();
- },
- /**
- * Add a new obstacle.
- * @param {number} currentSpeed
- */
- addNewObstacle(currentSpeed) {
- const obstacleCount =
- Obstacle.types[Obstacle.types.length - 1].type != 'COLLECTABLE' ||
- (Runner.isAltGameModeEnabled() && !this.altGameModeActive ||
- this.altGameModeActive) ?
- Obstacle.types.length - 1 :
- Obstacle.types.length - 2;
- const obstacleTypeIndex =
- obstacleCount > 0 ? getRandomNum(0, obstacleCount) : 0;
- const obstacleType = Obstacle.types[obstacleTypeIndex];
- // Check for multiples of the same type of obstacle.
- // Also check obstacle is available at current speed.
- if ((obstacleCount > 0 && this.duplicateObstacleCheck(obstacleType.type)) ||
- currentSpeed < obstacleType.minSpeed) {
- this.addNewObstacle(currentSpeed);
- } else {
- const obstacleSpritePos = this.spritePos[obstacleType.type];
- this.obstacles.push(new Obstacle(
- this.canvasCtx, obstacleType, obstacleSpritePos, this.dimensions,
- this.gapCoefficient, currentSpeed, obstacleType.width,
- this.altGameModeActive));
- this.obstacleHistory.unshift(obstacleType.type);
- if (this.obstacleHistory.length > 1) {
- this.obstacleHistory.splice(Runner.config.MAX_OBSTACLE_DUPLICATION);
- }
- }
- },
- /**
- * Returns whether the previous two obstacles are the same as the next one.
- * Maximum duplication is set in config value MAX_OBSTACLE_DUPLICATION.
- * @return {boolean}
- */
- duplicateObstacleCheck(nextObstacleType) {
- let duplicateCount = 0;
- for (let i = 0; i < this.obstacleHistory.length; i++) {
- duplicateCount =
- this.obstacleHistory[i] === nextObstacleType ? duplicateCount + 1 : 0;
- }
- return duplicateCount >= Runner.config.MAX_OBSTACLE_DUPLICATION;
- },
- /**
- * Reset the horizon layer.
- * Remove existing obstacles and reposition the horizon line.
- */
- reset() {
- this.obstacles = [];
- for (let l = 0; l < this.horizonLines.length; l++) {
- this.horizonLines[l].reset();
- }
- this.nightMode.reset();
- },
- /**
- * Update the canvas width and scaling.
- * @param {number} width Canvas width.
- * @param {number} height Canvas height.
- */
- resize(width, height) {
- this.canvas.width = width;
- this.canvas.height = height;
- },
- /**
- * Add a new cloud to the horizon.
- */
- addCloud() {
- this.clouds.push(new Cloud(this.canvas, this.spritePos.CLOUD,
- this.dimensions.WIDTH));
- },
- /**
- * Add a random background element to the horizon.
- */
- addBackgroundEl() {
- const backgroundElTypes =
- Object.keys(Runner.spriteDefinition.BACKGROUND_EL);
- if (backgroundElTypes.length > 0) {
- let index = getRandomNum(0, backgroundElTypes.length - 1);
- let type = backgroundElTypes[index];
- // Add variation if available.
- while (type == this.lastEl && backgroundElTypes.length > 1) {
- index = getRandomNum(0, backgroundElTypes.length - 1);
- type = backgroundElTypes[index];
- }
- this.lastEl = type;
- this.backgroundEls.push(new BackgroundEl(
- this.canvas, this.spritePos.BACKGROUND_EL, this.dimensions.WIDTH,
- type));
- }
- },
- };
|