forked from bonder/jarm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplants.js
More file actions
180 lines (162 loc) · 3.86 KB
/
plants.js
File metadata and controls
180 lines (162 loc) · 3.86 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function Plant(type, name, animation){
if (type !== undefined){
if (name === undefined){
name = type + " seeds";
}
if (animation === undefined){
animation = {};
}
animation = $.extend({
frame: 0,
numFrames: 4,
imageURL: "images/" + type + ".png",
delta: 20,
offsetX: 0,
offsetY: 0,
height: 20,
width: 20
}, animation);
this.type = type;
this.name = name;
this.illegal = false;
this.growth = 0;
this.growTime = 1000;
this.animation = animation;
}
}
Plant.num = 0;
Plant.prototype.grow = function(){
if (this.growth < this.growTime){
this.growth++;
if (this.growth % Math.floor(this.growTime / (this.animation.numFrames - 1)) == 0){
this.nextFrame();
}
}
}
Plant.prototype.fullGrown = function(){
return this.growth >= Math.floor(this.growTime / (this.animation.numFrames - 1)) * (this.animation.numFrames - 1);
}
Plant.prototype.isSeed = function(){
return this.name.match(/(seed|baby)/);
}
Plant.prototype.nextFrame = function(){
if (this.animation.frame < this.animation.numFrames){
this.elem.css("backgroundPosition", "-" + (this.animation.frame * this.animation.delta) + "px 0px");
this.animation.frame++;
if (this.fullGrown()){
this.growUp();
}
}
}
Plant.prototype.growUp = function(){
this.name = this.name.replace(" seeds", "");
}
Plant.prototype.createSprite = function(x, y){
var num = ++Plant.num;
this.animation.frame = 1;
if (y === undefined){
y = x.top;
x = x.left;
}
$.playground().addSprite("plant" + num, {
posx: x + this.animation.offsetX,
posy: y + this.animation.offsetY,
width: this.animation.width,
height: this.animation.height
});
this.elem = $("#plant" + num);
this.elem.plant = this;
this.elem.css({
backgroundImage: "url(" + this.animation.imageURL + ")"
});
return this.elem;
}
Plant.prototype.removeSprite = function(){
this.elem.remove();
}
function Marijuana(){
Plant.call(this, "marijuana", "marijuana seeds", {
offsetY: -2
});
this.value = 100;
this.illegal = true;
}
Marijuana.prototype = Plant.prototype;
function Rose(){
Plant.call(this, "rose");
this.value = 20;
}
Rose.prototype = Plant.prototype;
function Banana(){
Plant.call(this, "banana", "banana seeds", {
height: 40,
offsetY: -24
});
this.value = 5;
}
Banana.prototype = Plant.prototype;
function Apple(){
Plant.call(this, "apple", "apple seeds", {
height: 27,
offsetY: -10
});
this.value = 5;
}
Apple.prototype = Plant.prototype;
function Cactus(){
Plant.call(this, "cactus", "cactus seeds", {
height: 32,
offsetY: -15
});
this.value = 5;
}
Cactus.prototype = Plant.prototype;
function Carrot(){
Plant.call(this, "carrot");
this.value = 5;
}
Carrot.prototype = Plant.prototype;
function Cabbage(){
Plant.call(this, "cabbage");
this.value = 5;
}
Cabbage.prototype = Plant.prototype;
function Mushroom(){
Plant.call(this, "mushroom", "baby mushrooms");
this.value = 5;
}
Mushroom.prototype = new Plant();
Mushroom.prototype.growUp = function(){
this.name = this.name.replace(/baby /, "");
}
var plants = {
maxNum: 100,
getRandomSeed: function(){
return plants.getSeed(Math.floor(Math.random() * plants.maxNum));
},
getSeed: function(which){
if (which < 0 || which >= plants.maxNum){
console.log("Plant fetch out of bounds.");
return null;
}
if (which >= 98){
return new Marijuana();
}else if (which >= 96){
return new Rose();
}else if (which >= 91){
return new Apple();
}else if (which >= 86){
return new Banana();
}else if (which >= 81){
return new Carrot();
}else if (which >= 76){
return new Cabbage();
}else if (which >= 71){
return new Cactus();
}else if (which >= 66){
return new Mushroom();
}else{
return null;
}
}
};