-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.js
More file actions
166 lines (155 loc) · 4.4 KB
/
Player.js
File metadata and controls
166 lines (155 loc) · 4.4 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var Player = function (name, controller, playedPosition) {
console.log(playedPosition);
'use strict';
var my = {};
var i;
my.controller=controller;
my.name=name;
my.hand=[];
my.deck=[];
my.discards=[];
my.activeCard=null;
my.lives=10;
my.played=[];//on table
my.zone=new createjs.Container();//the table in front
my.zone.played=new createjs.Container();
my.zone.hand=new createjs.Container();
my.zone.addChild(my.zone.played, my.zone.hand);
if(playedPosition){
my.zone.hand.y=0;
my.zone.played.y=200;
} else {
my.zone.hand.y=200;
my.zone.played.y=0;
}
my.buildDeck=function(numCards){
var i,t;
for(i=0; i<numCards; i++){
t = new Card(Utils.getRandomInt(1,9),Utils.getRandomInt(1,9), my.controller.settings)
my.deck.push(t)
}
}
//PHASES
my.draw=function(num){
my.showHand()
var i,t;
if(num==undefined){
num=1
}
for(i=0; i<num; i++){
if(my.hand.length>4){
my.discards.push(my.deck.pop())
console.log("too many cards, discarded a card")
} else {
t=my.deck.pop()
my.zone.hand.addChild(t)
t.x=Game.stage.canvas.width+300
t.rotation=200
createjs.Tween.get(t).to(
{
x:my.hand.length*(my.controller.settings.cardWidth+my.controller.settings.cardOffset),
rotation:0,
y:0
}, 500+(500*i))
my.hand.push(t)
console.log("drew a card")
}
}
//remove "summoning sickness"
for(i=0; i<my.played.length; i++){
my.played[i].wakeUp();
}
}
my.castPhase=function(){
var i;
for(i=0; i<my.hand.length; i++){
my.hand[i].on("click", my.castSpell)
}
}
my.attackPhase=function(){
var i;
my._removeEventListeners()
for(i=0; i<my.played.length; i++){
if(my.played[i].ready){
my.played[i].on("click", my.attack)
}
}
}
my.discardPhase=function(){
var i;
my._removeEventListeners()
for(i=0; i<my.hand.length; i++){
my.hand[i].on("click", my.discard)
}
}
my.cleanUpPhase=function(){
my._removeEventListeners()
my.hideHand()
}
//PHASE ACTIONS
my.castSpell=function(e){
var c = e.currentTarget;
console.log(c, my)
var t=my.hand.splice(my.hand.indexOf(c),1)[0]
console.log(t)
var newY = playedPosition ? 200 : -200;
createjs.Tween.get(t).to(
{
y:newY,
x:my.played.length*(my.controller.settings.cardWidth+my.controller.settings.cardOffset)
}, 500).call(function(){
my.zone.hand.removeChild(t)
my.zone.played.addChild(t)
t.y=0
})
my.played.push(t)
t.sleep()
console.log("played a card")
Game.nextPhase();
}
my.attack=function(e){
var c = e.currentTarget;
console.log(c)
if(my.activeCard==c){
c.alpha=1;
my.activeCard=null;
} else {
my.activeCard=c;
c.alpha=0.8
//TODO EL på opponent etc, slet ikke lavet
}
}
my.discard=function(e){
var c = e.currentTarget;
my.hand.splice(my.hand.indexOf(c, 1))
Game.nextPhase();
}
//HELPERS
my.showHand=function(){
//TODO re layout cards
my.zone.hand.alpha=1
my._layoutHand()
}
my.hideHand=function(){
my.zone.hand.alpha=0
}
my._layoutHand=function(){
var posX= 0, i
my.zone.hand.removeAllChildren()
for(i=0; i<my.hand.length; i++){
my.hand[i].x=posX
posX+=(my.controller.settings.cardWidth+my.controller.settings.cardOffset)
my.zone.hand.addChild(my.hand[i])
}
}
my._removeEventListeners=function(){
var i;
for(i=0; i<my.hand.length; i++){
my.hand[i].removeAllEventListeners("click");
}
for(i=0; i<my.played.length; i++){
my.played[i].removeAllEventListeners("click");
}
}
return my;
};