-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjarm.js
172 lines (143 loc) · 4.13 KB
/
jarm.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
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
167
168
169
170
171
172
/**
* 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/>.
*/
var Animation = $.gameQuery.Animation;
var keycodes = $.gameQueryExt.keycodes;
var keyTracker;
var game = {
background: null,
playground: null,
dialog: null,
farmer: null,
plots: {},
// config params
worldSize: 2000,
searchRadius: 40,
state: "not started"
};
var animations = {
};
game.objects = new $.gameQueryExt.QuadTree(game.worldSize, game.worldSize);
var view;
function toWindowCoords(hash){
var pg = game.playground.offset();
var bg = game.background.position();
return {
left: hash.left + bg.left + pg.left,
top: hash.top + bg.top + pg.top
};
}
function toWorldCoords(x, y){
var pg = game.playground.offset();
var bg = game.background.position();
return new Vector(
x - pg.left - bg.left,
y - pg.top - bg.top
);
}
function visibleObjects(){
return game.objects.get(-game.background.position().left, -game.background.position().top,
game.playground.width(), game.playground.height());
}
function gameLoop(){
var timeElapsed = $.gameQueryExt.getTimeElapsed();
if (game.state == "playing"){
game.farmer.update(timeElapsed / JarmView.frameRate);
var plot;
for (var i in game.plots){
if (i.match(/plot/)){
plot = game.plots[i];
if (plot.contains !== null && !plot.contains.fullGrown()){
plot.contains.grow();
}
}
}
}
view.frame(timeElapsed);
return false;
}
function activateBush(bush){
// TODO: Make it so a bush can run out of seeds
var plant = plants.getRandomSeed();
if (plant === null){
view.addMessage("Nothing found.");
}else{
view.addMessage("Found " + plant.name + ".");
game.farmer.addItem(plant);
view.drawInventory();
}
}
function activatePlot(plot){
game.dialog = new PlantingDialog(plot);
}
function activateShop(shop){
game.dialog = new ShopDialog(shop);
}
// This one is called when the user presses space
function activate(){
var nearby = visibleObjects();
var obj;
$("#msg").html("");
for (var i = 0; i < nearby.length; i++){
obj = nearby[i];
// TODO: Use a better near() system
if (near(obj, game.farmer.elem, game.searchRadius * 2)){
if (obj.attr("id") == "shop"){
activateShop(game.shop);
}
}
if (near(obj, game.farmer.elem, game.searchRadius)){
if (obj.attr("id").match(/bush/)){
activateBush(obj);
break;
}else if (obj.attr("id").match(/plot/)){
activatePlot(game.plots[obj.attr("id")]);
break;
}
}
}
}
game.plant = function(plot, plant){
plot.contains = plant;
plant.createSprite(
plot.position().left + 5,
plot.position().top - 2
);
}
function onKeyPress(ev){
if (game.state == "playing"){
if (ev.which == keycodes.space){
activate();
}
}else if (game.state == "paused"){
if (ev.keyCode == keycodes.escape && game.dialog !== null){
game.dialog.close();
}
}
}
function onClick(ev){
if (game.state == "playing"){
if (ev.originalTarget.tagName.toLowerCase() != "a"){
// When you click a dialog item in Firefox it
// sends this click event, so we need to make
// sure we didn't just hit a link
// TODO: switch the close X to an A tag
var pg = game.playground.position();
if (ev.clientX > pg.left && ev.clientX < pg.left + game.playground.width() &&
ev.clientY > pg.top && ev.clientY < pg.top + game.playground.height()){
var pos = toWorldCoords(ev.clientX, ev.clientY);
game.farmer.moveTo(pos);
}
}
}
}