Skip to content

Commit

Permalink
Migrate functions to proper classes
Browse files Browse the repository at this point in the history
  • Loading branch information
slashman committed Feb 23, 2023
1 parent 9e2a65c commit e2a76e1
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 164 deletions.
6 changes: 3 additions & 3 deletions src/js/LevelGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export default {
level.map[Random.n(0,79)][Random.n(0,24)] = Tiles.WATER;
}
for (var i = 0; i < 5; i++){
var being = new Being(level.game, level, Races.RAT);
let being = new Being(level.game, level, Races.RAT);
level.addBeing(being, Random.n(0,79), Random.n(0,24));
being.intent = 'RANDOM';
being.setIntent('RANDOM');
being = new Being(level.game, level, Races.TROLL);
level.addBeing(being, Random.n(0,79), Random.n(0,24));
being.intent = 'CHASE';
being.setIntent('CHASE');
}
level.addItem(new Item(Items.IRON_SWORD), Random.n(0,79), Random.n(0,25));
level.addItem(new Item(Items.BOOK_OF_MIRDAS), Random.n(0,79), Random.n(0,25));
Expand Down
2 changes: 1 addition & 1 deletion src/js/LevelLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
if (def.being) {
var being = new Being(level.game, level, def.being);
level.addBeing(being, x, y);
being.intent = 'RANDOM';
being.setIntent('RANDOM');
}
if (def.exitTo) {
level.addExit(x, y, def.exitTo, def.tile);
Expand Down
36 changes: 20 additions & 16 deletions src/js/display/pixiDisplay/PIXITextBox.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
*
* @param {PIXI.Text} PIXIText A Pixi.js Text game object
*/
function PIXITextBox (PIXIText) {
this.PIXIText = PIXIText;
this.lastUpdateMillis = 0;
}

PIXITextBox.prototype.setText = function (str) {
this.PIXIText.text = str;
};
import { Text } from "@pixi/text";

PIXITextBox.prototype.addText = function (str) {
var currentTime = new Date().getTime();
if (currentTime - this.lastUpdateMillis > 200){
this.PIXIText.text = '';
}
this.lastUpdateMillis = currentTime;
this.PIXIText.text += str;
}
export default class PIXITextBox {
private PIXIText: Text;
private lastUpdateMillis: number;

constructor (PIXIText: Text) {
this.PIXIText = PIXIText;
this.lastUpdateMillis = 0;
}

export default PIXITextBox;
setText (str: string) {
this.PIXIText.text = str;
};
addText (str: string) {
var currentTime = new Date().getTime();
if (currentTime - this.lastUpdateMillis > 200){
this.PIXIText.text = '';
}
this.lastUpdateMillis = currentTime;
this.PIXIText.text += str;
}
}
36 changes: 20 additions & 16 deletions src/js/display/unicodeTilesDisplay/Box.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@
* @param {*} width
* @param {*} position
*/
function Box (term, height, width, position) {
this.term = term;
this.height = height;
this.width = width;
this.position = position;
this.spaces = "";
for (var i = 0; i < width; i++){
this.spaces += " ";
}
}
export default class Box {
private term: any;
private height: number;
private position: {x: number, y: number};
private spaces: string;

Box.prototype.draw = function(){
for (var i = 0; i < this.height; i++) {
this.term.putString(this.spaces, this.position.x, this.position.y + i, 255, 255, 255);
}
};
constructor (term: any, height: number, width: number, position: {x: number, y: number}) {
this.term = term;
this.height = height;
this.position = position;
this.spaces = "";
for (var i = 0; i < width; i++){
this.spaces += " ";
}
}

export default Box;
draw() {
for (var i = 0; i < this.height; i++) {
this.term.putString(this.spaces, this.position.x, this.position.y + i, 255, 255, 255);
}
}
}
143 changes: 77 additions & 66 deletions src/js/display/unicodeTilesDisplay/TextBox.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,94 @@
* @author Eben Howard
*/

function TextBox (term, height, width, position, display) {
this.display = display;
this.term = term;
this.curx = 0;
this.cury = 0;
this.height = height;
this.width = width;
export default class TextBox {
private display: any;
private term: any;
private curx: number;
private cury: number;
private height: number;
private width: number;

this.lines = new Array();
this.position = position;
for (var i = 0; i < height; i++){
this.lines[i] = "";
private lines: string[];
private position: {x: number, y: number};
private spaces: string;
private lastUpdateMillis: number;

constructor (term: any, height: number, width: number, position: {x: number, y: number}, display: any) {
this.display = display;
this.term = term;
this.curx = 0;
this.cury = 0;
this.height = height;
this.width = width;

this.lines = new Array();
this.position = position;
for (var i = 0; i < height; i++){
this.lines[i] = "";
}
this.spaces = "";
for (var i = 0; i < width; i++){
this.spaces += " ";
}
this.lastUpdateMillis = 0;
}
this.spaces = "";
for (var i = 0; i < width; i++){
this.spaces += " ";

draw () {
for (var i = 0; i < this.lines.length; i++) {
this.term.putString(this.lines[i], this.position.x, this.position.y + i, 255, 255, 255);
}
}
this.lastUpdateMillis = 0;
}

TextBox.prototype.draw = function(){
for (var i = 0; i < this.lines.length; i++) {
this.term.putString(this.lines[i], this.position.x, this.position.y + i, 255, 255, 255);
}
};
overgrown () {
return this.lines.length > this.height;
}

checkFaint () {
var currentTime = new Date().getTime();
if (this.overgrown() && currentTime - this.lastUpdateMillis > 200){
this.clear();
this.display.refresh();
}
}

TextBox.prototype.overgrown = function(){
return this.lines.length > this.height;
}
addText (text: string) {
var currentTime = new Date().getTime();
if (this.cury >= this.height - 1 && currentTime - this.lastUpdateMillis > 200){
this.clear();
this.display.refresh();
}
this.lastUpdateMillis = currentTime;

TextBox.prototype.checkFaint = function(){
var currentTime = new Date().getTime();
if (this.overgrown() && currentTime - this.lastUpdateMillis > 200){
this.clear();
this.display.refresh();
var tokens = text.split(" ");
for (var i = 0; i < tokens.length; i++) {
var distance = this.width - this.curx;
if (distance < tokens[i].length + 1) {
this.curx = 0;
this.cury++;
}
if (!this.lines[this.cury]){
this.lines[this.cury] = "";
}
this.lines[this.cury] += tokens[i] + " ";
this.curx += tokens[i].length + 1;
}
}
}

TextBox.prototype.addText = function(text){
var currentTime = new Date().getTime();
if (this.cury >= this.height - 1 && currentTime - this.lastUpdateMillis > 200){
setText (text: string) {
this.clear();
this.display.refresh();
this.addText(text);
this.draw();
this.term.render();
}
this.lastUpdateMillis = currentTime;

var tokens = text.split(" ");
for (var i = 0; i < tokens.length; i++) {
var distance = this.width - this.curx;
if (distance < tokens[i].length + 1) {
this.curx = 0;
this.cury++;
}
if (!this.lines[this.cury]){
this.lines[this.cury] = [];
clear () {
for (var i = 0; i < this.lines.length; i++) {
this.lines[i] = "";
this.term.putString(this.spaces, this.position.x, this.position.y + i, 255, 255, 255);
}
this.lines[this.cury] += tokens[i] + " ";
this.curx += tokens[i].length + 1;
}
};

TextBox.prototype.setText = function(text) {
this.clear();
this.addText(text);
this.draw();
this.term.render();
};

TextBox.prototype.clear = function() {
for (var i = 0; i < this.lines.length; i++) {
this.lines[i] = "";
this.term.putString(this.spaces, this.position.x, this.position.y + i, 255, 255, 255);
this.lines.length = this.height;
this.curx = 0;
this.cury = 0;
}
this.lines.length = this.height;
this.curx = 0;
this.cury = 0;
};

export default TextBox;
}
78 changes: 49 additions & 29 deletions src/js/model/Being.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,39 @@
*
*/


import Random from '../Random';
import Level from './Level.class';

export default class Being {
private game: any;
private level: Level;
private tile: any;
private tileName: string;
private tilesetData: any;
private xPosition: number;
private yPosition: number;
private intent: string;

get x(): number {
return this.xPosition;
}

function Being(game, level, race){
this.game = game;
this.level = level;
this.tile = race.tile;
this.tileName = race.name;
this.tilesetData = race.tilesetData;
this.x = null;
this.y = null;
this.intent = 'CHASE';
}
get y(): number {
return this.yPosition;
}

constructor (game: any, level: Level, race: any) {
this.game = game;
this.level = level;
this.tile = race.tile;
this.tileName = race.name;
this.tilesetData = race.tilesetData;
this.xPosition = 0;
this.yPosition = 0;
this.intent = 'CHASE';
}

Being.prototype = {
act: function(){
act () {
switch (this.intent){
case 'RANDOM':
this.actRandom();
Expand All @@ -28,16 +45,18 @@ Being.prototype = {
this.actChase();
break;
}
},
actRandom: function(){
}

actRandom () {
var dx = Random.n(-1, 1);
var dy = Random.n(-1, 1);
if (!this.level.canWalkTo(this.x+dx,this.y+dy)){
return;
}
this.moveTo(dx, dy);
},
actChase: function(){
}

actChase () {
var nearestEnemy = this.getNearestEnemy();
if (!nearestEnemy){
return;
Expand All @@ -48,18 +67,19 @@ Being.prototype = {
return;
}
this.moveTo(dx, dy);
},
getNearestEnemy: function(){
}

getNearestEnemy () {
return this.game.player;
},
moveTo: function(dx,dy){
this.level.beings[this.x][this.y] = false;
this.x = this.x + dx;
this.y = this.y + dy;
if (!this.level.beings[this.x])
this.level.beings[this.x] = [];
this.level.beings[this.x][this.y] = this;
}
}

export default Being;
moveTo (dx: number, dy: number) {
this.level.moveBeing(this, dx, dy)
this.xPosition = this.x + dx;
this.yPosition = this.y + dy;
}

setIntent (intent: string) {
this.intent = intent;
}
}
Loading

0 comments on commit e2a76e1

Please sign in to comment.