rename things
This commit is contained in:
parent
f85afb4629
commit
3b6c1886c0
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit 0ffddb21a256c268dc2be391b98eb2902b93f5b9
|
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
export default class Rogue {
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.message = ""
|
||||||
|
this.end = false
|
||||||
|
this.init_level()
|
||||||
|
this.x = 5
|
||||||
|
this.y = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
apply_commad = function(command)
|
||||||
|
{
|
||||||
|
this.message = ""
|
||||||
|
|
||||||
|
let nx = this.x
|
||||||
|
let ny = this.y
|
||||||
|
|
||||||
|
switch (command)
|
||||||
|
{
|
||||||
|
case "q":
|
||||||
|
this.end_game()
|
||||||
|
break
|
||||||
|
case "h":
|
||||||
|
nx--
|
||||||
|
break
|
||||||
|
case "j":
|
||||||
|
ny++
|
||||||
|
break
|
||||||
|
case "k":
|
||||||
|
ny--
|
||||||
|
break
|
||||||
|
case "l":
|
||||||
|
nx++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.level[nx][ny] == ".") {
|
||||||
|
this.x = nx
|
||||||
|
this.y = ny
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
end_game = function()
|
||||||
|
{
|
||||||
|
this.message = "good bye"
|
||||||
|
this.end = true
|
||||||
|
}
|
||||||
|
|
||||||
|
init_level = function()
|
||||||
|
{
|
||||||
|
const size = 20
|
||||||
|
this.level = []
|
||||||
|
for (let y = 0; y < size; y++) {
|
||||||
|
let cols = []
|
||||||
|
for (let x = 0; x < size; x++) {
|
||||||
|
let tile = '.'
|
||||||
|
if (x == 0 || y == 0 || x == size - 1 || y == size - 1) {
|
||||||
|
tile = '#'
|
||||||
|
}
|
||||||
|
cols.push(tile)
|
||||||
|
}
|
||||||
|
this.level.push(cols)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
process.stdin.setRawMode(true)
|
||||||
|
process.stdin.resume()
|
||||||
|
process.stdin.setEncoding("utf8")
|
||||||
|
|
||||||
|
import Rogue from "./Rogue.js"
|
||||||
|
let game = new Rogue()
|
||||||
|
draw_state()
|
||||||
|
|
||||||
|
process.stdin.on("data", (key) => {
|
||||||
|
|
||||||
|
const command = key
|
||||||
|
|
||||||
|
game.apply_commad(command)
|
||||||
|
draw_state()
|
||||||
|
|
||||||
|
if (game.end) {
|
||||||
|
process.exit()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
function draw_state() {
|
||||||
|
if (game.message){
|
||||||
|
console.log(game.message)
|
||||||
|
}
|
||||||
|
|
||||||
|
let screen = ''
|
||||||
|
for (let row = 0; row < game.level.length; row++) {
|
||||||
|
for (let col = 0; col < game.level.length; col++) {
|
||||||
|
if (row == game.y && col == game.x) {
|
||||||
|
screen += "@"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
screen += game.level[row][col]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
screen += "\n"
|
||||||
|
}
|
||||||
|
console.log(screen)
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue