-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcharacter.js
71 lines (58 loc) · 1.48 KB
/
character.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
var cards = require( './cards' )
var _ = require('underscore')
var PLAYER_ID_BOT = null
var nextId = 0
var getNextId = function() {
return nextId++
}
var Character = function( name, position, team, cardWeights ) {
this.id = getNextId()
this.name = name
this.cardIds = [0, 1, 2, 3, 4, 5, 6, 7, 8]
this.position = position
this.team = team
this.cardWeights = cardWeights
this.playerId = PLAYER_ID_BOT
this.statistics = {health:100}
this.selectCard = function( cardId ) {
this.selectedCard = cardId
}
this.autoSelectCard = function() {
var randomNumber = Math.random()
var totalProbability = 0
if (this.playerId != PLAYER_ID_BOT) {
this.selectedCard = 8 // Pass
} else {
var option = _.find(cardWeights, function(option) {
totalProbability += option.weight
return totalProbability >= randomNumber
})
this.selectedCard = option ? option.cardId : null
}
}
this.getSelectedCard = function() {
if(!this.selectedCard) {
this.autoSelectCard()
}
return this.selectedCard
}
this.resetCardSelection = function() {
this.selectedCard = null
}
this.takeBotControl = function(playerId) {
this.resetCardSelection()
if(this.playerId == PLAYER_ID_BOT) {
this.playerId = playerId
}
}
this.releaseBotControl = function(playerId) {
this.resetCardSelection()
if(this.playerId == playerId) {
this.playerId = PLAYER_ID_BOT
}
}
this.botEnabled = function() {
return this.playerId == PLAYER_ID_BOT
}
}
module.exports.Character = Character