Best JavaScript code snippet using mountebank
agent_types.js
Source:agent_types.js  
1/*!2 * Fierce Planet - AgentTypes3 *4 * Copyright (C) 2011 Liam Magee5 * MIT Licensed6 */7/*8Agent Type setup9 */10var AgentTypes = function() {};11/**12 *13 * @param cellWidth14 * @param x - the leftmost x co-ordinate for drawing the figure15 * @param y - the topmost y co-ordinate for drawing the figure16 */17function AgentStickFigure(x, _y, _figureWidth, _figureHeight) {18    var x = x, y = _y, figureWidth = _figureWidth, figureHeight = _figureHeight;19    var wholeBodyLength = (figureHeight * 1);20    var headRadius = (wholeBodyLength / 8) + 0.5 | 0;21    var bodyLength = (wholeBodyLength / 3) + 0.5 | 0;22    var shoulderPoint = (bodyLength / 3) + 0.5 | 0;23    var shoulderToElbowLength = (wholeBodyLength / 8) + 0.5 | 0;24    var elbowToHandLength = (wholeBodyLength / 6) + 0.5 | 0;25    var hipToKneeLength = (wholeBodyLength / 6) + 0.5 | 0;26    var kneeToFootLength = (wholeBodyLength / 6) + 0.5 | 0;27    var startOfHeadY = y - headRadius;28    var startOfBodyY = y + headRadius;29    var startOfShoulderY = startOfBodyY + shoulderPoint;30    var startOfHipY = startOfBodyY + bodyLength;31    var defaultAngle = Math.PI / 4;32    var fShoulderAngle = defaultAngle,33            fElbowAngle = defaultAngle,34            bShoulderAngle = defaultAngle,35            bElbowAngle = defaultAngle,36            fHipAngle = defaultAngle,37            fKneeAngle = defaultAngle,38            bHipAngle = defaultAngle,39            bKneeAngle = defaultAngle;40    var fElbowX, fElbowY,41        bElbowX, bElbowY,42        fHandX, fHandY,43        bHandX, bHandY,44        fKneeX, fKneeY,45        bKneeX, bKneeY,46        fFootX, fFootY,47        bFootX, bFootY;48    // Stick figure actions49    this.run = function(frame, direction) {50        switch (frame) {51            case 0:52                fShoulderAngle = Math.PI * (12 / 12);53                fElbowAngle = Math.PI * (6 / 12);54                bShoulderAngle = Math.PI * (4 / 12);55                bElbowAngle = Math.PI * (20 / 12);56                fHipAngle = Math.PI * (9 / 12);57                fKneeAngle = Math.PI * (9 / 12);58                bHipAngle = Math.PI * (1 / 12);59                bKneeAngle = Math.PI * (7 / 12);60                break;61            case 1:62                fShoulderAngle = Math.PI * (9 / 12);63                fElbowAngle = Math.PI * (3 / 12);64                bShoulderAngle = Math.PI * (5 / 12);65                bElbowAngle = Math.PI * (21 / 12);66                fHipAngle = Math.PI * (10 / 12);67                fKneeAngle = Math.PI * (15 / 12);68                bHipAngle = Math.PI * (3 / 12);69                bKneeAngle = Math.PI * (4 / 12);70                break;71            case 2:72                fShoulderAngle = Math.PI * (6 / 12);73                fElbowAngle = Math.PI * (0 / 12);74                bShoulderAngle = Math.PI * (9 / 12);75                bElbowAngle = Math.PI * (2 / 12);76                fHipAngle = Math.PI * (5 / 12);77                fKneeAngle = Math.PI * (13 / 12);78                bHipAngle = Math.PI * (6 / 12);79                bKneeAngle = Math.PI * (7 / 12);80                break;81        }82        if (direction == 1) {83            this.flipHorizontalDirection();84        }85        this.generateCoordinates();86    };87    // Stick figure actions88    this.runUpsideDown = function(frame, direction) {89        this.run(frame, direction);90        this.flipVerticalDirection();91        this.generateCoordinates();92    };93    // Stick figure actions94    this.walk = function(frame, direction) {95        switch (frame) {96            case 0:97                fShoulderAngle = Math.PI * (9 / 12);98                fElbowAngle = Math.PI * (8 / 12);99                bShoulderAngle = Math.PI * (4 / 12);100                bElbowAngle = Math.PI * (4 / 12);101                fHipAngle = Math.PI * (8 / 12);102                fKneeAngle = Math.PI * (9 / 12);103                bHipAngle = Math.PI * (4 / 12);104                bKneeAngle = Math.PI * (4 / 12);105                break;106            case 1:107                fShoulderAngle = Math.PI * (7 / 12);108                fElbowAngle = Math.PI * (6 / 12);109                bShoulderAngle = Math.PI * (6 / 12);110                bElbowAngle = Math.PI * (5 / 12);111                fHipAngle = Math.PI * (7 / 12);112                fKneeAngle = Math.PI * (8 / 12);113                bHipAngle = Math.PI * (5 / 12);114                bKneeAngle = Math.PI * (6 / 12);115                break;116            case 2:117                fShoulderAngle = Math.PI * (5 / 12);118                fElbowAngle = Math.PI * (4 / 12);119                bShoulderAngle = Math.PI * (7 / 12);120                bElbowAngle = Math.PI * (7 / 12);121                fHipAngle = Math.PI * (5 / 12);122                fKneeAngle = Math.PI * (6 / 12);123                bHipAngle = Math.PI * (7 / 12);124                bKneeAngle = Math.PI * (8 / 12);125                break;126        }127        if (direction == 1) {128            this.flipHorizontalDirection();129        }130        this.generateCoordinates();131    };132    this.flipHorizontalDirection = function() {133        fShoulderAngle = (Math.PI / 2) + ((Math.PI / 2) - fShoulderAngle);134        fElbowAngle = (Math.PI / 2) + ((Math.PI / 2) - fElbowAngle);135        bShoulderAngle = (Math.PI / 2) + ((Math.PI / 2) - bShoulderAngle);136        bElbowAngle = (Math.PI / 2) + ((Math.PI / 2) - bElbowAngle);137        fHipAngle = (Math.PI / 2) + ((Math.PI / 2) - fHipAngle);138        fKneeAngle = (Math.PI / 2) + ((Math.PI / 2) - fKneeAngle);139        bHipAngle = (Math.PI / 2) + ((Math.PI / 2) - bHipAngle);140        bKneeAngle = (Math.PI / 2) + ((Math.PI / 2) - bKneeAngle);141    };142    this.flipVerticalDirection = function() {143        y = y + figureHeight;144        wholeBodyLength = (figureHeight * 1);145        headRadius = (wholeBodyLength / 8) + 0.5 | 0;146        bodyLength = -bodyLength;147        shoulderPoint = -shoulderPoint;148        shoulderToElbowLength = -shoulderToElbowLength;149        elbowToHandLength = -elbowToHandLength;150        hipToKneeLength = -hipToKneeLength;151        kneeToFootLength = -kneeToFootLength;152        startOfHeadY = y + headRadius;153        startOfBodyY = y - headRadius;154        startOfShoulderY = startOfBodyY + shoulderPoint;155        startOfHipY = startOfBodyY + bodyLength;156    };157    this.generateCoordinates = function() {158        fElbowX = (x + Math.cos(fShoulderAngle) * shoulderToElbowLength);159        fElbowY = (startOfShoulderY + Math.sin(fShoulderAngle) * shoulderToElbowLength);160        fHandX = (fElbowX + Math.cos(fElbowAngle) * elbowToHandLength);161        fHandY = (fElbowY + Math.sin(fElbowAngle) * elbowToHandLength);162        bElbowX = (x + Math.cos(bShoulderAngle) * shoulderToElbowLength);163        bElbowY = (startOfShoulderY + Math.sin(bShoulderAngle) * shoulderToElbowLength);164        bHandX = (bElbowX + Math.cos(bElbowAngle) * elbowToHandLength);165        bHandY = (fElbowY + Math.sin(bElbowAngle) * elbowToHandLength);166        fKneeX = (x + Math.cos(fHipAngle) * hipToKneeLength);167        fKneeY = (startOfHipY + Math.sin(fHipAngle) * hipToKneeLength);168        fFootX = (fKneeX + Math.cos(fKneeAngle) * kneeToFootLength);169        fFootY = (fKneeY + Math.sin(fKneeAngle) * kneeToFootLength);170        bKneeX = (x + Math.cos(bHipAngle) * hipToKneeLength);171        bKneeY = (startOfHipY + Math.sin(bHipAngle) * hipToKneeLength);172        bFootX = (bKneeX + Math.cos(bKneeAngle) * kneeToFootLength);173        bFootY = (bKneeY + Math.sin(bKneeAngle) * kneeToFootLength);174    };175    this.drawFigure = function(context) {176        context.beginPath();177        // Head178        context.arc(x, y, headRadius, 0, Math.PI * 2, false);179        // Body180        context.moveTo(x, startOfBodyY);181        context.lineTo(x, startOfBodyY + bodyLength);182        // Front arm183        context.moveTo(x, startOfShoulderY);184        context.lineTo(fElbowX, fElbowY);185        context.moveTo(fElbowX, fElbowY);186        context.lineTo(fHandX, fHandY);187        // Back arm188        context.moveTo(x, startOfShoulderY);189        context.lineTo(bElbowX, bElbowY);190        context.moveTo(bElbowX, bElbowY);191        context.lineTo(bHandX, bHandY);192        // Front leg193        context.moveTo(x, startOfHipY);194        context.lineTo(fKneeX, fKneeY);195        context.moveTo(fKneeX, fKneeY);196        context.lineTo(fFootX, fFootY);197        // Back leg198        context.moveTo(x, startOfHipY);199        context.lineTo(bKneeX, bKneeY);200        context.moveTo(bKneeX, bKneeY);201        context.lineTo(bFootX, bFootY);202        context.closePath();203    };204    this.defaultAction = this.run;205}206/**207 * Register the default agent types208 */209(function() {210    AgentTypes.CITIZEN_AGENT_TYPE = new AgentType("Citizen", "000", World.resourceCategories);211    AgentTypes.CITIZEN_AGENT_TYPE.isHitable = (true);212    AgentTypes.CITIZEN_AGENT_TYPE.drawFunction = (function(ctx, agent, x, y, pieceWidth, pieceHeight, newColor, counter, direction) {213        if (pieceWidth < 8 || pieceHeight < 8) {214            var radius = (pieceWidth / 4);215            ctx.lineWidth = 1.5;216            ctx.beginPath();217            ctx.arc(x + radius, y + radius, radius, 0, Math.PI * 2, false);218            ctx.closePath();219            ctx.strokeStyle = "#ccc";220            ctx.stroke();221            ctx.fillStyle = "#" + newColor;222            ctx.fill();223        }224        else {225            // Define agent elements here226            var frames = 3;227            var speed = agent.speed;228            var countdown = agent.countdownToMove;229            var frame = Math.floor((countdown / (speed + 1)) * frames);230            var sf = new AgentStickFigure(x, y, pieceWidth, pieceHeight);231            if (speed > 5)232                sf.defaultAction = sf.walk;233            else234                sf.defaultAction = sf.run;235            sf.defaultAction(frame, direction);236            sf.drawFigure(ctx);237            // Now draw the figure238            ctx.lineWidth = 1.5;239            ctx.strokeStyle = "#" + newColor;240            ctx.lineCap = "round";241            ctx.stroke();242            ctx.fillStyle = "#" + newColor;243            ctx.fill();244        }245    });246    AgentTypes.CITIZEN_AGENT_TYPE.drawExpired = function(ctx, agent, x, y, pieceWidth, pieceHeight, newColor, counter, direction) {247        // Draw an explosion here248        var explosionX = x;249        var explosionY = y  + pieceWidth / 2;250        var radgrad = ctx.createRadialGradient(explosionX,explosionY,0,explosionX,explosionY,pieceWidth / 2);251          radgrad.addColorStop(0, 'rgba(255, 168, 81,1)');252          radgrad.addColorStop(0.8, '#FFF354');253          radgrad.addColorStop(1, 'rgba(255, 168, 81,0)');254        ctx.fillStyle = radgrad ;255        ctx.fillRect(x - pieceWidth / 2, y, pieceWidth, pieceHeight);256        if (pieceWidth < 8 || pieceHeight < 8) {257            var radius = (pieceWidth / 4);258            ctx.lineWidth = 2;259            ctx.beginPath();260            ctx.arc(x + radius, y + radius, radius, 0, Math.PI * 2, false);261            ctx.closePath();262            ctx.strokeStyle = "#ccc";263            ctx.stroke();264            ctx.fillStyle = "#" + newColor;265            ctx.fill();266        }267        else {268            // Define agent elements here269            // Quick round: + 0.5 | 0270            var wholeBodyLength = (pieceWidth * 1);271            var headRadius = (wholeBodyLength / 8) + 0.5 | 0;272            var bodyLength = (wholeBodyLength / 3) + 0.5 | 0;273            var shoulderPoint = (bodyLength / 3) + 0.5 | 0;274            var shoulderToElbowLength = (wholeBodyLength / 8) + 0.5 | 0;275            var elbowToHandLength = (wholeBodyLength / 6) + 0.5 | 0;276            var hipToKneeLength = (wholeBodyLength / 6) + 0.5 | 0;277            var kneeToFootLength = (wholeBodyLength / 6) + 0.5 | 0;278            var startOfHeadY = y - headRadius;279            var startOfBodyY = y + headRadius;280            var startOfShoulderY = startOfBodyY + shoulderPoint;281            var startOfHipY = startOfBodyY + bodyLength;282            ctx.lineWidth = 1;283            ctx.beginPath();284            // Angles285            var fShoulderAngle, fElbowAngle, bShoulderAngle, bElbowAngle;286            var fHipAngle, fKneeAngle, bHipAngle, bKneeAngle;287            fShoulderAngle = Math.PI * (14 / 12);288            fElbowAngle = Math.PI * (14 / 12);289            bShoulderAngle = Math.PI * (22 / 12);290            bElbowAngle = Math.PI * (22 / 12);291            fHipAngle = Math.PI * (10 / 12);292            fKneeAngle = Math.PI * (10 / 12);293            bHipAngle = Math.PI * (2 / 12);294            bKneeAngle = Math.PI * (2 / 12);295            // Head296            ctx.arc(x, y, headRadius, 0, Math.PI * 2, false);297            // Body298            ctx.moveTo(x, startOfBodyY);299            ctx.lineTo(x, startOfBodyY + bodyLength);300            // Front arm301            ctx.moveTo(x, startOfShoulderY);302            var fElbowX = (x + Math.cos(fShoulderAngle) * shoulderToElbowLength);303            var fElbowY = (startOfShoulderY + Math.sin(fShoulderAngle) * shoulderToElbowLength);304            ctx.lineTo(fElbowX, fElbowY);305            ctx.moveTo(fElbowX, fElbowY);306            var fHandX = (fElbowX + Math.cos(fElbowAngle) * elbowToHandLength);307            var fHandY = (fElbowY + Math.sin(fElbowAngle) * elbowToHandLength);308            ctx.lineTo(fHandX, fHandY);309            // Back arm310            ctx.moveTo(x, startOfShoulderY);311            var bElbowX = (x + Math.cos(bShoulderAngle) * shoulderToElbowLength);312            var bElbowY = (startOfShoulderY + Math.sin(bShoulderAngle) * shoulderToElbowLength);313            ctx.lineTo(bElbowX, bElbowY);314            ctx.moveTo(bElbowX, bElbowY);315            var bHandX = (bElbowX + Math.cos(bElbowAngle) * elbowToHandLength);316            var bHandY = (fElbowY + Math.sin(bElbowAngle) * elbowToHandLength);317            ctx.lineTo(bHandX, bHandY);318            // Front leg319            ctx.moveTo(x, startOfHipY);320            var fKneeX = (x + Math.cos(fHipAngle) * hipToKneeLength);321            var fKneeY = (startOfHipY + Math.sin(fHipAngle) * hipToKneeLength);322            ctx.lineTo(fKneeX, fKneeY);323            ctx.moveTo(fKneeX, fKneeY);324            var fFootX = (fKneeX + Math.cos(fKneeAngle) * kneeToFootLength);325            var fFootY = (fKneeY + Math.sin(fKneeAngle) * kneeToFootLength);326            ctx.lineTo(fFootX, fFootY);327            // Back leg328            ctx.moveTo(x, startOfHipY);329            var bKneeX = (x + Math.cos(bHipAngle) * hipToKneeLength);330            var bKneeY = (startOfHipY + Math.sin(bHipAngle) * hipToKneeLength);331            ctx.lineTo(bKneeX, bKneeY);332            ctx.moveTo(bKneeX, bKneeY);333            var bFootX = (bKneeX + Math.cos(bKneeAngle) * kneeToFootLength);334            var bFootY = (bKneeY + Math.sin(bKneeAngle) * kneeToFootLength);335            ctx.lineTo(bFootX, bFootY);336            ctx.closePath();337            ctx.strokeStyle = "#" + newColor;338            ctx.lineCap = "round";339            ctx.stroke();340            ctx.fillStyle = "#" + newColor;341            ctx.fill();342        }343    };344    AgentTypes.PREDATOR_AGENT_TYPE = new AgentType("Predator", "fbe53b", World.resourceCategories);345    AgentTypes.PREDATOR_AGENT_TYPE.canHit = (true);346    AgentTypes.PREDATOR_AGENT_TYPE.drawFunction = (function(ctx, agent, intX, intY, pieceWidth, pieceHeight, newColor, counter, direction) {347        var radius = (pieceWidth / 4);348        var bodyLength = (pieceWidth / 2);349        var img = new Image();350//    img.src = "/images/agents/fierce_planet_monster1.png";351        if (counter % 4 == 0) {352            img.src = "/images/agents/monster1.png";353        }354        else if (counter % 4 == 1) {355            img.src = "/images/agents/monster2.png";356        }357        else if (counter % 4 == 2) {358            img.src = "/images/agents/monster1.png";359        }360        else {361            img.src = "/images/agents/monster3.png";362        }363        ctx.drawImage(img, intX - pieceWidth / 2, intY - pieceWidth / 2, pieceWidth, pieceWidth);364    });365    AgentTypes.RIVAL_AGENT_TYPE = new AgentType("Rival", "3be5fb", World.resourceCategories);366    AgentTypes.RIVAL_AGENT_TYPE.drawFunction = (function(ctx, agent, intX, intY, pieceWidth, pieceHeight, newColor, counter, direction) {367        var radius = (pieceWidth / 4);368        var bodyLength = (pieceWidth / 2);369        ctx.beginPath();370        ctx.arc(intX, intY, radius, 0, Math.PI * 2, false);371        ctx.closePath();372        ctx.strokeStyle = "#ccc";373        ctx.stroke();374        ctx.fillStyle = "#" + newColor;375        ctx.fill();376        ctx.beginPath();377        ctx.moveTo(intX, intY + radius);378        ctx.lineTo(intX, intY + radius + bodyLength / 2);379        if (counter % 2 == 0) {380            // Legs381            var xOffset = Math.sin(30 * Math.PI/180) * bodyLength / 2;382            var yOffset = Math.cos(30 * Math.PI/180) * bodyLength / 2;383            ctx.moveTo(intX, intY + radius + bodyLength / 2);384            ctx.lineTo(intX - xOffset, intY + radius + bodyLength / 2 + yOffset);385            ctx.moveTo(intX, intY + radius + bodyLength / 2);386            ctx.lineTo(intX + xOffset, intY + radius + bodyLength / 2 + yOffset);387            // Arms - 90 degrees388            ctx.moveTo(intX - bodyLength / 2, intY + radius + bodyLength / 6);389            ctx.lineTo(intX + bodyLength / 2, intY + radius + bodyLength / 6);390        }391        else {392            // Legs - straight393            ctx.moveTo(intX, intY + radius + bodyLength / 2);394            ctx.lineTo(intX, intY + radius + bodyLength);395            // Arms - 45 degrees396            var xOffset = Math.sin(45 * Math.PI/180) * bodyLength / 2;397            var yOffset = Math.cos(45 * Math.PI/180) * bodyLength / 2;398            ctx.moveTo(intX - xOffset, intY + radius + bodyLength / 6 + yOffset);399            ctx.lineTo(intX, intY + radius + bodyLength / 6);400            ctx.moveTo(intX + xOffset, intY + radius + bodyLength / 6 + yOffset);401            ctx.lineTo(intX, intY + radius + bodyLength / 6);402        }403        ctx.closePath();404        ctx.strokeStyle = "#" + newColor;405        ctx.lineWidth = 2;406        ctx.stroke();407    });408    World.registerAgentTypes([AgentTypes.CITIZEN_AGENT_TYPE, AgentTypes.PREDATOR_AGENT_TYPE, AgentTypes.RIVAL_AGENT_TYPE]);409})();410FiercePlanet.registerDefaultAgentTypes = function() {411    AgentTypes.CITIZEN_AGENT_TYPE = new AgentType("Citizen", "000", World.resourceCategories);412    AgentTypes.CITIZEN_AGENT_TYPE.drawFunction = (function(ctx, agent, x, y, pieceWidth, pieceHeight, newColor, counter, direction) {413        if (pieceWidth < 8 || pieceHeight < 8) {414            var radius = (pieceWidth / 4);415            ctx.lineWidth = 2;416            ctx.beginPath();417            ctx.arc(x + radius, y + radius, radius, 0, Math.PI * 2, false);418            ctx.closePath();419            ctx.strokeStyle = "#ccc";420            ctx.stroke();421            ctx.fillStyle = "#" + newColor;422            ctx.fill();423        }424        else {425            var radius = (pieceWidth / 4);426            ctx.lineWidth = 1;427            ctx.beginPath();428            ctx.arc(x, y, radius, 0, Math.PI * 2, false);429            ctx.closePath();430            ctx.strokeStyle = "#ccc";431            ctx.stroke();432            ctx.fillStyle = "#" + newColor;433            ctx.fill();434            var bodyLength = (pieceWidth / 2);435            ctx.beginPath();436            ctx.moveTo(x, y - radius + 6);437            ctx.lineTo(x - 1, y - radius + 6 + bodyLength);438            if (counter % 2 == 1) {439                var start = (direction == 0 ? -1 : 1);440                var end = (direction == 0 ? -4 : 4);441                // Arms442                ctx.moveTo(x + 4, y + 8 + 2 * start);443                ctx.lineTo(x - 4, y + 8 - 2 * start);444                // 1st leg445                ctx.moveTo(x, y - radius + 6 + bodyLength);446                ctx.lineTo(x + start + end, y - radius + 6 + bodyLength + Math.abs(end));447                // 2nd leg448                ctx.moveTo(x, y - radius + 5 + bodyLength);449                ctx.lineTo(x - start - end / 2, y - radius + 6 + bodyLength);450                ctx.moveTo(x - start - end / 2, y - radius + 6 + bodyLength);451                ctx.lineTo(x - start - end / 2, y - radius + 6 + bodyLength + Math.abs(end) / 2);452            }453            else {454                var start = (direction == 0 ? 1 : -1);455                var end = (direction == 0 ? 4 : -4);456                // Arms457                ctx.moveTo(x + 4, y + 8 + 2 * start);458                ctx.lineTo(x - 4, y + 8 - 2 * start);459                // 1st leg460                ctx.moveTo(x, y - radius + 6 + bodyLength);461//        ctx.lineTo(x + end, y  - radius + 8 + bodyLength);462//        ctx.moveTo(x + end, y - radius + 8 + bodyLength);463                ctx.lineTo(x + end, y - radius + 6 + bodyLength + Math.abs(end));464                // 2nd leg465                ctx.moveTo(x, y - radius + 6 + bodyLength);466                ctx.lineTo(x - end / 2, y - radius + 6 + bodyLength + Math.abs(end) / 2);467                ctx.moveTo(x - end / 2, y - radius + 6 + bodyLength + Math.abs(end) / 2);468                ctx.lineTo(x - end, y - radius + 6 + bodyLength);469            }470            ctx.closePath();471            ctx.strokeStyle = "#" + newColor;472            ctx.stroke();473            ctx.fillStyle = "#" + newColor;474            ctx.fill();475        }476    });477    AgentTypes.PREDATOR_AGENT_TYPE = new AgentType("Predator", "fbe53b", World.resourceCategories);478    AgentTypes.PREDATOR_AGENT_TYPE.drawFunction = (function(ctx, agent, intX, intY, pieceWidth, pieceHeight, newColor, counter, direction) {479        var radius = (pieceWidth / 4);480        var bodyLength = (pieceWidth / 2);481        var img = new Image();482//    img.src = "/images/agents/fierce_planet_monster1.png";483        if (counter % 4 == 0) {484            img.src = "/images/agents/monster1.png";485        }486        else if (counter % 4 == 1) {487            img.src = "/images/agents/monster2.png";488        }489        else if (counter % 4 == 2) {490            img.src = "/images/agents/monster1.png";491        }492        else {493            img.src = "/images/agents/monster3.png";494        }495        ctx.drawImage(img, intX - pieceWidth / 2, intY - pieceWidth / 2, pieceWidth, pieceWidth);496    });497    AgentTypes.RIVAL_AGENT_TYPE = new AgentType("Rival", "3be5fb", World.resourceCategories);498    AgentTypes.RIVAL_AGENT_TYPE.drawFunction = (function(ctx, agent, intX, intY, pieceWidth, pieceHeight, newColor, counter, direction) {499        var radius = (pieceWidth / 4);500        var bodyLength = (pieceWidth / 2);501        ctx.beginPath();502        ctx.arc(intX, intY, radius, 0, Math.PI * 2, false);503        ctx.closePath();504        ctx.strokeStyle = "#ccc";505        ctx.stroke();506        ctx.fillStyle = "#" + newColor;507        ctx.fill();508        ctx.beginPath();509        ctx.moveTo(intX, intY + radius);510        ctx.lineTo(intX, intY + radius + bodyLength / 2);511        if (counter % 2 == 0) {512            // Legs513            var xOffset = Math.sin(30 * Math.PI/180) * bodyLength / 2;514            var yOffset = Math.cos(30 * Math.PI/180) * bodyLength / 2;515            ctx.moveTo(intX, intY + radius + bodyLength / 2);516            ctx.lineTo(intX - xOffset, intY + radius + bodyLength / 2 + yOffset);517            ctx.moveTo(intX, intY + radius + bodyLength / 2);518            ctx.lineTo(intX + xOffset, intY + radius + bodyLength / 2 + yOffset);519            // Arms - 90 degrees520            ctx.moveTo(intX - bodyLength / 2, intY + radius + bodyLength / 6);521            ctx.lineTo(intX + bodyLength / 2, intY + radius + bodyLength / 6);522        }523        else {524            // Legs - straight525            ctx.moveTo(intX, intY + radius + bodyLength / 2);526            ctx.lineTo(intX, intY + radius + bodyLength);527            // Arms - 45 degrees528            var xOffset = Math.sin(45 * Math.PI/180) * bodyLength / 2;529            var yOffset = Math.cos(45 * Math.PI/180) * bodyLength / 2;530            ctx.moveTo(intX - xOffset, intY + radius + bodyLength / 6 + yOffset);531            ctx.lineTo(intX, intY + radius + bodyLength / 6);532            ctx.moveTo(intX + xOffset, intY + radius + bodyLength / 6 + yOffset);533            ctx.lineTo(intX, intY + radius + bodyLength / 6);534        }535        ctx.closePath();536        ctx.strokeStyle = "#" + newColor;537        ctx.lineWidth = 2;538        ctx.stroke();539    });...script.js
Source:script.js  
1const screen = document.getElementById("squad")2const context = screen.getContext("2d")3const screenWidth = screenHeight = 6004const snake = {5  color: "#fff",6  squadLength: screenWidth / 30,7  bodyLength: [[80, 20], [60, 20], [40, 20], [20, 20]],8  bodyLengthNumber: 4,9  speedX: 20,10  speedY: 20,11  direction: "right"12}13const apple = {14  color: "red",15  pos: [120, 120],16  length: snake.squadLength17}18function drawSnake() {19  for(var i = 0; i < snake.bodyLength.length; i++) {20    21    context.fillStyle = snake.color22    context.fillRect(snake.bodyLength[i][0], snake.bodyLength[i][1], snake.squadLength, snake.squadLength)23    context.lineWidth = 2;24    context.strokeStyle="#000";25    context.strokeRect(snake.bodyLength[i][0], snake.bodyLength[i][1], snake.squadLength, snake.squadLength)26  }27}28function moveSnake() {29  for(var i = 0; i < snake.bodyLength.length; ++i) {30    31    if(i <= 0) {32      if(snake.direction == "right") {33        snake.bodyLength[0][0] = snake.bodyLength[0][0] + snake.speedX34      } else if(snake.direction == "left") {35        snake.bodyLength[0][0] = snake.bodyLength[0][0] - snake.speedX36      } else if(snake.direction == "down") {37        snake.bodyLength[0][1] = snake.bodyLength[0][1] + snake.speedY38      } else if(snake.direction == "up") {39        snake.bodyLength[0][1] = snake.bodyLength[0][1] - snake.speedY40      }41    } else if(i < snake.bodyLengthNumber - 1) {42      if(snake.direction == "right") {43        snake.bodyLength[i][0] = snake.bodyLength[i - 1][0] - 2044        snake.bodyLength[i][1] = snake.bodyLength[i - 1][1]45      } else if(snake.direction == "left") {46        snake.bodyLength[i][0] = snake.bodyLength[i - 1][0] + 2047        snake.bodyLength[i][1] = snake.bodyLength[i - 1][1]48      } else if(snake.direction == "down") {49        snake.bodyLength[i][1] = snake.bodyLength[i - 1][1] - 2050        snake.bodyLength[i][0] = snake.bodyLength[i - 1][0]51      } else if(snake.direction == "up") {52        snake.bodyLength[i][1] = snake.bodyLength[i - 1][1] + 2053        snake.bodyLength[i][0] = snake.bodyLength[i - 1][0]54      }55    }56    drawSnake()57  }58}59// function eatApple() {60//   if(snake.bodyLength[0][0] == apple.pos[0][0] && snake.bodyLength[0][1] == apple.pos[0][1]) {61//     snake.bodyLength = [...snake.bodyLength, [snake.bodyLength[snake.bodyLengthNumber - 1][0] - 20, snake.bodyLength[snake.bodyLengthNumber - 1][1] - 20]]62//     console.log("Pegou")63//   }64// }65function changeSnakeDirection(direction) {66  if(direction == "ArrowUp") {67    snake.direction = "up"68  } else if(direction == "ArrowDown") {69    snake.direction = "down"70  } else if(direction == "ArrowRight") {71    snake.direction = "right"72  } else {73    snake.direction = "left"74  }75}76function wallColision() {77  if(snake.bodyLength[snake.bodyLengthNumber - 1][0] > screenWidth) {78    snake.bodyLength[0][0] = 079  }80  if(snake.bodyLength[snake.bodyLengthNumber - 1][1] > screenHeight) {81    snake.bodyLength[0][1] = 082  }83  if(snake.bodyLength[snake.bodyLengthNumber - 1][0] < 0) {84    snake.bodyLength[0][0] = screenWidth85  }86  if(snake.bodyLength[snake.bodyLengthNumber - 1][1] < 0) {87    snake.bodyLength[0][1] = screenHeight88  }89}90window.addEventListener("keydown", (e) => {91  changeSnakeDirection(e.key)92})93function game() {94  context.fillStyle = "#111"95  context.clearRect(0, 0, screenWidth, screenHeight)96  context.fillRect(0, 0, screenWidth, screenHeight)97  98  context.fillStyle = apple.color99  context.fillRect(apple.pos[0], apple.pos[1], apple.length, apple.length)100  101  moveSnake()102  wallColision()103  eatApple()104}...Project_Fish-Tank.js
Source:Project_Fish-Tank.js  
1background(89, 216, 255);2var bodyLength = random (0,200);3var bodyHeight = random (0,200);4var drawFish = function(bodyLength, bodyHeight  ) {5    var centerX = random (0,400);6var centerY = random (0,400);7var bodyColor = color(random (0,365), random (0,365), random (0,365));8    noStroke();9fill(bodyColor);10// body11ellipse(centerX, centerY, bodyLength, bodyHeight);12// tail13var tailWidth = bodyLength/4;14var tailHeight = bodyHeight/2;15triangle(centerX-bodyLength/2, centerY,16         centerX-bodyLength/2-tailWidth, centerY-tailHeight,17         centerX-bodyLength/2-tailWidth, centerY+tailHeight);18// eye19fill(33, 33, 33);20ellipse(centerX+bodyLength/4, centerY, bodyHeight/5, bodyHeight/5);21         22};23drawFish(bodyLength, bodyHeight );24drawFish(bodyLength, bodyHeight );25drawFish(bodyLength, bodyHeight );26drawFish(bodyLength, bodyHeight );27drawFish(bodyLength, bodyHeight );28drawFish(bodyLength, bodyHeight );29drawFish(bodyLength, bodyHeight );30drawFish(bodyLength, bodyHeight );31drawFish(bodyLength, bodyHeight );32drawFish(bodyLength, bodyHeight );...Using AI Code Generation
1var request = require('request');2var options = {3    json: {4        "stubs": [{5            "responses": [{6                "is": {7                }8            }]9        }]10    }11};12request(options, function(error, response, body) {13    console.log(body);14});15var request = require('request');16var options = {17    json: {18        "stubs": [{19            "responses": [{20                "is": {21                }22            }]23        }]24    }25};26request(options, function(error, response, body) {27    console.log(body);28});29var request = require('request');30var options = {31    json: {32        "stubs": [{33            "responses": [{34                "is": {35                }36            }]37        }]38    }39};40request(options, function(error, response, body) {41    console.log(body);42});43var request = require('request');44var options = {45    json: {46        "stubs": [{47            "responses": [{48                "is": {49                }50            }]51        }]52    }53};54request(options, function(error, response, body) {55    console.log(body);56});57var request = require('request');58var options = {59    json: {Using AI Code Generation
1var mb = require('mountebank');2var imposter = {3        {4                {5                    is: {6                    }7                }8        }9};10mb.create(imposter).then(function (imposter) {11    console.log('Imposter created on port ' + imposter.port);12    return mb.get('/imposters/' + imposter.port);13}).then(function (response) {14    console.log('Imposter response: ' + response.bodyLength);15});Using AI Code Generation
1var request = require('request');2var options = {3    json: {4            {5                    {6                        is: {7                        }8                    }9            }10    }11};12request(options, function (error, response, body) {13    if (!error && response.statusCode == 201) {14        console.log(body.stubs[0].responses[0].is.body);15        console.log(body.stubs[0].responses[0].is.bodyLength);16    }17});18var request = require('request');19var options = {20    json: {21            {22                    {23                        is: {24                        }25                    }26            }27    }28};29request(options, function (error, response, body) {30    if (!error && response.statusCode == 201) {31        console.log(body.stubs[0].responses[0].is.body);32        console.log(body.stubs[0].responses[0].is.bodyLength);33    }34});Using AI Code Generation
1const mb = require('mountebank');2const imposters = [{3    "stubs": [{4        "responses": [{5            "is": {6            }7        }]8    }]9}];10mb.create({ imposters }, err => {11    if (err) {12        console.error(err);13        process.exit(1);14    }15    console.log('Imposter created!');16    mb.get('/imposters/3000', (err, response) => {17        if (err) {18            console.error(err);19            process.exit(1);20        }21        console.log('Imposter retrieved!');22        console.log(response.bodyLength);23        mb.stop(() => console.log('Shutdown complete'));24    });25});26{27  "scripts": {28  },29  "dependencies": {30  }31}32const mb = require('mountebank');33const imposters = [{34    "stubs": [{35        "responses": [{36            "is": {37            }38        }]39    }]40}];41mb.create({ imposters }, err => {42    if (err) {43        console.error(err);Using AI Code Generation
1var mb = require('mountebank');2var request = require('request');3var assert = require('assert');4var port = 2525;5var imposterPort = 3000;6describe('test', function () {7    this.timeout(3000);8    before(function (done) {9        mb.start({10        }, done);11    });12    after(function (done) {13        mb.stop(port, done);14    });15    it('should return 200', function (done) {16        var imposter = {17            stubs: [{18                responses: [{19                    is: {20                    }21                }]22            }]23        };24        mb.createImposter(port, imposter, function () {25            request.get(imposterUrl, function (error, response, body) {26                assert.equal(response.statusCode, 200);27                mb.get('/imposters/' + imposterPort, port, function (error, response, body) {28                    assert.equal(body.requests[0].bodyLength, 10);29                    done();30                });31            });32        });33    });34});Using AI Code Generation
1var mb = require('mountebank');2var assert = require('assert');3var port = 2525;4var host = 'localhost';5var path = '/test';6var stub = {7    responses: [{8        is: {9        }10    }]11};12var predicateGenerators = [{13    matches: {14    }15}];16var imposter = {17    _links: {18        self: {19        }20    },21};22mb.create(imposter).then(function (imposter) {23    mb.post('/imposters/' + port + '/requests', {24    }).then(function (response) {25        assert.equal(response.statusCode, 200);26        assert.equal(response.body, 'hello world');27    }).finally(function () {28        mb.del('/imposters/' + port);29    });30});Using AI Code Generation
1var mb = require('mountebank');2var fs = require('fs');3var imposter = fs.readFileSync('imposter.json', 'utf8');4var options = {protocol: 'http', port: 3000, allowInjection: true};5mb.create(options, function (error, mb) {6    mb.post('/imposters', imposter, function (error, response) {7        console.log('Imposter created');8        mb.get('/imposters/1', function (error, response) {9            console.log(response.body);10        });11    });12});13{14    {15        {16          "is": {17            "headers": {18            },19          }20        }21    }22}Using AI Code Generation
1const mbHelper = require('../mbHelper')2const mb = mbHelper.createMbHelper()3const fs = require('fs')4mb.post('/test', function (req, res, next) {5    console.log(req.bodyLength)6    res.send(200, 'success')7    next()8})9mb.start(2525, function () {10    console.log('mountebank started')11})12const mb = require('mountebank')13const fs = require('fs')14function createMbHelper() {15    return {16        post: function (path, callback) {17            mb.post(path, callback)18        },19        start: function (port, callback) {20            mb.create({21            }, callback)22        }23    }24}25module.exports = {26}27{28}29protoc --include_imports --include_source_info -o ./protofile.proto ./protofile.proto30syntax = "proto3";31package com.example;32message ExampleMessage {33    string name = 1;34    string email = 2;35}36service ExampleService {37    rpc ExampleMethod(ExampleMessage) returns (ExampleUsing AI Code Generation
1const express = require('express');2const app = express();3const port = 3000;4const mb = require('mountebank');5const imposter = {6    {7        {8          is: {9          }10        }11    }12};13mb.create(imposter).then(() => {14  console.log('Imposter created');15});16app.get('/', (req, res) => {17  res.send('Hello World!');18});19app.listen(port, () => {20});21const express = require('express');22const app = express();23const port = 3000;24const mb = require('mountebank');25const imposter = {26    {27        {28          is: {29            headers: {30              'Content-Length': '<%= bodyLength(body) %>'31            }32          }33        }34    }35};36mb.create(imposter).then(() => {37  console.log('Imposter created');38});39app.get('/', (req, res) => {40  res.send('Hello World!');41});42app.listen(port, () => {43});Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
