-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rewrite code in different classes
- Loading branch information
1 parent
eb83024
commit a698179
Showing
14 changed files
with
258 additions
and
78 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | | ||
+--------------------------------------------------------------+`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default interface Position { | ||
x?: number; | ||
y?: number; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default interface Size { | ||
x?: number; | ||
y?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default interface Velocity { | ||
x?: number; | ||
y?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.