use arrow keys/return

Troubleshooting basic room item selection and moving.

Bugz:

  • moving the selection off the last column of a row selects… nothing? and then the next row on another keypress
  • overflow function crashy
  • drop function borks holding after the pickup function
  • can’t move up to the beginning of the rop row or down to the end of the last row
  • generally crappy
var sketchHeight = 710;
var sketchWidth = 1200;

var defaultItemHeight = 120;
var defaultItemWidth = 80;
var maxRoomItems = 56;
var maxRoomRows =4;
let maxRoomColumns = maxRoomItems / maxRoomRows;
var maxInventoryItems = 14;
var terminalBarHeight = 40;
var terminalBarYLocation = sketchHeight-150;

let isLit;
let roomColor;
let roomItems = [];
let inventory =[];
let characters = [];
let inscription;
let selectedItemNumber = 0;

let lifting = false;
let holding= false;
let heldItem;
let liftDuration = 1000;

function preload() {
    candleLit_sprite_sheet = loadSpriteSheet('assets/animation/candleLit.png', 200, 267, 13);
    candleUnlit_sprite_frame = loadAnimation('assets/animation/candleUnlit.png');
    incenseLit_sprite_sheet = loadSpriteSheet('assets/animation/incenseLit.png', 200, 387, 11);
    incenseUnlit_sprite_frame = loadAnimation('assets/animation/incenseUnlit.png');
    EBGaramond = loadFont('assets/fonts/EBGaramond12-AllSC.otf');
    courierCode = loadFont('assets/fonts/courier_prime_code-webfont.woff');
}

function setup() {
    textFont(EBGaramond);
    let canvas = createCanvas(sketchWidth, sketchHeight);
    noSmooth();
    canvas.parent('sketch');
    roomColor = color('#ddaabb');
    noStroke();
    terminalColor = color(red(roomColor) /3, green(roomColor)/3, blue(roomColor)/3);
    dashboardColor = color(red(roomColor) /2, green(roomColor)/2, blue(roomColor)/2);
    thermometerFillColor = color(red(roomColor) /2, green(roomColor)/2, blue(roomColor)/3);

    for (i = 0; i < 40; i++){
        let candle = createSprite(-100,-100); // this is bad, it's drawing an extra sprite when the sprite is created, so I hide it offscreen
        let candleLitAnim = loadAnimation(candleLit_sprite_sheet);
        candle.addAnimation('normal', candleLitAnim);
        let candleItem = new Item('candle', candle);
        roomItems.push(candleItem);

        let incense = createSprite(-100,-100);
        let incenseLitAnim = loadAnimation(incenseLit_sprite_sheet);
        incense.addAnimation('normal', incenseLitAnim);
        let incenseItem = new Item('incense', incense);
        roomItems.push(incenseItem);
    }


    inscription = "Robert Taylor Biggs, 1921 - 1986";
    console.log(`${roomColor},${inscription},${roomItems[0].itemName} `)
}

class Item {
    constructor(name, sprite, active) {
        this.name = name;
        this.sprite = sprite;
        this.active = active;
    }

}

function drawDashboard() {
    rectMode(CORNER);
    fill(dashboardColor);
    rect(0, height-150, width, 150);

    fill(terminalColor);
    rect(0, terminalBarYLocation, width, terminalBarHeight);
    fill(255);

}

function drawMessage(item) {
    fill(255);
    textSize(width/50);
    textFont(courierCode);
    if (!holding) {
        text("hold button to get " + item.name, width/2,terminalBarYLocation + terminalBarHeight/1.5 );

    } else {
        text("press button to " + item.actionName +" "+ item.name + " | hold button to place " + item.name, width/2,terminalBarYLocation + terminalBarHeight/1.5 );
    }

}

function liftItem(item) {

    if (lifting) {
        fill(thermometerFillColor);
        let diff = millis() - startTime;
        let x = map(diff, 0, liftDuration, 0, width);
        x = constrain(x, 0, width);
        normalizedValue = x/liftDuration;
        easedWidth = cubicIn(normalizedValue) * width;
        rect(0, terminalBarYLocation, easedWidth, terminalBarHeight);
        if (x >= width) {
            lifting = false;
            holding = true;
            heldItem = item;
        }
        drawMessage(item);
        }
}

function dropItem(itemName) {

    if (lifting) {
        fill(thermometerFillColor);
        let diff = millis() - startTime;
        let x = map(diff, 0, liftDuration, 0, width);
        x = constrain(x, 0, width);
        normalizedValue = x/liftDuration;
        easedWidth = cubicIn(normalizedValue) * width;
        rect(0, terminalBarYLocation, easedWidth, terminalBarHeight);
        if (x >= width) {
            lifting = false;
            holding= false;
            heldItem = null;
        }
        drawMessage("hold button to place", itemName);
        
    } else {
        console.log("tried dropping but wasn't lifting");
    }
}

function itemOverflow(){
    if (roomItems.length > maxRoomItems){
        for (i = maxRoomItems; i <= roomItems.length; i++){
            inventory.push(inventory[inventory.length]);
            roomItems.splice[i,1];
        }
    }

    if (inventory.length > maxInventoryItems){
        for (i = maxInventoryItems; i <= inventory.length; i++){
            roomItems.push(roomItems[roomItems.length]);
            inventory.splice[i,1];
        }
    }
}

function drawSpritesfromArrays(array) {
    let duration = 1;
    let itemRowCounter = 1;
    let itemColumnCounter = 1;

    for (i = 0; i < array.length; i++) {
        let thisItem = array[i];
        let thisSprite = thisItem.sprite;
        
        if (thisItem != heldItem){
            thisSprite.scale = .5;
        } else {
            thisSprite.scale = .8;
            drawMessage(thisItem);
        }
        if (itemColumnCounter > maxRoomColumns){
            itemRowCounter +=1 ;
            itemColumnCounter = 1;
        } else {
            if (selectedItemNumber === i && !holding) {
                TweenLite.to(thisSprite, duration, {scale: .6});
                drawMessage(thisItem);
                if (keyIsDown(RETURN)) {
                    liftItem(thisItem);
                    TweenLite.to(thisSprite, 1.5, {scale: .8});
                }
                    
            }  

/*else if (selectedItemNumber === i && holding) {
                drawMessage(thisItem);
                if (keyIsDown(RETURN)) {
                    dropItem(heldItem);
                    TweenLite.to(thisSprite, 1.5, {scale: .4});
                }
}*/

            else if (selectedItemNumber != i){
                TweenLite.to(thisSprite, duration, {scale: .4});
            }   else {
                thisSprite.scale = .8;
            }
            thisSprite.position.x = itemColumnCounter * defaultItemWidth;
            thisSprite.position.y = itemRowCounter * defaultItemHeight;
            itemColumnCounter +=1;
        }

    }
}

function draw() {
    //textOutput();
    textAlign(CENTER);
    background(roomColor);
    textSize(width/25);
    fill(0);
    text(inscription, width/2, 50);
    drawSprites();
    drawDashboard();
    drawSpritesfromArrays(roomItems);
    //drawSpritesfromArrays(inventory);
    //itemOverflow(); //not working
}

function itemMove(mod) {
    nextItemNumber = mod;
    let previousItemNumber = selectedItemNumber;
    let nextItem = roomItems[nextItemNumber];
    selectedItemNumber = mod;
    if (holding) {
        roomItems[selectedItemNumber] = heldItem;
        roomItems[previousItemNumber] = nextItem;
    }
}

function changeItemArray() {
    // switch item array from roomItems to inventory or back
}

function keyPressed() {

    if (keyCode === LEFT_ARROW) {
        mod = selectedItemNumber - 1
        if (mod >= 0){
            itemMove(mod);
        } else {
            selectedItemNumber = 0;
        }
    } else if (keyCode === RIGHT_ARROW) {
        mod = selectedItemNumber + 1
        if (mod <= roomItems.length){
            itemMove(mod);
        } else {
            selectedItemNumber = roomItems.length;
        }
    } else if (keyCode === DOWN_ARROW) {
        mod = selectedItemNumber + 15
        if (mod <= maxRoomItems){
            itemMove(mod);
        } else {
            changeItemArray();
        }
    } else if (keyCode === UP_ARROW) {
        mod = selectedItemNumber - 15;
        if (mod > 0){
            itemMove(mod);
        }
    } else if (keyCode === RETURN) {
        if (!lifting) {
            console.log("button pressed");
            startTime = millis();
            lifting = true;
        }
    }
    console.log("current selection: " + selectedItemNumber);
}

    function keyReleased() {
    if (keyCode === RETURN) {
        lifting = false;
    }
}