Skip to content

Commit

Permalink
feat: rewrite code in different classes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPierne committed Oct 30, 2022
1 parent eb83024 commit a698179
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 78 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"homepage": "https://github.com/sherpa1/typescript-project-bootstrap#readme",
"dependencies": {
"@types/jest": "^29.1.1",
"chalk": "^4.1.2",
"keypress": "^0.2.1",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
Expand Down
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import World from './modules/world';
import Monster from './modules/monster';
import Player from './modules/player';

const world = new World();
const goomba = new Monster(world, {x: 13, y: 33});
const mario = new Player(world, {x: 13, y: 8});

world.generate();
world
.addEntity(mario)
.addEntity(goomba)
.generate();

console.log(world.render());
32 changes: 24 additions & 8 deletions src/modules/elementWorld.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
class ElementWorld {
import chalk from 'chalk';

static readonly FLOOR : string = '#';
static readonly BONUS : string = '?';
static readonly FLOAT_PLAT : string = '=';
static readonly MARIO : string = 'M';
static readonly MONSTER : string = '@';
export default class ElementWorld {

};
static readonly BACKGROUND : string = chalk.bgBlue(' ');
static readonly FLOOR : string = chalk.hex('#027100')('#');
static readonly BONUS : string = chalk.bgBlue('?');
static readonly FLOAT_PLAT : string = chalk.bgBlue('=');
static readonly MARIO : string = chalk.bgBlue(chalk.red('M'));
static readonly MONSTER : string = chalk.bgBlue(chalk.hex('#cb610d')('@'));
static readonly ITEM : string = chalk.bgBlue(chalk.hex('#fff705')('*'));

export default ElementWorld;
static readonly LEXIQUE : string =
` ** Mario Bros (by Théo & Dorian) **
+--------------------------------------------------------------+
| ${ElementWorld.FLOOR} : Représente le sol |
| ${chalk.red(chalk.reset(ElementWorld.MARIO))} : Représente Mario |
| ${ElementWorld.MONSTER} : Représente un Goomba |
| ${ElementWorld.FLOAT_PLAT} : Représente un bloc |
| ${ElementWorld.BONUS} : Représente un bloc magique avec un item |
| ${ElementWorld.ITEM} : Représente un item (une étoile) |
| <- : Pour que Mario marche vers la gauche |
| -> : Pour que Mario marche vers la droite |
| SPACE : Pour que Mario saute verticalement |
| CTRL+C : Pour arrêter la partie |
+--------------------------------------------------------------+`;
};
26 changes: 26 additions & 0 deletions src/modules/entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Position from './interfaces/iposition';
import Velocity from './interfaces/ivelocity';
import Size from './interfaces/isize';
import World from './world';

export default abstract class Entity {

position: Position;
velocity: Velocity;
size: Size;
world: World;
onGround: boolean;

constructor({ x = 0, y = 0 } : Position = {}, world : World){
this.position = { x, y };
this.velocity = { x: 0, y: 0 };
this.size = { x: 0, y: 0 };
this.world = world;
this.onGround = true;
}

abstract init() : void;

abstract controller() : void|any;

};
4 changes: 0 additions & 4 deletions src/modules/interfaces/icharacter.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/modules/interfaces/ielement.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/modules/interfaces/iposition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface Position {
x?: number;
y?: number;
}
4 changes: 0 additions & 4 deletions src/modules/interfaces/irender.tsx

This file was deleted.

4 changes: 4 additions & 0 deletions src/modules/interfaces/isize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface Size {
x?: number;
y?: number;
}
4 changes: 4 additions & 0 deletions src/modules/interfaces/ivelocity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface Velocity {
x?: number;
y?: number;
}
62 changes: 62 additions & 0 deletions src/modules/monster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { setInterval, clearInterval } from 'node:timers';

import World from './world';
import Entity from './entity';
import ElementWorld from './elementWorld';

import Position from './interfaces/iposition';

export default class Monster extends Entity {

_moveForward : boolean;
_interval : NodeJS.Timer;

constructor(world: World, position : Position){
super(position, world);
this._moveForward = true;
}

init() : void {
this._interval = setInterval(this.controller.bind(this), 500).unref();
}

stop() : void {
clearInterval(this._interval);
this._interval = null;
}

controller() : void {
let [x, y] = this.world.getPosOf(ElementWorld.MONSTER);

if(!x && !y) {
this.stop();
return;
}

if (this._moveForward) {
if (y <= 38) {
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x, ++y, ElementWorld.MONSTER);
console.log(this.world.render());
} else {
this._moveForward = false;
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x, --y, ElementWorld.MONSTER);
console.log(this.world.render());
}
} else {
if (y >= 26) {
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x, --y, ElementWorld.MONSTER);
console.log(this.world.render());
} else {
this._moveForward = true;
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x, ++y, ElementWorld.MONSTER);
console.log(this.world.render());
}

}
}

}
88 changes: 88 additions & 0 deletions src/modules/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import process from 'node:process';
import { setTimeout } from 'node:timers';
import keypress from "keypress";

import ElementWorld from './elementWorld';
import World from './world';
import Entity from './entity';

import Position from './interfaces/iposition';

export default class Player extends Entity {

inventory: any[];

constructor(world : World, position : Position){
super(position, world);
this.inventory = [];
this.jumped = false;
}

init() : void {
this.controller();
}

collect(item) : void {
this.inventory.push(item);
}

controller() : void {
let [x, y] = this.world.getPosOf(ElementWorld.MARIO);
keypress(process.stdin);

process.stdin.on('keypress', (ch, key) => {
if (key && (key.ctrl && key.name == 'c') || (key.name === 'q')) {
process.stdin.pause();
process.exit(0);
}

if (key && key.name === 'left') {
if (this.world.getPos(x, y - 1) && this.onGround) {
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x, --y, ElementWorld.MARIO);
console.log(this.world.render());
}
}

if (key && key.name === 'right') {
if (this.world.getPos(x, y + 1) && this.onGround) {
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x, ++y, ElementWorld.MARIO);
console.log(this.world.render());
}
}

if (key && key.name === 'space') {
this.onGround = false;
if (this.world.getPos(x - 3, y) === ElementWorld.BACKGROUND) {
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x-4, y, ElementWorld.MARIO);
this.resetJump(x, y, 4);
} else if (this.world.getPos(x - 3, y) === ElementWorld.BONUS){
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x-4, y, ElementWorld.ITEM);
this.world.setPos(x-2, y, ElementWorld.MARIO);
this.resetJump(x, y, 2);
}else{
this.world.setPos(x, y, ElementWorld.BACKGROUND);
this.world.setPos(x-2, y, ElementWorld.MARIO);
this.resetJump(x, y, 2);
}
}

});

process.stdin.setRawMode(true);
process.stdin.resume();
}

resetJump(x, y, xn) : void {
console.log(this.world.render());
setTimeout(() => {
this.world.setPos(x - xn, y, ElementWorld.BACKGROUND);
this.world.setPos(x, y, ElementWorld.MARIO);
this.onGround = true;
console.log(this.world.render());
}, 300);
}
}
Loading

0 comments on commit a698179

Please sign in to comment.