"use strict"; // // App.js // Team 8 Hifi Hackathon 2018 // Chang Kayla Sam Lab // // Copyright 2018 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // (function() { //***************************************************** // THE APP TABLET ICON ON OFF //***************************************************** var TABLET_BUTTON_NAME = "Carotte"; var ICON_URL = Script.resolvePath("../../../system/assets/images/luci-i.svg"); var ACTIVE_ICON_URL = Script.resolvePath("../../../system/assets/images/luci-a.svg"); var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); var button = tablet.addButton({ text: TABLET_BUTTON_NAME, icon: ICON_URL, activeIcon: ACTIVE_ICON_URL }); var gameOn = false; var window; function onClicked() { if (gameOn) { killGame() } else { createGame() } } button.clicked.connect(onClicked); Script.scriptEnding.connect(function () { killGame() button.clicked.disconnect(onClicked); tablet.removeButton(button); }); //***************************************************** // THE GAME CONFIG //***************************************************** var theConfig = { grid: { size: 5.0, }, tile: { modelURL: "https://hifi-public.s3.amazonaws.com/sam/2018-oct/models/model.obj", }, terrain: { }, carrot: { dim: { x: 0.3, y: 1.0, z: 0.3 }, // dim: { x: 0.1, y: 0.1, Z: 0.1 }, modelURL: "https://hifi-public.s3.amazonaws.com/sam/2018-oct/models/carrot2.fbx", }, }; function Print(m) { if (true) { print(m) } } var theGame = {} function createGame() { Print("Carotte Game Start") gameOn = true theGame = new Game(theConfig) button.editProperties({isActive: true}); } function killGame() { gameOn = false theGame.kill() button.editProperties({isActive: false}) Print("Carotte Game End") } //***************************************************** // THE GAME //***************************************************** function Game(config) { Print("Game Created:" + JSON.stringify(this)); this.grid = new Grid(config); this.tiles = [] var index = 0; for (var j = -2; j <3; j++) { for (var i = -2; i < 3; i++) { this.tiles.push(new Tile(config, this.grid, {x: i, y: 0, z: j}, index)); index++; } } // this.tiles.push(new Tile(config, this.grid, {x: 0, y: 0, z: 0}, index)); } Game.prototype.kill = function () { for (var i = 0; i < this.tiles.length; i++) { this.tiles[i].kill(); } Print("Game Killed:" + JSON.stringify(this)); }; //***************************************************** // GRID //***************************************************** var GRID_AXIS_UNIT = 1.0; var GRID_CELL_SIZE = 5.0; var GRID_HEX_H = (GRID_CELL_SIZE / 2) * 0.86602540378; var GRID_HEX_V = GRID_CELL_SIZE / 4; var GRID_X_OFFSET = 3 * GRID_HEX_V; var GRID_Y_OFFSET = GRID_AXIS_UNIT; var GRID_Z_OFFSET = 2 * GRID_HEX_H; var GRID_ROOT_Y_OFFSET = -GRID_AXIS_UNIT; var GRID_HEX_TILE_DIM = {x: 4 * GRID_HEX_V, y: GRID_AXIS_UNIT, z: GRID_HEX_H * 2.0} function Grid(config) { this.stageOrientation = Quat.fromPitchYawRollDegrees(0.0, 0.0, 0.0); this.stageRoot = {"x":0.0,"y":0.0,"z":0.0}; this.stageAxisA = Vec3.multiply(GRID_AXIS_UNIT, Quat.getForward(this.stageOrientation)); this.stageAxisB = Vec3.multiply(GRID_AXIS_UNIT, Quat.getRight(this.stageOrientation)); this.stageAxisC = Vec3.multiply(GRID_AXIS_UNIT, Quat.getUp(this.stageOrientation)); this.initializeGrid = function() { // MyAvatar.orientation = Quat.fromPitchYawRollDegrees(0.0, 0.0, 0.0); var orientation = Quat.fromPitchYawRollDegrees(0.0, 0.0, 0.0); orientation = Quat.safeEulerAngles(orientation); orientation.x = 0; orientation = Quat.fromVec3Degrees(orientation); this.stageOrientation = orientation; this.stageAxisA = Vec3.multiply(GRID_AXIS_UNIT, Quat.getForward(this.stageOrientation)); this.stageAxisB = Vec3.multiply(GRID_AXIS_UNIT, Quat.getRight(this.stageOrientation)); this.stageAxisC = Vec3.multiply(GRID_AXIS_UNIT, Quat.getUp(this.stageOrientation)); var pos = Vec3.sum(MyAvatar.position, Vec3.multiply(-1.0, Quat.getForward(orientation))); this.stageRoot = Vec3.sum(pos, Vec3.multiply(-1.0, Quat.getUp(orientation))); Print("Game Killed:" + JSON.stringify(this)); } this.initializeGrid(); } function tileCoordToPos(grid, coord) { var isEven = coord.z == 2 * Math.floor(coord.z / 2); return Vec3.sum(grid.stageRoot, { x: coord.z * GRID_X_OFFSET, y: coord.y, z: coord.x * GRID_Z_OFFSET + (isEven ? 0.0 : GRID_HEX_H)} ); } function tileCoordRelPosToPos(grid, coord, pos) { var origin = tileCoordToPos(grid, coord) return Vec3.sum(origin, { x: pos.z * 2.0 * GRID_HEX_V, y: pos.y, z: pos.x * GRID_HEX_H} ); } function tileDim() { return { x: GRID_CELL_SIZE,y: GRID_AXIS_UNIT,z: GRID_CELL_SIZE }; // return GRID_HEX_TILE_DIM; } //***************************************************** // TERRAIN TILE //***************************************************** var shapeTypes = [ "none", "box", "sphere", "compound", "simple-hull", "simple-compound", "static-mesh" ]; var DEFAULT_LIFETIME = 60; function Tile(config, grid, coord, index, lifetime) { this.coord = coord; this.index = index; var pos = tileCoordToPos(grid, coord); print("Tile pos:" + JSON.stringify(pos)); this.entityProperties = { type: "Shape", //shape: "Cube", shape: "Hexagon", name: "Tile-" + index, color: { red: 20, green: 20, blue: 40 }, position: pos, rotation: grid.stageOrientation, dimensions: tileDim(), lifetime: (lifetime === undefined) ? DEFAULT_LIFETIME : lifetime, userData: JSON.stringify({ grabbableKey: { grabbable: false } }), shapeType:shapeTypes[1] } this.entity = Entities.addEntity( this.entityProperties ); this.entityProperties = Entities.getEntityProperties(this.entity); this.carrots = [] var numCarrots = 3; var carrotIndex = index * numCarrots; for (var i = 0; i < numCarrots; i++) { this.carrots.push(new Carrot(config, grid, this, coord, carrotIndex)); carrotIndex++; } // print("Tile Created:" + JSON.stringify(this)); } Tile.prototype.kill = function () { for (var i = 0; i < this.carrots.length; i++) { this.carrots[i].kill(); } if (this.entity) { Entities.deleteEntity(this.entity); // this.entity = null } Print("Tile Killed:" + JSON.stringify(this)); }; //***************************************************** // CARROT //***************************************************** function Carrot(config, grid, tile, coord, index, lifetime) { this.coord = coord; this.index = index; this.tilePos = {x: Math.random() - 0.5, y: 0.5, z: Math.random() - 0.5}; var pos = tileCoordRelPosToPos(grid, coord, this.tilePos); this.entityProperties = { type: "Model", name: "Carrot-" + index, position: pos, rotation: grid.stageOrientation, dimensions: config.carrot.dim, lifetime: (lifetime === undefined) ? DEFAULT_LIFETIME : lifetime, modelURL: config.carrot.modelURL, userData: JSON.stringify({ grabbableKey: { grabbable: false } }), shapeType:shapeTypes[4], /* dynamic: true, gravity:{"x":0,"y":-9.8,"z":0}, velocity:{"x":0,"y":0.02,"z":0}, angularVelocity:OBJECT_SPIN, restitution:0.70, friction:0.01, damping:0.01,*/ } this.entity = Entities.addEntity( this.entityProperties ); this.entityProperties = Entities.getEntityProperties(this.entity); Print("Carrot Created:" + JSON.stringify(this)); } Carrot.prototype.kill = function () { if (this.entity) { Entities.deleteEntity(this.entity); // this.entity = null } Print("Carrot Killed:" + JSON.stringify(this)); }; }());