Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added an event system and a cop that appears. Cop behaviour is

unfinished.
  • Loading branch information...
commit f5f7decf3ccea1a8c37b5979c0e63714242de610 1 parent 8864184
Rob authored
View
3  dialog.js
@@ -94,8 +94,7 @@ PlantingDialog.prototype.pick = function(which){
var plot = game.plots[which];
game.farmer.addItem(plot.contains);
- plot.contains.removeSprite();
- plot.contains = null;
+ plot.removePlant();
view.drawInventory();
this.close();
}
View
92 events.js
@@ -0,0 +1,92 @@
+/**
+ * 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 Events = {
+ eventsMax: 100,
+ allowCop: false,
+
+ randomEvent: function(){
+ var which = Math.floor(Math.random() * Events.eventsMax);
+
+ if (which > 98){
+ Events.cop();
+ }
+ },
+
+ cop: function(){
+ if (!Events.allowCop){
+ // cops need to be activated by selling an illegal plant
+ return;
+ }
+ if ($("#cop").length > 0){
+ // don't add a new cop if there is already one on screen
+ return;
+ }
+
+ var entry = Math.floor(Math.random() * 500) + 100;
+ function Cop(){
+ game.playground
+ .addSprite("cop", {animation: animations.cop.anim,
+ width: animations.cop.width, height: animations.cop.height,
+ posx: 0, posy: entry});
+ this.elem = $("#cop");
+ this.state = "initialize";
+ this.visitedPlots = [];
+ }
+
+ Cop.prototype.think = Events.copThink;
+
+ game.addThinker(new Cop(), 0, entry);
+ },
+
+ copThink: function(){
+ // TODO: Cop behaviour:
+ // Cop goes and looks at each plot in turn for illegal plants.
+ // If one is found it is taken and the cop gives a fine.
+ if (this.state == "initialize"){
+ // just grab the first plot
+ for (var i in game.plots){
+ if (i.match(/plot/)){
+ this.targetPlot = game.plots[i];
+ break;
+ }
+ }
+ this.state = "movingToPlot";
+ }else if (this.state == "movingToPlot"){
+ // if (near plot){
+ // this.state = "checkingPlot";
+ // }else{
+ // TODO: Pull out moving information from farmer into a function and load it in as a mixin
+ // move closer
+ // }
+ }else if (this.state == "checkingPlot"){
+ if (this.targetPlot.contains.illegal){
+ game.farmer.addMoney(this.targetPlot.contains.value * 2);
+ this.targetPlot.removePlant();
+ }
+ this.visitedPlots.push(this.targetPlot);
+ this.targetPlot = null;
+ this.state = "lookingForPlot";
+ }else if (this.state == "lookingForPlot"){
+ // if (no more plots to check){
+ // leave
+ // }else{
+ // search for an unvisited plot
+ // }
+ }
+ }
+}
View
4 farmer.js
@@ -192,3 +192,7 @@ Farmer.prototype.collided = function(collision){
this.targetPoint = undefined;
this.target = undefined;
}
+
+Farmer.prototype.addMoney = function(amount){
+ this.money += amount;
+}
View
BIN  images/cop.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1  index.html
@@ -47,6 +47,7 @@
<script type = "text/javascript" src = "jquery.gamequery-0.4.0.js"></script>
<script type = "text/javascript" src = "jquery.gamequery.ext.js"></script>
<script type = "text/javascript" src = "helpers.js"></script>
+ <script type = "text/javascript" src = "events.js"></script>
<script type = "text/javascript" src = "shop.js"></script>
<script type = "text/javascript" src = "vector.js"></script>
<script type = "text/javascript" src = "farmer.js"></script>
View
10 initialize.js
@@ -72,8 +72,18 @@ function loadObjects(){
var plot = $("#plot" + i);
plot.contains = null;
plot.isPlot = true;
+ plot.removePlant = function(){
+ this.contains.removeSprite();
+ this.contains = null;
+ };
game.addObject(plot, 260 + i * 40, 350);
}
+
+ animations.cop = {
+ anim: new Animation({imageURL: "images/cop.png"}),
+ width: 17,
+ height: 31
+ };
}
function loadShop(){
View
15 jarm.js
@@ -27,6 +27,7 @@ var game = {
farmer: null,
plots: {},
objectsHash: {},
+ thinkers: [],
// config params
worldSize: 2000,
@@ -38,7 +39,7 @@ var animations = {
};
game.objects = new $.gameQuery.QuadTree(game.worldSize, game.worldSize);
game.addObject = function(obj, x, y){
- if (x !== undefined && x !== undefined){
+ if (x !== undefined && y !== undefined){
game.objects.add(obj, x, y);
}
@@ -49,6 +50,12 @@ game.addObject = function(obj, x, y){
game.objectsHash[obj.attr("id")] = obj;
}
}
+game.addThinker = function(thinker, x, y){
+ if (x !== undefined && y !== undefined){
+ game.objects.add(thinker, x, y);
+ }
+ game.thinkers.push(thinker);
+}
var view;
function toWindowCoords(hash){
@@ -93,6 +100,12 @@ function gameLoop(){
}
}
+ $.each(game.thinkers, function(i, obj){
+ obj.think();
+ });
+
+ Events.randomEvent();
+
view.frame(timeElapsed);
return false;
}
View
3  plants.js
@@ -37,6 +37,8 @@ function Plant(type, name, animation){
this.type = type;
this.name = name;
+ this.illegal = false;
+
this.growth = 0;
this.growTime = 1000;
this.animation = animation;
@@ -100,6 +102,7 @@ function Marijuana(){
offsetY: -2
});
this.value = 100;
+ this.illegal = true;
}
Marijuana.prototype = Plant.prototype;
View
5 shop.js
@@ -19,5 +19,8 @@ function Shop(){
}
Shop.prototype.sell = function(obj){
- game.farmer.money += obj.value;
+ if (obj.illegal){
+ Events.allowCop = true;
+ }
+ game.farmer.addMoney(obj.value);
}
Please sign in to comment.
Something went wrong with that request. Please try again.