-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.js
74 lines (65 loc) · 2.29 KB
/
dashboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import './node_modules/jquery/dist/jquery.js'
const $renderOnce = $("<section></section")
const $renderContinouos = $("<section></section")
$("#buttonContainer").append($renderOnce)
$("#dataContainer").append($renderContinouos)
/**
* Initialize the dashboard and render "to-be-rendered-once" data
* @constructor
* @param {Player[]} players
* @param {function} changeTurn - function to change turn
* @param {function} defend - Actionhandler defence function
*/
export default function Dashboard (players, changeTurn, defend) {
this.players = players
this.changeTurn = changeTurn
this.defend = defend
$renderOnce.append(this.displayButtons())
}
/**
* Render data that is updated throughout the game
* @param {number} turn - index of player with turn
* @param {boolean} isInBattlePhase - whether game is in battle phase
*/
Dashboard.prototype.render = function (turn, isInBattlePhase) {
this.turn = turn
if(isInBattlePhase) {
$("#defend").attr('disabled', false)
}
$renderContinouos.html("")
$renderContinouos.append(this.displayTurn())
$renderContinouos.append(this.displayPlayerData())
if(this.players.length === 1) {
$("#winnerDeclaration").text(`${this.players[0].name} Wins!`)
}
}
Dashboard.prototype.displayPlayerData = function () {
let $playerDisplay = $("<section id='playerDisplay'></section>")
this.players.forEach(player => {
let $player = $("<div class='player'></div>").css({
"flex": "1"
})
$playerDisplay.append($player)
$player.html(`
<h3>${player.name}</h3>
<p>HP: ${player.hp}/${player.maxHP}</p>
<p>Position: ${JSON.stringify(player.pos)}</p>
<p>Damage: ${player.weapon.damage}</p>
<p>Armor: ${player.armor}</p>
`)
})
return $playerDisplay
}
Dashboard.prototype.displayTurn = function () {
let $turnDisplay = $("<section id='turnDisplay'></section>")
$turnDisplay.append($(`<p>Turn: ${this.players[this.turn].name}</p>`))
return $turnDisplay
}
Dashboard.prototype.displayButtons = function() {
let $buttonContainer = $("<section id='controls'></section>")
let $changeTurnButton = $("<button id='endTurn'>End Turn</button>").on('click', this.changeTurn)
let $defendButton = $(`<button id='defend' disabled>Defend</button>`).on('click', this.defend)
$buttonContainer.append($changeTurnButton)
$buttonContainer.append($defendButton)
return $buttonContainer
}