Adventure

Developer
Size
16,660 Kb
Views
30,360

How do I make an adventure?

This is totally inspired by this videos (of the great Ron Gilbert :D):https://vimeo.com/117019614 [Thimbleweed Park Scripting Test]. What is a adventure? How do you make a adventure? This script and codes were developed by Aitor on 16 September 2022, Friday.

Adventure Previews

Adventure - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Adventure</title> <link href='https://fonts.googleapis.com/css?family=Press+Start+2P' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body>
<canvas width="640" height="480"></canvas> <script src="js/index.js"></script>
</body>
</html>

Adventure - Script Codes CSS Codes

html,
body { background: #000; color: #fff; padding: 0; margin: 0; overflow: hidden;
}
canvas { position: absolute; top: 50%; left: 50%; margin-left: -320px; margin-top: -240px; border: 1px solid #fff;
}

Adventure - Script Codes JS Codes

"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/** * Historia * * Nave espacial abandonada * */
var canvas = document.querySelector("canvas"), context = canvas.getContext("2d");
var DEBUG = false;
var DEFAULT_ACTOR_TEXT_COLOR = "#0cf";
var DEFAULT_FONT = "normal normal 12px 'Press Start 2P'";
// TODO: Esto todavía no se usa
// @see http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
function lineIntersection(x0, y0, x1, y1, x2, y2, x3, y3) { var s1_x = x1 - x0; var s1_y = y1 - y0; var s2_x = x3 - x2; var s2_y = y3 - y2; var s = (-s1_y * (x0 - x2) + s1_x * (y0 - y2)) / (-s2_x * s1_y + s1_x * s2_y); var t = (s2_x * (y0 - y2) - s2_y * (x0 - x2)) / (-s2_x * s1_y + s1_x * s2_y); if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { return { intersects: true, point: vec(x0 + t * s1_x, y0 + t * s1_y) }; } return { intersects: false, point: null };
}
function sideOf(a, b, p) { return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}
function vec() { var x = arguments.length <= 0 || arguments[0] === undefined ? 0.0 : arguments[0]; var y = arguments.length <= 1 || arguments[1] === undefined ? 0.0 : arguments[1]; return new Vector(x, y);
}
function point(id) { var x = arguments.length <= 1 || arguments[1] === undefined ? 0.0 : arguments[1]; var y = arguments.length <= 2 || arguments[2] === undefined ? 0.0 : arguments[2]; return new Point(id, x, y);
}
function walkableArea(id) { var points = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; var connections = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; var isEnabled = arguments.length <= 3 || arguments[3] === undefined ? true : arguments[3]; return new WalkableArea(id, points, connections, isEnabled);
}
function area(id) { var points = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; var isEnabled = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; return new Area(id, points, isEnabled);
}
var Vector = function () { function Vector() { var x = arguments.length <= 0 || arguments[0] === undefined ? 0.0 : arguments[0]; var y = arguments.length <= 1 || arguments[1] === undefined ? 0.0 : arguments[1]; _classCallCheck(this, Vector); this.x = x; this.y = y; } Vector.prototype.operation = function operation(name, args) { var ops = this[name + "s"], opv = this[name + "v"], opn = this[name + "n"]; if (args.length === 1 && typeof args[0] === "number") { return ops(args[0]); } else if (args.length === 1 && Vector.isLikeVector(args[0])) { return opv(args[0]); } else if (args.length === 2 && typeof args[0] === "number" && typeof args[1] === "number") { return opn(args[0], args[1]); } else { throw new Error(); } }; Vector.prototype.set = function set(x, y) { this.x = x;this.y = y;return this; }; Vector.prototype.add = function add() { return this.operation("add", arguments); }; Vector.prototype.addn = function addn(x, y) { return this.set(this.x + x, this.y + y); }; Vector.prototype.addv = function addv(v) { return this.addn(v.x, v.y); }; Vector.prototype.adds = function adds(s) { return this.addn(s, s); }; Vector.prototype.sub = function sub() { return this.operation("sub", arguments); }; Vector.prototype.subn = function subn(x, y) { return this.set(this.x - x, this.y - y); }; Vector.prototype.subv = function subv(v) { return this.subn(v.x, v.y); }; Vector.prototype.subs = function subs(s) { return this.subn(s, s); }; Vector.prototype.mul = function mul() { return this.operation("mul", arguments); }; Vector.prototype.muln = function muln(x, y) { return this.set(this.x * x, this.y * y); }; Vector.prototype.mulv = function mulv(v) { return this.muln(v.x, v.y); }; Vector.prototype.muls = function muls(s) { return this.muln(s, s); }; Vector.prototype.div = function div() { return this.operation("div", arguments); }; Vector.prototype.divn = function divn(x, y) { return this.set(this.x / x, this.y / y); }; Vector.prototype.divv = function divv(v) { return this.divn(v.x, v.y); }; Vector.prototype.divs = function divs(s) { return this.divn(s, s); }; Vector.prototype.polar = function polar(direction) { var length = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1]; return this.set(Math.cos(direction) * length, Math.sin(direction) * length); }; Vector.prototype.move = function move(direction) { var length = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1]; return this.addn(Math.cos(direction) * length, Math.sin(direction) * length); }; Vector.prototype.clone = function clone() { return new Vector(this.x, this.y); }; _createClass(Vector, [{ key: "direction", get: function get() { return Math.atan2(this.y, this.x); } }, { key: "lengthSquared", get: function get() { return this.x * this.x + this.y * this.y; } }, { key: "length", get: function get() { return Math.sqrt(this.lengthSquared); } }]); return Vector;
}();
var Point = function (_Vector) { _inherits(Point, _Vector); function Point(id) { var x = arguments.length <= 1 || arguments[1] === undefined ? 0.0 : arguments[1]; var y = arguments.length <= 2 || arguments[2] === undefined ? 0.0 : arguments[2]; _classCallCheck(this, Point); var _this = _possibleConstructorReturn(this, _Vector.call(this, x, y)); _this.id = id; return _this; } return Point;
}(Vector);
var Area = function () { function Area(id) { var points = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; var isEnabled = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; _classCallCheck(this, Area); this.id = id; this.points = points; this.isEnabled = isEnabled; } Area.prototype.addPoint = function addPoint(point) { this.points.push(point); return this; }; Area.prototype.getPointIndex = function getPointIndex(point) { return this.points.indexOf(point); }; Area.prototype.removePoint = function removePoint(point) { var index = this.getPointIndex(point); if (index >= 0) { var removed = this.points.splice(index, 1); } return this; }; Area.prototype.getNearest = function getNearest(to) { var distance = Number.MAX_VALUE, nearestPoint = null; for (var index = 0; index < this.points.length; index++) { var _point = this.points[index]; var pointDistance = _point.clone().subv(to).length; distance = Math.min(pointDistance, distance); if (pointDistance === distance) { nearestPoint = _point; } } return nearestPoint; }; Area.prototype.isIn = function isIn(point) { for (var index = 0; index < this.points.length; index++) { var start = this.points[index % this.points.length], end = this.points[(index + 1) % this.points.length]; if (sideOf(start, end, point) < 0) { return false; } } return true; }; Area.prototype.render = function render(now) { if (DEBUG === true) { var center = this.center; for (var index = 0; index < this.points.length; index++) { var start = this.points[index]; var end = this.points[(index + 1) % this.points.length]; context.beginPath(); context.moveTo(start.x, start.y); context.lineTo(end.x, end.y); context.strokeStyle = this.isEnabled ? "#0f0" : "#777"; context.stroke(); context.beginPath(); context.moveTo(start.x, start.y); context.lineTo(center.x, center.y); context.strokeStyle = this.isEnabled ? "#070" : "#333"; context.stroke(); context.fillStyle = this.isEnabled ? "#ff0" : "#777"; context.fillRect(start.x - 4, start.y - 4, 8, 8); } context.fillStyle = this.isEnabled ? "#770" : "#333"; context.fillRect(center.x - 4, center.y - 4, 8, 8); } }; _createClass(Area, [{ key: "center", get: function get() { var min = new Vector(Number.MAX_VALUE, Number.MAX_VALUE), max = new Vector(Number.MIN_VALUE, Number.MIN_VALUE); for (var index = 0; index < this.points.length; index++) { var _point2 = this.points[index]; min.x = Math.min(_point2.x, min.x); min.y = Math.min(_point2.y, min.y); max.x = Math.max(_point2.x, max.x); max.y = Math.max(_point2.y, max.y); } return max.subv(min).muls(0.5).addv(min); } }]); return Area;
}();
var WalkableArea = function (_Area) { _inherits(WalkableArea, _Area); function WalkableArea(id) { var points = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; var connections = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; var isEnabled = arguments.length <= 3 || arguments[3] === undefined ? true : arguments[3]; _classCallCheck(this, WalkableArea); var _this2 = _possibleConstructorReturn(this, _Area.call(this, id, points, isEnabled)); _this2.connections = connections; return _this2; } return WalkableArea;
}(Area);
var Direction = { NONE: -1, RIGHT: 0, DOWN: 1, LEFT: 2, UP: 3
};
var Item = function () { function Item(id) { var points = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; var position = arguments.length <= 2 || arguments[2] === undefined ? vec() : arguments[2]; var name = arguments.length <= 3 || arguments[3] === undefined ? "" : arguments[3]; var direction = arguments.length <= 4 || arguments[4] === undefined ? Direction.NONE : arguments[4]; _classCallCheck(this, Item); if (this.constructor.name === "Item") { console.warn("Instead of instancing this class directly, you should extend it"); } this.id = id; // contadores que usamos para contar el número de veces // que un item ha sido usado, mirado o hablado // en una habitación. this.counters = { used: 0, looked: 0, talked: 0, walked: 0 }; this.hitArea = area(this.id, points); this.hitPosition = vec(position.x, position.y); this.hitDirection = direction; this.position = this.hitPosition.clone(); this.name = name; } Item.prototype.onWalk = function onWalk(room) { this.counters.walked++; return this; }; Item.prototype.onTalk = function onTalk(room) { this.counters.talked++; return this; }; Item.prototype.onUse = function onUse(room) { this.counters.used++; return this; }; Item.prototype.onLook = function onLook(room) { this.counters.looked++; return this; }; Item.prototype.update = function update(now) {}; Item.prototype.render = function render(now) { if (DEBUG === true) { var center = this.hitArea.center; for (var index = 0; index < this.hitArea.points.length; index++) { var start = this.hitArea.points[index]; var end = this.hitArea.points[(index + 1) % this.hitArea.points.length]; context.beginPath(); context.moveTo(start.x, start.y); context.lineTo(end.x, end.y); context.strokeStyle = "#f00"; context.stroke(); context.beginPath(); context.moveTo(start.x, start.y); context.lineTo(center.x, center.y); context.strokeStyle = "#700"; context.stroke(); context.fillStyle = "#ff0"; context.fillRect(start.x - 4, start.y - 4, 8, 8); } context.fillStyle = "#770"; context.fillRect(center.x - 4, center.y - 4, 8, 8); context.fillStyle = "#f00"; context.fillRect(this.hitPosition.x - 2, this.hitPosition.y - 16, 4, 32); context.fillRect(this.hitPosition.x - 16, this.hitPosition.y - 2, 32, 4); } }; return Item;
}();
var Inventory = function () { function Inventory() { _classCallCheck(this, Inventory); this.items = []; } Inventory.prototype.get = function get(item) { var index = this.items.indexOf(item); if (index >= 0) { return this.items[index]; } return null; }; Inventory.prototype.add = function add(item) { if (!this.has(item)) { this.items.push(item); } return this; }; Inventory.prototype.remove = function remove(item) { var index = this.items.indexOf(item); if (index >= 0) { this.items.splice(index, 1); } return this; }; Inventory.prototype.has = function has(item) { return this.items.indexOf(item) >= 0; }; return Inventory;
}();
var Action = { LOOK_AT: "look-at", TALK_TO: "talk-to", USE: "use"
};
var ACTOR_MAX_VELOCITY = 5;
var ActorLine = function () { function ActorLine(text, style) { _classCallCheck(this, ActorLine); this.text = text; this.style = style; this.time = 0; this.duration = text.length * 50; this.start = Date.now(); this.current = Date.now(); } ActorLine.prototype.reset = function reset() { var delay = arguments.length <= 0 || arguments[0] === undefined ? 200 : arguments[0]; this.start = Date.now() + delay; this.current = Date.now(); return this; }; ActorLine.prototype.update = function update(now) { this.current = Date.now(); if (this.current > this.start) { this.time = this.current - this.start; } }; ActorLine.prototype.render = function render(now) { if (this.current > this.start) { context.save(); context.font = DEFAULT_FONT; context.textAlign = "center"; context.textBaseline = "top"; context.fillStyle = "#000"; context.fillText(this.text, 2, 2); context.fillStyle = this.style; context.fillText(this.text, 0, 0); context.restore(); } }; _createClass(ActorLine, [{ key: "hasExpired", get: function get() { return this.time >= this.duration; } }]); return ActorLine;
}();
var Actor = function () { function Actor(id) { _classCallCheck(this, Actor); this.room = null; this.id = id; this.action = Action.LOOK_AT; this.position = new Vector(5, 5); this.movement = new Vector(); this.inventory = new Inventory(); this.entity = null; this.say = null; this.sayStyle = DEFAULT_ACTOR_TEXT_COLOR; this.sayQueue = []; this.path = []; this.pathTarget = null; this.width = 128; this.height = 256; this.spriteIndex = 0; this.spriteSubindex = 0; this.spriteIndexRelaxed = 1; this.spriteX = 0; this.spriteY = 0; this.spritesheet = new Image(); } Actor.prototype.setSpritesheet = function setSpritesheet(url) { this.spritesheet.src = url; return this; }; Actor.prototype.clearPath = function clearPath() { while (this.path.length > 0) { this.path.pop(); } this.pathTarget = null; return this; }; Actor.prototype.addPath = function addPath(point) { this.path.push(point); return this; }; Actor.prototype.useWith = function useWith(anEntity, anotherEntity) {}; Actor.prototype.use = function use(entity) { this.action = Action.USE; return this; }; Actor.prototype.lookAt = function lookAt(entity) { this.action = Action.LOOK_AT; return this; }; Actor.prototype.talkTo = function talkTo(entity) { this.action = Action.TALK_TO; return this; }; Actor.prototype.walkTo = function walkTo(point) { this.action = Action.LOOK_AT; return this; }; Actor.prototype.onLook = function onLook(room, entity) { return this; }; Actor.prototype.onTalk = function onTalk(room, entity) { return this; }; Actor.prototype.onUse = function onUse(room, entity) { return this; }; Actor.prototype.onWalk = function onWalk(room, entity) { return this; }; Actor.prototype.sayLines = function sayLines() { for (var _len = arguments.length, lines = Array(_len), _key = 0; _key < _len; _key++) { lines[_key] = arguments[_key]; } for (var index = 0; index < lines.length; index++) { this.sayLine(lines[index]); } return this; }; Actor.prototype.sayLine = function sayLine(line) { this.sayQueue.push(new ActorLine(line, this.sayStyle)); return this; }; Actor.prototype.update = function update(now) { if (this.say === null && this.sayQueue.length > 0) { this.say = this.sayQueue.shift(); this.say.reset(); } if (this.say) { this.say.update(now); if (this.say.hasExpired) { this.say = null; } } if (this.path.length > 0 && this.pathTarget === null) { this.pathTarget = this.path.shift(); } if (this.pathTarget !== null) { var diff = this.pathTarget.clone().subv(this.position); if (diff.x === 0 && diff.y === 0) { this.pathTarget = null; if (this.path.length === 0) { if (this.entity) { this.entity.onWalk(this.room, this); this.onWalk(this.room, this.entity); if (this.action === Action.TALK_TO) { this.entity.onTalk(this.room, this); this.onTalk(this.room, this.entity); } else if (this.action === Action.LOOK_AT) { this.entity.onLook(this.room, this); this.onLook(this.room, this.entity); } else if (this.action === Action.USE) { this.entity.onUse(this.room, this); this.onUse(this.room, this.entity); } } } } this.movement.polar(diff.direction, diff.length > ACTOR_MAX_VELOCITY ? ACTOR_MAX_VELOCITY : diff.length); } this.position.addv(this.movement); }; Actor.prototype.render = function render(now) { context.save(); context.translate(this.position.x, this.position.y); if (DEBUG === true) { context.fillStyle = "#f0f"; context.fillRect(-4, -4, 8, 8); } if (this.say) { context.save(); context.translate(0, -this.height); this.say.render(now); context.restore(); } if (this.movement.x !== 0) { this.spriteIndex = Math.floor((this.spriteSubindex += 0.25) % (this.spritesheet.width / this.width)); } else { this.spriteIndex = this.spriteIndexRelaxed; } this.spriteX = this.spriteIndex * this.width; if (this.movement.x < 0) { this.direction = Direction.LEFT; } else { this.direction = Direction.RIGHT; } if (this.direction === Direction.LEFT) { context.scale(-1, 1); } else { context.scale(1, 1); } context.drawImage(this.spritesheet, this.spriteX, this.spriteY, this.width, this.height, -this.width * 0.5, -this.height, this.width, this.height); context.restore(); if (this.path.length > 0 && DEBUG === true) { context.beginPath(); for (var index = 0; index < this.path.length; index++) { var to = this.path[index]; if (index === 0) { context.moveTo(this.position.x, this.position.y); context.lineTo(to.x, to.y); } else { context.lineTo(to.x, to.y); } } context.strokeStyle = "#fff"; context.stroke(); } }; return Actor;
}();
var Room = function () { function Room() { _classCallCheck(this, Room); this.actor = null; this.click = null; this.move = null; this.entity = null; this.entities = []; this.areas = []; this.points = []; this.background = new Image(); } Room.prototype.setBackground = function setBackground(url) { this.background.src = url; return this; }; Room.prototype.getActor = function getActor() { return this.actor; }; Room.prototype.getActorById = function getActorById(id) { var entity = this.getEntityById(id); if (!(entity instanceof Actor)) { return null; } return entity; }; Room.prototype.setActorById = function setActorById(id) { var entity = this.getActorById(id); if (!entity) { console.error(id + " is not a valid actor"); } this.actor = entity; return this; }; Room.prototype.setActorAt = function setActorAt(id, pointId) { var actor = this.getActorById(id); if (!actor) { console.error(id + " is not a valid actor"); } var point = this.getPointById(pointId); if (!point) { console.error(id + " is not a valid Point"); } actor.position.set(point.x, point.y); return this; }; Room.prototype.distanceBetween = function distanceBetween(a, b) { return a.center.clone().subv(b.center).length; }; Room.prototype.reconstructPath = function reconstructPath(path, current) { var reconstructedPath = [current]; while (path.length > 0) { reconstructedPath.unshift(path.pop()); } return reconstructedPath; }; Room.prototype.getPathBetweenAreas = function getPathBetweenAreas(fromArea, toArea) { var _gScore, _fScore; // este método devuelve una ruta para que el personaje // la pueda recorrer. var closed = [], open = [fromArea], path = [], gScore = (_gScore = {}, _gScore[fromArea.id] = 0, _gScore), fScore = (_fScore = {}, _fScore[fromArea.id] = this.distanceBetween(fromArea, toArea), _fScore); var currentArea = null, tentativeGScore = null; while (open.length > 0) { var lowestFScore = Number.MAX_VALUE, lowestFScoreArea = null, lowestFScoreAreaIndex = null; for (var index = 0; index < open.length; index++) { var fScoreArea = open[index]; lowestFScore = Math.min(fScore[fScoreArea.id], lowestFScore); if (lowestFScore === fScore[fScoreArea.id]) { lowestFScoreArea = fScoreArea; lowestFScoreAreaIndex = index; } } open.splice(lowestFScoreAreaIndex, 1); currentArea = lowestFScoreArea; if (currentArea === toArea) { return this.reconstructPath(path, toArea); } closed.push(currentArea); for (var index = 0; index < currentArea.connections.length; index++) { var connectedArea = this.areas[currentArea.connections[index]]; if (!connectedArea.isEnabled) { continue; } if (closed.indexOf(connectedArea) >= 0) { continue; } tentativeGScore = gScore[currentArea.id] + this.distanceBetween(currentArea, connectedArea); if (open.indexOf(connectedArea) < 0) { open.push(connectedArea); } else if (tentativeScore >= gScore[connectedArea.id]) { continue; } path.push(currentArea); gScore[connectedArea.id] = tentativeGScore; fScore[connectedArea.id] = gScore[connectedArea.id] + this.distanceBetween(connectedArea, toArea); } } return null; }; Room.prototype.getEntityAt = function getEntityAt(point) { for (var index = 0; index < this.entities.length; index++) { var entity = this.entities[index]; if (entity instanceof Item) { if (entity.hitArea.isIn(point)) { return entity; } } } return null; }; Room.prototype.getEntityById = function getEntityById(id) { for (var index = 0; index < this.entities.length; index++) { var entity = this.entities[index]; if (entity.id === id) { return entity; } } return null; }; Room.prototype.getAreaAt = function getAreaAt(index) { if (index >= 0 && index < this.areas.length) { return this.areas[index]; } return null; }; Room.prototype.getAreaAtPoint = function getAreaAtPoint(point) { for (var index = 0; index < this.areas.length; index++) { var _area = this.areas[index]; if (_area.isIn(point)) { return _area; } } return null; }; Room.prototype.addEntity = function addEntity(entity) { entity.room = this; this.entities.push(entity); return this; }; Room.prototype.getEntity = function getEntity(entity) { var index = this.entities.indexOf(entity); if (index >= 0) { return this.entities[index]; } return null; }; Room.prototype.removeEntity = function removeEntity(entity) { var index = this.entities.indexOf(entity); if (index >= 0) { var removed = this.entities.splice(index, 1); var removedEntity = removed[0]; removedEntity.room = null; } return this; }; Room.prototype.addArea = function addArea(area) { this.areas.push(area); return this; }; Room.prototype.getPointById = function getPointById(id) { for (var index = 0; index < this.points.length; index++) { var _point3 = this.points[index]; if (_point3.id === id) { return _point3; } } return null; }; Room.prototype.addPoint = function addPoint(point) { this.points.push(point); return this; }; Room.prototype.update = function update(now) { for (var index = 0; index < this.entities.length; index++) { var entity = this.entities[index]; entity.update(now); } }; Room.prototype.render = function render(now) { // dibujamos el fondo. context.drawImage(this.background, 0, 0); // necesitamos reordenar la posición de los elementos. this.entities.sort(function (a, b) { return a.position.y - b.position.y; }); for (var index = 0; index < this.entities.length; index++) { var entity = this.entities[index]; entity.render(now); } for (var index = 0; index < this.areas.length; index++) { var _area2 = this.areas[index]; _area2.render(now); } if (this.entity) { context.save(); context.translate(this.entity.hitArea.center.x, this.entity.hitArea.center.y); context.font = DEFAULT_FONT; context.textAlign = "left"; context.textBaseline = "top"; context.fillStyle = "#000"; context.fillText(this.entity.name, 2, 2); context.fillStyle = "#fff"; context.fillText(this.entity.name, 0, 0); context.restore(); } if (this.click && DEBUG === true) { context.save(); context.translate(this.click.x, this.click.y); context.fillStyle = "#0ff"; context.fillRect(-4, -4, 8, 8); context.restore(); } }; return Room;
}();
var Game = { rooms: [], room: null, getRoomById: function getRoomById(id) { return this.rooms.reduce(function (id, room) { if (room.id === id) { return room; } return null; }, id); }, addRoom: function addRoom(room) { this.rooms.push(room); return this; }, setRoom: function setRoom(room) { this.room = room; return this; }, setRoomById: function setRoomById(id) { var room = this.getRoomById(id); if (!room) { console.error("Room " + id + " doesn't exists"); } return this.setRoom(room); }, move: function move(x, y) { var room = this.room; if (!room.move) { room.move = new Vector(x, y); } else { room.move.set(x, y); } var entity = room.getEntityAt(room.move); room.entity = entity; }, click: function click(x, y) { var actor = this.room.getActor(); if (actor === null) { return; } var room = this.room; if (!room.click) { room.click = new Vector(x, y); } else { room.click.set(x, y); } var fromArea = undefined, toArea = undefined; var entity = room.getEntityAt(room.click); room.entity = entity; var destination = room.entity ? room.entity.hitPosition : room.click; actor.entity = room.entity; // TODO: Podría ser interesante guardar // la posición del jugador. fromArea = room.getAreaAtPoint(actor.position); toArea = room.getAreaAtPoint(destination); if (fromArea && fromArea.isEnabled && toArea && toArea.isEnabled) { actor.clearPath(); if (fromArea === toArea) { actor.addPath(destination); } else { // TODO: Esto funcionaría mucho mejor si realizásemos // la intersección de las líneas del área con la // línea del recorrido. var path = room.getPathBetweenAreas(fromArea, toArea); if (path) { var fromPoint = actor.position; for (var index = 0; index < path.length; index++) { var _area3 = path[index]; var toPoint = _area3.getNearest(fromPoint, destination); actor.addPath(toPoint); fromPoint = toPoint; } actor.addPath(destination); } } } }, update: function update(now) { this.room.update(now); return this; }, render: function render(now) { this.room.render(now); return this; }
};
//const actor = aRoom.getEntityById("prota");
//actor.position = aRoom.areas[0].center.clone();
function update(now) { Game.update(now);
}
function render(now) { context.clearRect(0, 0, canvas.width, canvas.height); Game.render(now);
}
function frame(now) { update(now); render(now); window.requestAnimationFrame(frame);
}
function move(e) { var rect = canvas.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; Game.move(x, y);
}
function click(e) { var rect = canvas.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; Game.click(x, y);
}
window.requestAnimationFrame(frame);
canvas.addEventListener("click", click);
canvas.addEventListener("mousemove", move);
/** * JUEGO * * 1. Definición de clases * 2. Definición de salas * 3. Setup */
var SideTable = function (_Item) { _inherits(SideTable, _Item); function SideTable() { _classCallCheck(this, SideTable); return _possibleConstructorReturn(this, _Item.apply(this, arguments)); } SideTable.prototype.onLook = function onLook(room) { var prota = room.getEntityById("prota"); prota.sayLine("It's a little side table"); return _Item.prototype.onLook.call(this, room); }; return SideTable;
}(Item);
var RedPicture = function (_Item2) { _inherits(RedPicture, _Item2); function RedPicture() { _classCallCheck(this, RedPicture); return _possibleConstructorReturn(this, _Item2.apply(this, arguments)); } RedPicture.prototype.onLook = function onLook(room) { var prota = room.getEntityById("prota"); prota.sayLine("Mmm... it's a nice red picture"); return _Item2.prototype.onLook.call(this, room); }; return RedPicture;
}(Item);
var YellowPicture = function (_Item3) { _inherits(YellowPicture, _Item3); function YellowPicture() { _classCallCheck(this, YellowPicture); return _possibleConstructorReturn(this, _Item3.apply(this, arguments)); } YellowPicture.prototype.onLook = function onLook(room) { var prota = room.getEntityById("prota"); prota.sayLine("It's an old map of the world"); return _Item3.prototype.onLook.call(this, room); }; return YellowPicture;
}(Item);
var MurderSilhouette = function (_Item4) { _inherits(MurderSilhouette, _Item4); function MurderSilhouette() { _classCallCheck(this, MurderSilhouette); return _possibleConstructorReturn(this, _Item4.apply(this, arguments)); } MurderSilhouette.prototype.onLook = function onLook(room) { var prota = room.getEntityById("prota"); if (this.counters.looked === 0) { prota.sayLine("Oh, god"); prota.sayLine("It looks like there was a murder!"); prota.sayLine("Should I do something?"); } else { prota.sayLine("It's the silhouette of a murder"); } return _Item4.prototype.onLook.call(this, room); }; return MurderSilhouette;
}(Item);
var HelpMe = function (_Item5) { _inherits(HelpMe, _Item5); function HelpMe() { _classCallCheck(this, HelpMe); return _possibleConstructorReturn(this, _Item5.apply(this, arguments)); } HelpMe.prototype.onLook = function onLook(room) { var prota = room.getEntityById("prota"); if (room.getEntityById("murderSilhouette").counters.looked === 0) { prota.sayLine("Oh, god"); prota.sayLine("It looks like there was a murder!"); prota.sayLine("Should I do something?"); } if (this.counters.looked === 0) { prota.sayLine("Looks like he wrote that with his own blood"); } else { prota.sayLine("It says \"Help Me\""); } return _Item5.prototype.onLook.call(this, room); }; return HelpMe;
}(Item);
var DoorToHallway = function (_Item6) { _inherits(DoorToHallway, _Item6); function DoorToHallway() { _classCallCheck(this, DoorToHallway); return _possibleConstructorReturn(this, _Item6.apply(this, arguments)); } DoorToHallway.prototype.onWalk = function onWalk(room) { Game.setRoom(hallway); hallway.setActorAt("prota", "startPoint"); return _Item6.prototype.onWalk.call(this, room); }; return DoorToHallway;
}(Item);
var DoorToDorm = function (_Item7) { _inherits(DoorToDorm, _Item7); function DoorToDorm() { _classCallCheck(this, DoorToDorm); return _possibleConstructorReturn(this, _Item7.apply(this, arguments)); } DoorToDorm.prototype.onWalk = function onWalk(room) { Game.setRoom(dorm); dorm.setActorAt("prota", "startPoint"); return _Item7.prototype.onWalk.call(this, room); }; return DoorToDorm;
}(Item);
var hallway = new Room("a"), dorm = new Room("b");
Game.addRoom(hallway);
Game.addRoom(dorm);
Game.setRoom(dorm);
Game.setRoom(hallway);
dorm.setBackground("http://rojo2.com/labs/aventura/habitación-640.png").addEntity(new Actor("prota").setSpritesheet("http://rojo2.com/labs/aventura/personaje-blurry.png")).addEntity(new SideTable("sideTable", [vec(200, 360), vec(240, 360), vec(240, 420), vec(200, 420)], vec(220, 420), "Side table")).addEntity(new RedPicture("redPicture", [vec(210, 160), vec(400, 160), vec(400, 280), vec(210, 280)], vec(400, 420), "Red picture")).addEntity(new YellowPicture("yellowPicture", [vec(0, 140), vec(90, 180), vec(90, 300), vec(0, 320)], vec(200, 450), "Old world map")).addEntity(new DoorToHallway("door", [vec(580, 170), vec(615, 150), vec(615, 460), vec(580, 460)], vec(600, 440), "Hallway")).addArea(walkableArea(0, [vec(200, 420), vec(615, 420), vec(615, 480), vec(160, 480)])).addPoint(point("startPoint", 600, 440)).setActorById("prota").setActorAt("prota", "startPoint");
hallway.setBackground("http://rojo2.com/labs/aventura/habitación-2-640.png").addEntity(new Actor("prota").setSpritesheet("http://rojo2.com/labs/aventura/personaje-blurry.png")).addEntity(new DoorToDorm("door", [vec(20, 160), vec(50, 160), vec(50, 460), vec(20, 460)], vec(40, 440), "Dorm")).addEntity(new MurderSilhouette("murderSilhouette", [vec(200, 350), vec(440, 350), vec(500, 400), vec(200, 400)], vec(300, 400), "Murder silhouette")).addEntity(new HelpMe("helpMe", [vec(450, 160), vec(520, 80), vec(520, 300), vec(450, 300)], vec(350, 400), "Help me")).addArea(walkableArea(0, [vec(40, 440), vec(200, 440), vec(200, 460), vec(40, 460)], [1])).addArea(walkableArea(1, [vec(200, 440), vec(240, 400), vec(530, 400), vec(580, 460), vec(200, 460)], [0])).addPoint(point("startPoint", 100, 460)).setActorById("prota").setActorAt("prota", "startPoint");
Adventure - Script Codes
Adventure - Script Codes
Home Page Home
Developer Aitor
Username AzazelN28
Uploaded September 16, 2022
Rating 4
Size 16,660 Kb
Views 30,360
Do you need developer help for Adventure?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Aitor (AzazelN28) Script Codes
Name
Circuitry
Superformula
Dancing Text
Galaxy Preview
Countdown
Physics 101
Loader
Asteroids
Romboids
AudioContext Concept
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!