Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum ActionTypes {
- WantToPlaceRoad,
- PlaceRoad,
- WantToPlaceShip,
- PlaceShip,
- }
- enum NotificationType {
- RoadWasPlaced,
- ShipWasPlaced,
- }
- class Game {
- actionEngine: ActionEngine
- mechanics
- broadcast: Broadcast
- road: Road
- optionalMechanics: Mechanic[]
- longestRoadController: LongestRaodController
- constructor() {
- this.broadcast = new Broadcast()
- this.actionEngine = new ActionEngine(this)
- this.road = new Road(this)
- this.longestRoadController = new LongestRaodController(this)
- this.optionalMechanics = []
- }
- gotAction(value: string, type: ActionTypes) {
- this.actionEngine.request(value, type)
- }
- }
- class Broadcast {
- broadcastToSocket(data: string) {
- console.log('*Broadcasting: ' + data)
- }
- }
- // ---
- class ActionEngine {
- parentGame: Game
- constructor(parentGame: Game) {
- this.parentGame = parentGame
- }
- observers: Observer[] = []
- registerObserver(observer: Observer) {
- this.observers.push(observer)
- }
- notify(value: string, type: NotificationType) {
- for (const observer of this.observers) {
- observer.onNotify(value, type)
- }
- }
- mechanics: Mechanic[] = []
- registerMechanic(mechanic: Mechanic) {
- this.mechanics.push(mechanic)
- }
- request(value: string, type: ActionTypes) {
- for(const mechanic of this.mechanics) {
- mechanic.onRequest(value, type)
- }
- }
- }
- abstract class Mechanic {
- abstract onRequest(value: string, type: ActionTypes)
- parentGame: Game
- constructor(parentGame: Game) {
- this.parentGame = parentGame
- this.parentGame.actionEngine.registerMechanic(this)
- }
- protected actionComplete(value: string, type: NotificationType) {
- this.parentGame.actionEngine.notify(value, type)
- }
- }
- abstract class Observer {
- abstract onNotify(value: string, type: NotificationType)
- parentGame: Game
- constructor(parentGame: Game) {
- this.parentGame = parentGame
- this.parentGame.actionEngine.registerObserver(this)
- }
- }
- // ============================
- // ============================
- // ============================
- class Road extends Mechanic {
- parentGame: Game
- constructor(parentGame: Game) {
- super(parentGame)
- }
- onRequest(value: string, type: ActionTypes) {
- if(type == ActionTypes.WantToPlaceRoad) {
- console.log('player clicked want to place road button')
- // logic to check if move is ok
- // this.parentGame.changeActionState(ActionState.wantToPlaceRoad)
- }
- if(type == ActionTypes.PlaceRoad) {
- console.log('player click node and is trying to place road at edge: *pass road index data*')
- // logic to check if move is ok
- this.placeRoad()
- }
- }
- private placeRoad() {
- this.parentGame.broadcast.broadcastToSocket('update road UI')
- console.log('-Placed road! gonna notify all that are listening')
- this.actionComplete('road placed', NotificationType.RoadWasPlaced)
- }
- }
- // ============================
- // ============================
- // ============================
- class Ship extends Mechanic {
- parentGame: Game
- constructor(parentGame: Game) {
- super(parentGame)
- }
- onRequest(value: string, type: ActionTypes) {
- if(type == ActionTypes.WantToPlaceShip) {
- console.log('player clicked want to place ship button')
- // logic to check if move is ok
- // this.parentGame.changeActionState(ActionState.wantToPlaceRoad)
- }
- if(type == ActionTypes.PlaceShip) {
- console.log('player click node and is trying to place ship at edge: *pass road index data*')
- // logic to check if move is ok
- this.placeShip()
- }
- }
- private placeShip() {
- this.parentGame.broadcast.broadcastToSocket('update ship UI')
- console.log('-Placed ship! gonna notify all that are listening')
- this.actionComplete('ship placed', NotificationType.ShipWasPlaced)
- }
- }
- // ============================
- // ============================
- // ============================
- class LongestRaodController extends Observer {
- parentGame: Game
- constructor(parentGame: Game) {
- super(parentGame)
- }
- onNotify(value: string, type: NotificationType) {
- if(type == NotificationType.RoadWasPlaced) {
- this.updateLongestRoad()
- }
- if(type == NotificationType.ShipWasPlaced) {
- this.updateLongestRoad()
- }
- }
- updateLongestRoad() {
- console.log('Updating longest road')
- }
- }
- console.log('----')
- console.log('normal game with just road -> player tries to place road')
- const game = new Game()
- game.gotAction('player wants to place road', ActionTypes.WantToPlaceRoad)
- game.gotAction('player confirmed where they want to place road', ActionTypes.PlaceRoad)
- console.log('----')
- console.log('normal game -> player tries to place ship')
- game.gotAction('player wants to place ship', ActionTypes.WantToPlaceShip)
- game.gotAction('player confirmed where they want to place ship', ActionTypes.PlaceShip)
- console.log('nothing happens')
- console.log('----')
- console.log('game with ships - player tries to place ship')
- game.optionalMechanics.push(new Ship(game))
- game.gotAction('player wants to place ship', ActionTypes.WantToPlaceShip)
- game.gotAction('player wants to place ship', ActionTypes.PlaceShip)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement