Created
May 17, 2019 09:47
-
-
Save dacostafilipe/fe648e621e8121fedfc4228faa741e00 to your computer and use it in GitHub Desktop.
themarcba/simple-rpg
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
export default class Map { | |
/** | |
* | |
* @param fields | |
*/ | |
constructor(fields) { | |
this.fields = fields; | |
} | |
/** | |
* | |
* @param spawnPoint | |
*/ | |
setSpawnPoint(spawnPoint) { | |
this.spawnPoint = spawnPoint; | |
}; | |
/** | |
* | |
* @param field1 | |
* @param field2 | |
* @param side | |
*/ | |
connectFields = function (field1, field2, side) { | |
switch (side) { | |
case 'north': | |
field1.north = field2; | |
field2.south = field1; | |
break; | |
case 'east': | |
field1.east = field2; | |
field2.west = field1; | |
break; | |
case 'south': | |
field1.south = field2; | |
field2.north = field1; | |
break; | |
case 'west': | |
field1.west = field2; | |
field2.east = field1; | |
break; | |
default: | |
break; | |
} | |
this.fields.forEach((field, index) => { | |
field.id = index; | |
}); | |
}; | |
} |
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
import SystemLog from './SystemLog'; | |
import Map from './Map'; | |
export default class Player { | |
/** | |
* | |
* @param name | |
* @param {Map} map | |
*/ | |
constructor(name, map){ | |
this.name = name; | |
this.health = 100; | |
this.map = map; | |
this.currentField = map.spawnPoint; | |
} | |
move (){ | |
if(this.health > 0) { | |
if (this.currentField[direction] && this.currentField[direction].canMove) { | |
this.currentField = this.currentField[direction]; | |
SystemLog.write(`moved ${direction}. now on field ${this.currentField.id} (${this.currentField.name})`); | |
if(this.currentField.enterAction) { | |
this.currentField.enterAction(this); | |
} | |
} else { | |
if (this.currentField[direction] && !this.currentField[direction].canMove) { | |
SystemLog.write(`can't move here. ${this.currentField[direction].cantMoveReason}`); | |
} else { | |
SystemLog.write(`can't move here. there is nothing there.`); | |
} | |
} | |
} else { | |
SystemLog.write(`${this.name} can't move. he passed out.`); | |
} | |
} | |
} |
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
class SystemLog{ | |
constructor(selector = 'body'){ | |
this.element = document.querySelector(selector); | |
} | |
/** | |
* | |
* @param message | |
* @param messageType | |
*/ | |
write(message, messageType = 'info'){ | |
this.element.innerHTML += `${message}<br>`; | |
console[messageType](message); | |
} | |
} | |
export default new SystemLog(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment