Permalink
Browse files

Simple GravityWell implementation.

  • Loading branch information...
1 parent ee2bf91 commit 003e53fdeb8f6aa76485782ef7d4ae6ff9d91986 @presstube committed Oct 26, 2011
Showing with 52 additions and 38 deletions.
  1. +1 −0 index.html
  2. +19 −4 src/hunted/GravityWell.js
  3. +11 −10 src/hunted/HuntedApp.js
  4. +21 −23 src/hunted/ItemScroller.js
  5. +0 −1 src/hunted/Nav.js
View
@@ -36,6 +36,7 @@
<script src="src/hunted/ScaleStage.js"></script>
<script src="src/hunted/TrackingStage.js"></script>
<script src="src/hunted/ParallaxScroller.js"></script>
+ <script src="src/hunted/GravityWell.js"></script>
<script src="src/hunted/ItemScroller.js"></script>
<script src="src/hunted/ShipControlsKeyboard.js"></script>
<script src="src/hunted/ExhaustPipe.js"></script>
View
@@ -1,13 +1,28 @@
(function(window){
- var GravityWell = function() {this.initialize();};
+ var GravityWell = function(target) {this.initialize(target);};
var p = GravityWell.prototype = new Container();
p.Container_initialize = p.initialize;
- p.initialize = function() {
- ths.Container_initialize();
+ p.initialize = function(target) {
+ this.Container_initialize();
-
+ var radius = 100;
+
+ this.addChild(PTUtils.makeCircle('#FFF', radius));
+
+ this.alpha = 0.5;
+
+
+ this.tick = function() {
+ var globalTargetPos = target.localToGlobal(0,0);
+ var dist = PTUtils.distance(new Point(0, 0), this.globalToLocal(globalTargetPos.x, globalTargetPos.y));
+
+ if (dist < radius) {
+ var degrees = PTUtils.angleDegrees(target, this);
+ target.addForce(PTUtils.polarDegrees(6, degrees));
+ }
+ };
};
window.GravityWell = GravityWell;
View
@@ -25,22 +25,15 @@
numItems: 50
}),
- itemScroller = new ItemScroller({
- scaleStage: scaleStage,
- trackingStage: trackingStage,
- wrapRadius: wrapRadius,
- numItems: 50
- }),
-
ship = new Ship({
name: "hero",
controlsClass: ShipControlsKeyboard,
skinClass: ShipSkinGeneric,
drag: 0.95,
thrustLimit: 2,
- boostThrust: 3,
- boostFuelLimit: 20,
- boostRegenerateFrequency: 10,
+ boostThrust: 5,
+ boostFuelLimit: 50,
+ boostRegenerateFrequency: 5,
steeringResponse: 2,
steeringLimit: 10,
launcherSpread: 5,
@@ -53,6 +46,14 @@
projectiles: projectiles
}),
+ itemScroller = new ItemScroller({
+ scaleStage: scaleStage,
+ trackingStage: trackingStage,
+ wrapRadius: wrapRadius,
+ numItems: 50,
+ ship: ship
+ }),
+
// avoider = new Ship({
// name: "avoider",
// controlsClass: ShipControlsAIAvoid,
View
@@ -8,15 +8,18 @@
this.Container_initialize();
- this.props = props || {};
+ var _p = this.props = props || {};
var that = this,
trackingStage = props.trackingStage,
scaleStage = (props && props.scaleStage) ? props.scaleStage : this,
wrapRadius = (props && props.wrapRadius) ? props.wrapRadius : 300,
numItems = (props && props.numItems) ? props.numItems : 60,
globalScaleStageCenter = new Point(),
- direction = 0;
+ direction = 0,
+ items = [];
+
+ console.log("wr: " + wrapRadius);
this.hitTarget = undefined;
@@ -28,38 +31,33 @@
function makeItem(i) {
var color = (i === 0) ? '#0F0' : '#0FF';
- var item = PTUtils.makeCircle(color, 2);
- var spawnPoint = PTUtils.polarDegrees(Math.random()*wrapRadius, Math.random()*360);
+ var item = new GravityWell(_p.ship);
+ // var item = PTUtils.makeCircle(color, 2);
+ var spawnPoint = PTUtils.polarDegrees((Math.random()*wrapRadius), Math.random()*360);
item.x = spawnPoint.x;
item.y = spawnPoint.y;
item.i = i;
- item.tick = function() {
- var globalItemCenter = this.localToGlobal(0, 0);
- var distanceFromCenter = PTUtils.distance(globalItemCenter, globalScaleStageCenter);
-
- // if (distanceFromCenter < 40) {
- // var hitLocal = this.localToLocal(0, 0, that.hitTarget);
- // this.scaleX = this.scaleY = that.hitTarget.hitTest(hitLocal.x, hitLocal.y) ? 8 : 1;
- // that.hitTarget.controls.triggerBoost();
- // }
-
- if ( distanceFromCenter > wrapRadius ) {
- var localScaleStageCenter = that.globalToLocal(globalScaleStageCenter.x, globalScaleStageCenter.y);
- var respawnAngleRadians = PTUtils.getOppositeAngleRadians(direction);
- var respawnPoint = PTUtils.polarRadians(wrapRadius, respawnAngleRadians);
- this.x = localScaleStageCenter.x + respawnPoint.x;
- this.y = localScaleStageCenter.y + respawnPoint.y;
- }
- };
+ items.push(item);
that.addChild(item);
}
};
- populate(300);
+ populate(100);
this.tick = function() {
globalScaleStageCenter = scaleStage.localToGlobal(0, 0);
direction = PTUtils.angleRadians(trackingStage.getAmountToMove(), new Point() );
+ _.each(items, function(item) {
+ var globalItemCenter = item.localToGlobal(0, 0);
+ var distanceFromCenter = PTUtils.distance(globalItemCenter, globalScaleStageCenter);
+ if ( distanceFromCenter > wrapRadius ) {
+ var localScaleStageCenter = that.globalToLocal(globalScaleStageCenter.x, globalScaleStageCenter.y);
+ var respawnAngleRadians = PTUtils.getOppositeAngleRadians(direction);
+ var respawnPoint = PTUtils.polarRadians(wrapRadius, respawnAngleRadians);
+ item.x = localScaleStageCenter.x + respawnPoint.x;
+ item.y = localScaleStageCenter.y + respawnPoint.y;
+ }
+ });
};
};
View
@@ -28,7 +28,6 @@
}
function registerNavItem(navItem) {
- console.log("registering nav item");
navItems.push(navItem);
}

0 comments on commit 003e53f

Please sign in to comment.