All files / src/editor characters-layer.js

0% Statements 0/209
0% Branches 0/1
0% Functions 0/1
0% Lines 0/209

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { Camera } from '../camera.js';
import { Sprite } from '../sprite.js';
import { Story } from '../story.js';
import { Render } from './render.js';
import { Elements } from './elements.js';
import { AreasLayer } from './areas-layer.js';

export const CharactersLayer = {
  initialize({
    charactersCanvas, characters = [], viewport, canvasWidth, canvasHeight, panel, player, sprites,
  }) {
    const characterPane = panel.querySelector('#character-pane');
    let selected;
    let stat;
    const args = {
      width: canvasWidth,
      height: canvasHeight,
      canvas: charactersCanvas,
      context: charactersCanvas.getContext('2d'),
      player,
      sprites,
    };

    function changeCharacters(change) {
      switch (change.type) {
        case 'update-character':
          updateCharacters(characters.map((t) => { // eslint-disable-line
            if (t.id !== change.id) { return t; }
            return { ...t, [change.prop]: change.value };
          }));
          break;
        case 'delete-character':
          updateCharacters(characters.filter((t) => { // eslint-disable-line
            if (t.id !== change.id) { return true; }
            return false;
          }));
          break;
        default:
          throw Error('Unknown Change Type');
      }
    }
    const pad = 5;
    function updateCharacters(newCharacters) {
      characters = newCharacters; // eslint-disable-line
      if (selected && selected) {
        selected = characters.find((t) => t.id === selected.id);
      }
      CharactersLayer.drawAll({ ...args, characters, selected, viewport, pad });
      CharactersLayer.renderCharacterPane({
        characterPane, characters, changeCharacters, sprites, player, selected,
      });
    }
    CharactersLayer.renderCharacterPane({
      characterPane, characters, changeCharacters, sprites, player, selected,
    });

    charactersCanvas.addEventListener('mousedown', (e) => {
      const x = (e.offsetX / viewport.scale + viewport.x);
      const y = (e.offsetY / viewport.scale + viewport.y);
      selected = AreasLayer.inArea({ areas: characters, x, y });
      stat = 'move';
      if (!selected) {
        const id = Story.newId(characters);
        selected = { ...CharactersLayer.newCharacter(id, player), x, y };
      }

      // Update characters
      if (selected) {
        updateCharacters(characters.filter((t) => t !== selected).concat(selected));
      }
    });
    charactersCanvas.addEventListener('mousemove', (e) => {
      if (selected && stat === 'move') {
        const updatedCharacters = characters.filter((t) => t !== selected);
        selected = {
          ...selected,
          x: selected.x + e.movementX / viewport.scale,
          y: selected.y + e.movementY / viewport.scale,
        };
        updateCharacters(updatedCharacters.concat(selected));
      }
    });

    function done() {
      // Something selected and we're doing something
      if (selected && stat != null) {
        const updatedCharacters = characters.filter((t) => t !== selected);
        updateCharacters(updatedCharacters.concat(selected));
      }
      stat = null;
    }
    charactersCanvas.addEventListener('mouseup', done);
    charactersCanvas.addEventListener('mouseout', done);

    return {
      updateCharacters,
      updateViewport(newViewport) {
        viewport = newViewport; // eslint-disable-line
        CharactersLayer.drawAll({ ...args, characters, selected, viewport, pad });
      },
      getCharacters() { return characters; },
    };
  },

  newCharacter(id, example) {
    return {
      id,
      x: 0,
      y: 0,
      spriteIndex: 12,
      width: example.width,
      height: example.height,
      speed: example.speed,
      name: CharactersLayer.newName(id),
    };
  },

  drawAll({
    context,
    characters,
    width,
    height,
    selected,
    viewport,
    sprites,
    player,
    pad,
  }) {
    Camera.drawScene({
      player,
      characters,
      context,
      width,
      height,
      sprites,
      layoutContext: null,
      oldViewport: viewport, // Hack to not draw the background
      viewport,
      drawActorToContext: Sprite.drawActorToContext,
    });
    characters.forEach((character) => {
      context.strokeStyle = ((selected && selected.id) === character.id) ? 'red' : 'black';
      context.strokeRect(
        (character.x - viewport.x - pad) * viewport.scale,
        (character.y - viewport.y - pad) * viewport.scale,
        (character.width + 2 * pad) * viewport.scale,
        (character.height + 2 * pad) * viewport.scale,
      );
    });
  },

  newName(id) {
    return `name${id}`;
  },
  createSmallerCanvas(canvas, spritePart) {
    const newCanvas = Elements.create('canvas');
    Camera.setCanvasResolution(newCanvas, spritePart.width, spritePart.height);
    const ctx = canvas.getContext('2d');
    const newCtx = newCanvas.getContext('2d');
    const imageData = ctx.getImageData(
      spritePart.x,
      spritePart.y,
      spritePart.width,
      spritePart.height,
    );
    newCtx.putImageData(imageData, 0, 0);
    return newCanvas;
  },
  createSpritePicker({ sprite, selected, changeCharacters }) {
    return Elements.wrap(
      Elements.create('div', { class: 'character-options' }),
      sprite.parts.map((t, index) => {
        const smallCanvas = CharactersLayer.createSmallerCanvas(sprite.canvas, t);
        if (selected && selected.spriteIndex === index) {
          smallCanvas.setAttribute('class', 'selected');
        }
        Render.registerEvent(smallCanvas, 'click', () => {
          if (!selected) { return; }
          changeCharacters({ type: 'update-character', id: selected.id, prop: 'spriteIndex', value: index });
        });
        return smallCanvas;
      }),
    );
  },
  renderCharacterPane({ characterPane, characters, changeCharacters, sprites, player, selected }) {
    const ul = Elements.create('ul');
    const content = [
      Elements.wrap('h2', 'Characters'),
      CharactersLayer.createSpritePicker({
        sprite: sprites.characterSprite[player.width * 2], selected, changeCharacters,
      }),
      ul,
    ];
    for (let i = 0; i < characters.length; i++) {
      const c = characters[i];

      const nameEl = Elements.create('input', { type: 'text', value: c.name });
      Render.registerEvent(nameEl, 'change', (e) => {
        changeCharacters({ type: 'update-character', id: c.id, prop: 'name', value: e.target.value });
      });
      const del = Elements.create('input', { type: 'button', value: 'x' });
      Render.registerEvent(del, 'click', () => { changeCharacters({ type: 'delete-character', id: c.id }); });

      const item = Elements.wrap('li', [Elements.wrap('div', `${c.id}`), nameEl, del]);
      ul.appendChild(item);
    }
    Render.renderToEl(characterPane, content);
  },
};