dont change the code structure,order just rewrite on top.DONT REMOVE THE COMMENTS., KEEP…
- User
dont change the code structure,order just rewrite on top.DONT REMOVE THE COMMENTS., KEEP THEM. GIVCE THE WHOLE CODE IN CODE FORMAT. DONT REMOVE THE COMMENTS AND THE COMMENTS ALSO TELL YOU WHAT TO CODE. REWRITE THE CODE SO IT LOOKS DIFFERENT BUT OUTPUT IS THE SAME. DONT CHANGE THE CODE TOO MUCH JSUT HOW TI LOOKS LIKE. DONT CHANGE STRUCTURE JSUT NAMES AND WAYS THEY ARE ACHIEVED. /* This class handles the graphical user interface and is yours to complete. / / config.js contains important configuration data in an object. It’s used e.g. with config.mapWidth. / import { config } from ‘./config.js’; import { AdventureGame } from “./adventuregame.js”; import { AdventureMap } from ‘./adventuremap.js’;
export class Gui { constructor(game) { this.game = game; this.icons = this.preloadImages(); }
// Draws the map. Parameter “map” contains a two-dimensional array of MapUnit objects. Each square is 40x40 pixels. // Width and height in squares are specified in config file. drawMap(map) { let canvas = document.getElementById(config.mapCanvas); let context = canvas.getContext(“2d”);
// draw background color (lightgreen) context.fillStyle = “lightgreen”; context.fillRect(0, 0, canvas.width, canvas.height);
// go through the map (loop inside loop) and draw contents to map canvas for (let y = 0; y < map.length; y++) { const row = map[y];
for (let x = 0; x < row.length; x++) { const rowItem = row[x];
if (rowItem.terrain === “forest”) { const image = new Image; image.src = “./images/tree.png”;
context.drawImage(image, x40, y40); } else if (rowItem.terrain === “town”) { const image = new Image; image.src = “./images/town.png”;
context.drawImage(image, x40, y40); }
if (rowItem.enemy === “enemy”) { const image = new Image; image.src = “./images/enemy.png”;
context.drawImage(image, x40, y40); } } }
// items are not shown (they’re hidden)
}
// draw player character (NOTE: x and y are actual coordinates, not grid squares) drawPlayer(x, y) { let canvas = document.getElementById(config.mapCanvas); let context = canvas.getContext(‘2d’);
const image = new Image; image.src = “./images/player.png”;
context.drawImage(image, x, y); }
// draw the inventory items, given as list (max number of inventory items is 4 - return immediately if more) drawInventory(items) { let canvas = document.getElementById(config.inventoryCanvas); let context = canvas.getContext(“2d”);
context.clearRect(0, 0, canvas.width, canvas.height);
if (items.length > 4) return;
// draw item image for each item in list (image is 50x50 pixels) for (let i = 0; i < items.length; i++) { const item = items[i];
const image = new Image; image.src = ./images/${item}.png;
context.drawImage(image, i50, 0); }
}
// Draw the text given as parameter to the narrative box. Use 14px Helvetica as font. // Use wrapText method to split it to lines, then draw each line with fillText method. Method returns arrays within an array. // Loop through the items in the outer array. In each item (inner array), slots are text, x coordinate and y coordinate in that order. drawNarrative(text) { let canvas = document.getElementById(config.narrativeCanvas); let context = canvas.getContext(“2d”);
- Assistant