-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdialog.js
More file actions
158 lines (133 loc) · 3.97 KB
/
dialog.js
File metadata and controls
158 lines (133 loc) · 3.97 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
/**
* Copyright (C) 2010 Rob Britton
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function Dialog(selector){
if (selector === undefined){
selector = "#dialog";
}
game.state = "paused";
this.dlg = $(selector)
.children(".dlg-content")
.end()
.show();
}
Dialog.prototype.close = function(){
game.state = "playing";
this.dlg.hide();
};
Dialog.prototype.setContent = function(html){
this.dlg.children(".dlg-content").html(html);
};
function PlantingDialog(plot){
Dialog.call(this);
var text;
if (plot.contains !== null){
var plantName;
if (plot.contains.type.match(/^[aeiou]/i)){
plantName = "n " + plot.contains.type;
}else{
plantName = " " + plot.contains.type;
}
text = "This plot contains a" + plantName + " plant.<br />";
if (plot.contains.fullGrown()){
text += '<a href = "#" onclick = "game.dialog.pick(\'' + plot.attr("id") + '\'); return false">Pick It</a>';
}else{
text += "When it is full grown you will be able to pick it.";
}
}else{
// find something to plant
text = "This plot is empty. What would you like to plant?<br />";
var found = false;
game.farmer.eachItem(function(i, item){
if (item.isSeed()){
found = true;
text += '<a href = "#" onclick = "game.dialog.plant(' + i + '); return false">' +
item.name + '</a><br />';
}
});
if (!found){
text = "You have nothing to plant. Find some seeds!";
}else{
text += '<a href = "#" onclick = "game.dialog.close(); return false">Nothing</a>';
}
}
this.plot = plot;
this.setContent(text);
}
PlantingDialog.prototype = Dialog.prototype;
PlantingDialog.prototype.plant = function(which){
var plant = game.farmer.getItem(which);
if (!plant.isSeed()){
console.log("Error: tried to plant non-seed");
return;
}
game.farmer.removeItem(which);
game.plant(this.plot, plant);
view.drawInventory();
this.close();
}
PlantingDialog.prototype.pick = function(which){
var plot = game.plots[which];
game.farmer.addItem(plot.contains);
plot.contains.removeSprite();
plot.contains = null;
view.drawInventory();
this.close();
}
function ShopDialog(shop){
Dialog.call(this, "#shop-dlg");
var text = "";
// Load up buy page
text = "Nothing to buy yet!";
$("#shop-buy").html(text);
// Load up sell page
var found = false;
text = "";
game.farmer.eachItem(function(i, obj){
if (!obj.isSeed()){
found = true;
text += '<a href = "#" onclick = "game.dialog.sell(' + i + '); return false">' +
obj.name + '</a><br />';
}
});
if (!found){
text = "You have nothing to sell. Grow some plants!";
}else{
text = "What would you like to sell?<br />" +
text +
'<a href = "#" onclick = "game.dialog.close(); return false">Nothing</a>';
}
this.shop = shop;
$("#shop-sell").html(text);
}
ShopDialog.prototype = Dialog.prototype;
ShopDialog.prototype.sell = function(index){
var obj = game.farmer.getItem(index);
// TODO: make it so you can sell seeds
if (obj.isSeed()){
console.log("Error: tried to sell seed");
}
game.farmer.removeItem(index);
this.shop.sell(obj);
view.drawInventory();
this.close();
}
ShopDialog.prototype.openPanel = function(which){
if (which == "sell"){
$("#shop-sell").show();
$("#shop-buy").hide();
}else{
$("#shop-sell").hide();
$("#shop-buy").show();
}
}