Data Types: Numbers, Strings, Booleans, Arrays, Objects

Requirement

Use all 5 data types: Numbers, Strings, Booleans, Arrays, and Objects (JSON) in game logic.


1. Numbers - Position, Velocity, Scale Factors

Ghost.js - Numeric Properties

class Ghost extends Enemy {
    constructor(data, gameEnv) {
        super(data, gameEnv);
        this.followSpeedFactor = data?.followSpeedFactor ?? 0.4;  // ← NUMBER: speed multiplier
        this.followStopDistance = data?.followStopDistance ?? 8;   // ← NUMBER: collision distance
        this._hasTriggeredDeath = false;
    }

    followPlayer(player) {
        const ghostCenter = this.getCenter();
        const playerCenter = player.getCenter();

        const dx = playerCenter.x - ghostCenter.x;  // ← NUMBER: x delta
        const dy = playerCenter.y - ghostCenter.y;  // ← NUMBER: y delta
        const distance = Math.hypot(dx, dy);         // ← NUMBER: calculated distance

        const baseSpeed = player?.xVelocity || (this.gameEnv?.innerWidth || 800) / 2000;
        const speed = Math.max(0.3, baseSpeed * this.followSpeedFactor);  // ← NUMBER math
        
        this.position.x += nx * speed;  // ← NUMBER: position update
        this.position.y += ny * speed;
        this.velocity.x = 0;            // ← NUMBER: velocity reset
        this.velocity.y = 0;
    }
}

How it meets the requirement:

  • Scale Factors: followSpeedFactor = 0.4, followStopDistance = 8
  • Calculations: Distance (dx, dy), position updates
  • Physics: Velocity components (x, y)

GameLevelMaze.js - Scale Factor for Characters

const MC_SCALE_FACTOR = 20;  // ← NUMBER: character size multiplier
const sprite_data_mc = {
    id: 'Knight',
    SCALE_FACTOR: MC_SCALE_FACTOR,
    STEP_FACTOR: 1750,      // ← NUMBER: movement speed
    ANIMATION_RATE: 100,    // ← NUMBER: milliseconds per frame
    INIT_POSITION: {
        x: 202 / 1911 * width,   // ← NUMBERS: normalized position
        y: 760 / 851 * height
    },
    pixels: { height: 432, width: 234 },  // ← NUMBERS: sprite dimensions
    hitbox: { widthPercentage: 0.1, heightPercentage: 0.15 }  // ← NUMBERS: collision box
};

How it meets the requirement:

  • Scale and Timing: SCALE_FACTOR, STEP_FACTOR, ANIMATION_RATE
  • Positioning: x, y coordinates as numbers
  • Collision: hitbox percentages as decimals

2. Strings - Character Names, Sprite Paths, Dialogue

GameLevelOutside.js - String Properties

const playerSpriteOptions = {  // ← OBJECT with STRING values
    gray: path + "/images/projects/castle-game/grayKnight.png",     // ← STRING: file path
    green: path + "/images/projects/castle-game/greenKnight.png",
    dark: path + "/images/projects/castle-game/darkKnight.png"
};

const sprite_src_mc = getPlayerSpriteSrc(getStoredPlayerSkinKey());  // ← STRING: sprite path

const sprite_data_mc = {
    id: 'Knight',                    // ← STRING: character ID
    greeting: "Hi, I am a Knight.",  // ← STRING: dialogue
    src: sprite_src_mc,              // ← STRING: sprite source
    keypress: { up: 87, left: 65, down: 83, right: 68 }  // ← NUMBER keycodes
};

const sir_morty_data = {
    id: "Sir Morty",                 // ← STRING: NPC name
    greeting: "Hello! I'm Sir Morty!",  // ← STRING: greeting
    expertise: "default",             // ← STRING: skill category
    dialogues: [                      // ← ARRAY of STRINGS
        "Enter the castle if you dare!",
        "The Dark Knight awaits inside.",
        "I heard there's a treasure in the castle."
    ]
};

How it meets the requirement:

  • File Paths: Image paths concatenated with strings
  • Dialogue: NPC greetings and responses
  • IDs: Character identifiers

GameLevelMaze.js - String Configuration

const sprite_data_mc = {
    id: 'Knight',              // ← STRING: character ID
    greeting: "Hi, I am a Knight.",  // ← STRING: greeting message
    src: sprite_src_mc,        // ← STRING: sprite image path
    pixels: { height: 432, width: 234 }  // ← NUMBERS
};

const mortyData = {
    id: 'morty',               // ← STRING: NPC ID
    greeting: 'Hey there!',    // ← STRING: dialogue
    src: path + "/images/projects/castle-game/morty.png",  // ← STRING: file path
    dialogues: ["a"],          // ← ARRAY of STRINGS
    // ... methods ...
};

How it meets the requirement:

  • Object IDs: String identifiers
  • File Paths: String resource locations

3. Booleans - State Flags and Conditions

Ghost.js - Boolean Flags

class Ghost extends Enemy {
    constructor(data, gameEnv) {
        super(data, gameEnv);
        this._hasTriggeredDeath = false;  // ← BOOLEAN: collision flag
    }

    update() {
        const player = this.getPlayer();
        if (player && !player.isDead) {   // ← BOOLEAN condition: player exists and alive
            this.followPlayer(player);
        }
        super.update();
    }

    handleCollisionEvent() {
        if (this._hasTriggeredDeath || this.playerDestroyed) return;  // ← BOOLEAN OR logic

        const player = this.getPlayer();
        if (!player || player.isDead) return;  // ← BOOLEAN checks

        this._hasTriggeredDeath = true;  // ← Set BOOLEAN to true
        this.playerDestroyed = true;
        player.isDead = true;
    }
}

How it meets the requirement:

  • State Tracking: _hasTriggeredDeath, playerDestroyed, isDead
  • Conditionals: Boolean checks prevent duplicate actions

DeathBarrier.js - Boolean State Management

class DeathBarrier extends Barrier {
    constructor(data, gameEnv) {
        super(data, gameEnv);
        this._hasTriggeredDeath = false;  // ← BOOLEAN: collision flag
    }

    update() {
        if (this._hasTriggeredDeath) return;  // ← Guard clause with BOOLEAN

        if (this.collisionData?.hit) {       // ← BOOLEAN check: collision occurred
            this._hasTriggeredDeath = true;  // ← Set BOOLEAN
            player.isDead = true;             // ← Set BOOLEAN on player
        }
    }
}

How it meets the requirement:

  • Collision Flags: _hasTriggeredDeath prevents duplicate triggers
  • State Transitions: isDead flag marks game over condition

4. Arrays - Object Collections

GameLevelOutside.js - Array of Game Objects

this.classes = [
    { class: GameEnvBackground, data: image_data_floor },
    { class: Player, data: sprite_data_mc },
    { class: Npc, data: sprite_data_darkKnight },
    { class: Npc, data: sir_morty_data },
    { class: Npc, data: sprite_data_closet },
    { class: SpriteSheetCoin, data: gem_data }
];  // ← ARRAY of configuration objects

const dialogues = [                  // ← ARRAY of STRINGS
    "Enter the castle if you dare!",
    "The Dark Knight awaits inside.",
    "I heard there's a treasure in the castle."
];

How it meets the requirement:

  • Game Objects: Array of class/data pairs for instantiation
  • Dialogue Lists: Array of strings for NPC responses

GameLevelMaze.js - Array of Barriers

this.classes = [
    { class: GameEnvBackground, data: bgData },
    { class: Player, data: sprite_data_mc },
    { class: Npc, data: mortyData },
    { class: Npc, data: sprite_data_invis },
    { class: Ghost, data: ghostData },
    { class: DeathBarrier, data: dbarrier_1 },
    { class: DeathBarrier, data: dbarrier_2 },
    // ... 17 more barriers in the ARRAY ...
    { class: DeathBarrier, data: dbarrier_19 }
];  // ← ARRAY containing 19+ objects

How it meets the requirement:

  • 19 Barriers: Array of multiple configuration objects

5. Objects (JSON) - Configuration Objects

GameLevelOutside.js - Sprite Data Object

const sprite_data_mc = {
    id: 'Knight',                      // STRING
    greeting: "Hi, I am a Knight.",    // STRING
    src: sprite_src_mc,                // STRING
    SCALE_FACTOR: MC_SCALE_FACTOR,     // NUMBER
    STEP_FACTOR: 1500,                 // NUMBER
    ANIMATION_RATE: 40,                // NUMBER
    INIT_POSITION: {                   // NESTED OBJECT
        x: 0.5 * width,                // NUMBER
        y: 0.75 * height               // NUMBER
    },
    pixels: { height: 432, width: 234 },  // NESTED OBJECT with NUMBERs
    orientation: { rows: 4, columns: 3 },  // NESTED OBJECT
    down: { row: 0, start: 0, columns: 3 },   // NESTED OBJECT
    hitbox: { widthPercentage: 0.1, heightPercentage: 0.15 },  // NESTED OBJECT
    keypress: { up: 87, left: 65, down: 83, right: 68 }  // OBJECT with NUMBERs
};  // ← COMPLEX JSON OBJECT with multiple data types

How it meets the requirement:

  • Nested Objects: Hierarchical data structure (position, pixels, orientation)
  • Mixed Types: Strings, numbers, and nested objects in one configuration

GameLevelOutside.js - Knowledge Base JSON

const sir_morty_data = {
    id: "Sir Morty",
    expertise: "default",
    chatHistory: [],              // ARRAY
    knowledgeBase: {              // OBJECT with ARRAY of OBJECTs
        default: [
            {                      // OBJECT
                question: "What is inside the castle?",     // STRING
                answer: "Inside the castle lays a prisoner..."
            },
            {
                question: "Who are you?",                   // STRING
                answer: "I am Sir Morty, a brave knight..."
            },
            {
                question: "How do I win the game?",
                answer: "To win, you need to successfully navigate..."
            }
        ]
    }
};  // ← Complex JSON for AI knowledge base

How it meets the requirement:

  • Array of Objects: knowledgeBase contains array of Q&A pairs
  • Nested Structure: Multiple levels of objects and arrays

Summary

Numbers - Position (x, y), velocity, scale factors, animation rates
Strings - File paths, character IDs, dialogue, sprite sources
Booleans - State flags (_hasTriggeredDeath, isDead), conditions
Arrays - Game object collections, dialogue lists, barrier instances
Objects (JSON) - Sprite data, position, collision, knowledge base