Skip to content

Commit

Permalink
Migrate JS modules to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
slashman committed Feb 23, 2023
1 parent bcc4bb5 commit 9e2a65c
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 63 deletions.
26 changes: 16 additions & 10 deletions src/js/Game.js → src/js/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
*
*/

const UnicodeTilesDisplay = require('./display/unicodeTilesDisplay/UnicodeTilesDisplay');
const PIXIDisplay = require('./display/pixiDisplay/PixiDisplay');
const TestDisplay = require('./display/TestDisplay');
import UnicodeTilesDisplay from './display/unicodeTilesDisplay/UnicodeTilesDisplay';
import PIXIDisplay from'./display/pixiDisplay/PixiDisplay';
import TestDisplay from './display/TestDisplay';

var World = require('./model/World');
var Player = require('./model/Player');
var Input = require('./Input');
import World from './model/World';
import Player from './model/Player';
import Input from './Input';

var Item = require('./model/Item.class');
var Items = require('./data/Items.data');
import Item from './model/Item.class';
import Items from './data/Items.data';

var Game = {
declare global {
interface Window {
Game: {}
}
}

const Game = {
start: async function(config){
let selectedDisplay;
switch (config.display) {
Expand Down Expand Up @@ -57,4 +63,4 @@ var Game = {

window.Game = Game;

module.exports = Game;
export default Game;
4 changes: 3 additions & 1 deletion src/js/Input.js → src/js/Input.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const ut = (window as any).ut;

/**
* Single object that receives keypresses from the player and
* executes actions based on them.
*/

module.exports = {
export default {
inputEnabled: true,
init: function(game){
this.game = game;
Expand Down
14 changes: 7 additions & 7 deletions src/js/LevelGenerator.js → src/js/LevelGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* Used by the World object whenever a level needs to be generated.
*/

var Tiles = require('./data/Tiles.data');
var Races = require('./data/Races.data');
var Items = require('./data/Items.data');
var Being = require('./model/Being.class');
var Item = require('./model/Item.class');
var Random = require('./Random');
import Tiles from './data/Tiles.data';
import Races from './data/Races.data';
import Items from './data/Items.data';
import Being from './model/Being.class';
import Item from './model/Item.class';
import Random from './Random';

module.exports = {
export default {
generateTestLevel: function(level, fromId, nextLevelId){
for (var x = 0; x < 80; x++){
level.map[x] = [];
Expand Down
8 changes: 4 additions & 4 deletions src/js/LevelLoader.js → src/js/LevelLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*
*/

const {globalDefs, levelMaps} = require('./data/Maps.data');
var Being = require('./model/Being.class');
var Item = require('./model/Item.class');
import {globalDefs, levelMaps} from './data/Maps.data';
import Being from './model/Being.class';
import Item from './model/Item.class';

module.exports = {
export default {
loadLevel: function(level, mapId, fromId){
const map = levelMaps[mapId];
level.map = [];
Expand Down
2 changes: 1 addition & 1 deletion src/js/Random.js → src/js/Random.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
n: function(a, b){
return Math.floor(Math.random() * (b - a + 1))+a;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* them.
*/

module.exports = {
export default {
WEAPON: {
name: 'Weapon',
useFunction: function(game, item){
Expand Down
6 changes: 4 additions & 2 deletions src/js/data/Items.data.js → src/js/data/Items.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var ItemType = require('./ItemTypes.data')
const ut = (window as any).ut;

module.exports = {
import ItemType from './ItemTypes.data';

export default {
IRON_SWORD: {
type: ItemType.WEAPON,
name: 'Iron Sword',
Expand Down
8 changes: 4 additions & 4 deletions src/js/data/Maps.data.js → src/js/data/Maps.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var Tiles = require('./Tiles.data');
var Races = require('./Races.data');
var Items = require('./Items.data');
import Tiles from './Tiles.data';
import Races from './Races.data';
import Items from './Items.data';

const globalDefs = [
{ char: '$', tile: Tiles.STAIRS_DOWN, start: true },
Expand Down Expand Up @@ -56,4 +56,4 @@ const levelMaps = {
}
}

module.exports = { globalDefs, levelMaps }
export { globalDefs, levelMaps }
4 changes: 3 additions & 1 deletion src/js/data/Races.data.js → src/js/data/Races.data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
const ut = (window as any).ut;

export default {
RAT: {
tile: new ut.Tile('r', 128, 128, 0),
name: 'Rat',
Expand Down
4 changes: 3 additions & 1 deletion src/js/data/Tiles.data.js → src/js/data/Tiles.data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
const ut = (window as any).ut;

export default {
GRASS: {
tile: new ut.Tile('.', 0, 128, 0),
darkTile: new ut.Tile('.', 128, 128, 128),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
init: function(game, config){
this.game = game;
this.textBox = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ PIXITextBox.prototype.addText = function (str) {
}


module.exports = PIXITextBox;
export default PIXITextBox;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*
*/

const { Application, Assets, Texture, Rectangle, Sprite, Text, Container } = require('pixi.js');
const PIXITextBox = require('./PIXITextBox.class');
const PixiUtils = require('./PixiUtils');
import { Application, Assets, Texture, Rectangle, Sprite, Text, Container } from 'pixi.js';
import PIXITextBox from './PIXITextBox.class';
import PixiUtils from './PixiUtils';

let theCanvas;

Expand All @@ -31,11 +31,11 @@ function resizeCanvas () {

window.addEventListener("resize", resizeCanvas);

module.exports = {
export default {
init: async function(game, config){
this.textureMap = {};
this.game = game;
const app = new Application({
const app = new Application<HTMLCanvasElement>({
width: config.tileSize * config.viewportCountX,
height: config.tileSize * config.viewportCountY,
})
Expand Down Expand Up @@ -91,12 +91,10 @@ module.exports = {
fill: 0xdddddd,
align: 'left',
wordWrap: true,
wordWrapWidth: config.tileSize * config.viewportCountX * 4,
position: {
x: 10,
y: 10,
}
wordWrapWidth: config.tileSize * config.viewportCountX * 4
});
text.position.x = 10;
text.position.y = 10;
text.scale.x = 0.25;
text.scale.y = 0.25;
mainGameContainer.addChild(text);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Text } = require('pixi.js');
import { Text } from 'pixi.js';

module.exports = {
createTextBox(x, y, fontSize, initialText, wordWrapWidth) {
export default {
createTextBox(x, y, fontSize, initialText, wordWrapWidth?) {
const textBox = new Text(initialText, {
fontFamily: 'Kenney Pixel',
fontSize: fontSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ Box.prototype.draw = function(){
}
};

module.exports = Box;
export default Box;
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ TextBox.prototype.clear = function() {
this.cury = 0;
};

module.exports = TextBox;
export default TextBox;
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const ut = (window as any).ut;

/**
* Implements the Display interface using unicodetiles.js to display the
* level around the player using character graphics and the UI using
* text components laid over the map.
*
*/

var TextBox = require('./TextBox.class');
var Box = require('./Box.class');
import TextBox from './TextBox.class';
import Box from './Box.class';

let theCanvas;

Expand All @@ -30,7 +32,7 @@ function resizeCanvas () {

window.addEventListener("resize", resizeCanvas);

module.exports = {
export default {
BLANK_TILE: new ut.Tile(' ', 255, 255, 255),
CURSOR_TILE: new ut.Tile('*', 255, 255, 255),
init: function(game, config){
Expand Down
5 changes: 3 additions & 2 deletions src/js/model/Being.class.js → src/js/model/Being.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*
*/

var Random = require('../Random');

import Random from '../Random';

function Being(game, level, race){
this.game = game;
Expand Down Expand Up @@ -61,4 +62,4 @@ Being.prototype = {
}
}

module.exports = Being;
export default Being;
2 changes: 1 addition & 1 deletion src/js/model/Item.class.js → src/js/model/Item.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Item.prototype = {
// JSRL Doesn't define any specific behavior for items.
}

module.exports = Item;
export default Item;
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ Level.prototype = {
},
}

module.exports = Level;
export default Level;
4 changes: 3 additions & 1 deletion src/js/model/Player.js → src/js/model/Player.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const ut = (window as any).ut;

/**
* Object that contains the state of the player, as well as functions for it
* to interact with the world.
Expand All @@ -9,7 +11,7 @@
* Contains the field of view logic using simple raycasting.
*/

module.exports = {
export default {
MAX_SIGHT_RANGE: 10,
x: 20,
y: 20,
Expand Down
11 changes: 5 additions & 6 deletions src/js/model/World.js → src/js/model/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* Contains the state of the levels generated or loaded previously.
*/

var Level = require('./Level.class');
var LevelGenerator = require('../LevelGenerator');
var LevelLoader = require('../LevelLoader');
import Level from './Level.class';
import LevelGenerator from '../LevelGenerator';
import LevelLoader from '../LevelLoader';
import Random from '../Random';

var Random = require('../Random');

module.exports = {
export default {
levels: {},
init: function(game){
this.game = game;
Expand Down

0 comments on commit 9e2a65c

Please sign in to comment.