Infection

by Chris Nicholson

HTML

<div id="board">

    <div id="one"></div>
    
    <div id="two" class="neutral"></div>
    
    <div id="three"></div>

</div>

CSS

div {
    position: absolute;
    
    height: 50px;
    width: 50px;
    border-radius: 999px;
}

.selected {
    border: 2px solid lime;
}

.neutral {
    background: #dddddd;
}

#one {
    top: 100px;
    left: 100px;
    height: 200px;
    width: 200px;
    background: red;
}

#two {
    top: 300px;
    left: 300px;
}

#three {
    top: 200px;
    left: 500px;
    background: yellow;
}

JavaScript

$(document).ready(function(){

    var player1 = new player('1', 'Chris', 'red');
    var player1Bases = new Array();
    player1Bases.push(new baseObj('1', 'one'));
    player1Bases.push(new baseObj('1', 'three'));
    
    for (i = 0; i < player1Bases.length; i++) {
        $('#' + player1Bases[i].baseId).css('background', player1.color);
    }
    
    setInterval(function(){ grow() }, 1000);
    
    
    $('#board div').click(function(){
    
        var anySelected = 0;
        $('#board div').each(function(){
            if ($(this).hasClass('selected')) {
               anySelected++;
            } 
        });

        
        if (anySelected === 0) {
            // None selected, add .selected to clicked div (if yours)
            var from = $(this);

            // if the base is theres, add class
            for (i = 0; i < player1Bases.length; i++) {
                if (from.attr('id') === player1Bases[i].baseId) {
                    from.addClass('selected');    
                }    
            }            
        } else {
            
            // if the same as the one selected
            var sameOneCheck = $(this);
            if (sameOneCheck.hasClass('selected')) {
                sameOneCheck.removeClass('selected');
            } else {
                
                // Set from and to points
                var from = $('#board div.selected');
                var to = $(this);
                
                // can remove the selected class allowing next move
                from.removeClass('selected');
        
                // Make from smaller
                from.css({
                    height: (from.height() / 2),
                    width: (from.width() / 2)
                });
                
                // Add the travelling obj to the board
                var randomId = 'travel' + Math.floor(Math.random()*99999);
                $('#board').append('<div id="' + randomId + '" class="travelling">');
            ...