Skip to content

Commit

Permalink
add field, ball and collision
Browse files Browse the repository at this point in the history
  • Loading branch information
aberestovskyi committed Jun 25, 2016
1 parent 2155827 commit ed86a16
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 23 deletions.
16 changes: 6 additions & 10 deletions src/com/pinball/core/PinPongAppLaunch.as
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.pinball.core
{
import com.pinball.core.configs.GameConfig;
import com.pinball.data.GameStatics;

import flash.display.Sprite;
import flash.display.StageAlign;
Expand Down Expand Up @@ -43,17 +44,12 @@ package com.pinball.core
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

var stageWidth:Number = int(stage.stageWidth);
var stageHeight:Number = int(stage.stageHeight);
GameStatics.GAME_WIDTH = stage.stageWidth;
GameStatics.GAME_HEIGHT = stage.stageHeight;

var viewPort:Rectangle = RectangleUtil.fit(
new Rectangle(0, 0, stageWidth, stageHeight),
new Rectangle(0, 0, stage.stageWidth, stage.stageHeight),
ScaleMode.SHOW_ALL, false);

_starling = new Starling(PingPongGameCore, stage, viewPort, null, "auto", "auto");
_starling.stage.stageWidth = stageWidth;
_starling.stage.stageHeight = stageHeight;
_starling = new Starling(PingPongGameCore, stage);
_starling.stage.stageWidth = GameStatics.GAME_WIDTH;
_starling.stage.stageHeight = GameStatics.GAME_HEIGHT;
_starling.enableErrorChecking = false;
_starling.simulateMultitouch = false;
_starling.showStats = true;
Expand Down
11 changes: 11 additions & 0 deletions src/com/pinball/data/GameStatics.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Created by andrey on 26.06.2016.
*/
package com.pinball.data
{
public class GameStatics
{
public static var GAME_WIDTH:Number;
public static var GAME_HEIGHT:Number;
}
}
54 changes: 52 additions & 2 deletions src/com/pinball/ui/controls/Ball.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,71 @@
*/
package com.pinball.ui.controls
{
import starling.animation.IAnimatable;
import starling.core.Starling;
import starling.display.Shape;
import starling.display.Sprite;

public class Ball extends Sprite
public class Ball extends Sprite implements IAnimatable
{

private var _maxSpeed:Number;
private var _radius:Number;
private var _vx:Number = 0;
private var _vy:Number = 0;
public function Ball(radius:Number)
{
super();

_radius = radius;
var shape:Shape = new Shape();
shape.graphics.beginFill(0x000000);
shape.graphics.drawCircle(0,0,radius);
shape.graphics.endFill();
addChild(shape);
}

public function setVelocity(speed:Number, angle:Number):void
{
_maxSpeed = speed;
_vx = speed*Math.cos(angle*Math.PI/180);
_vy = speed*Math.sin(angle*Math.PI/180);
}

public function advanceTime(time:Number):void
{
_vy+=0.8;

var time1:Number = time / (Starling.current.nativeStage.frameRate / 1000);
var xx:Number = x + _vx * time1;
var yy:Number = y + _vy * time1;

x = xx;
y = yy;
}

public function get radius():Number
{
return _radius;
}

public function get vx():Number
{
return _vx;
}

public function get vy():Number
{
return _vy;
}

public function set vx(value:Number):void
{
_vx = value;
}

public function set vy(value:Number):void
{
_vy = value;
}
}
}
2 changes: 2 additions & 0 deletions src/com/pinball/ui/controls/HitObject.as
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ package com.pinball.ui.controls
addChild(shape);
}


public function get radius():Number
{
return _radius;
}

}
}
103 changes: 99 additions & 4 deletions src/com/pinball/ui/view/game/GameView.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,128 @@
*/
package com.pinball.ui.view.game
{
import com.pinball.data.GameStatics;
import com.pinball.ui.controls.Ball;
import com.pinball.ui.controls.HitObject;
import com.pinball.ui.view.AbstractView;
import com.pinball.utils.Vector2dUtils;

public class GameView extends AbstractView
import flash.geom.Point;
import flash.geom.Rectangle;

import starling.animation.IAnimatable;
import starling.core.Starling;

public class GameView extends AbstractView implements IAnimatable
{
private var _hitObjects:Vector.<HitObject>;
private var _ball:Ball;
private var _boundsRect:Rectangle;
private var _isInField:Boolean;
public function GameView()
{
super();
}


override protected function complete():void
{
super.complete();

_ball = new Ball(10);
_ball.x = 640;
_ball.y = 400;
addChild(_ball);
}

public function start():void
{
_isInField =false;
_ball.x = 640;
_ball.y = 400;
_ball.setVelocity(25 + Math.random()*5, -97 - Math.random()*3);
Starling.juggler.add(this);
Starling.juggler.add(_ball);
}

public function createField(rows:int, cols:int):void
{
var startX:Number = 200;
_hitObjects = new Vector.<HitObject>();

var padding:Number = 35;
var startX:Number = (GameStatics.GAME_WIDTH - cols * padding)*0.5;
var startY:Number = 100;
var padding:Number = 30;
var hitRadius:Number = 5;
var hitRadius:Number = 4;
var hitObject:HitObject;
_boundsRect = new Rectangle(startX - padding/2, startY, cols * padding + padding/2, rows * padding);
trace(_boundsRect);
for(var i:int = 0; i<rows; i++)
{
for(var j:int = 0; j<cols; j++)
{
hitObject = new HitObject(hitRadius);
hitObject.x = startX+ j * padding;
hitObject.y = startY + i * padding;
if(i%2 == 0)
hitObject.x+=padding/2;
addChild(hitObject);
_hitObjects.push(hitObject);
}
}


}

public function advanceTime(time:Number):void
{
for(var i:int = 0; i<_hitObjects.length; i++)
{
checkCollision(_hitObjects[i]);
}

checkBallForBorders();
}

private function checkBallForBorders():void
{
if(_ball.x - _ball.radius>_boundsRect.left && _ball.x + _ball.radius<_boundsRect.right)
{
_isInField = true;
}

if(_isInField)
{
if(_ball.x + _ball.radius>=_boundsRect.right)
{
_ball.x = _boundsRect.right-_ball.radius;
_ball.vx*=-1;
}
else if(_ball.x - _ball.radius<=_boundsRect.x)
{
_ball.x = _boundsRect.x + _ball.radius;
_ball.vx*=-1;
}
}
}

public function checkCollision(hitObject:HitObject):void
{
var xx:Number = _ball.x - hitObject.x;
var yy:Number = _ball.y - hitObject.y;
var distance:Number = Math.sqrt(xx * xx + yy * yy);
var totalRadius:Number = _ball.radius + hitObject.radius;
var diff:Number = totalRadius - distance;
if(diff >= 0)
{
var dx:Number = xx/distance;
var dy:Number = yy/distance;
_ball.x += diff * dx;
_ball.y += diff * dy;

var resultVector:Point = Vector2dUtils.getBounceVector(new Point(_ball.vx, _ball.vy), dy, -dx, dx, dy);
_ball.vx = resultVector.x/1.5;
_ball.vy = resultVector.y/1.5;
}
}
}
}
10 changes: 3 additions & 7 deletions src/com/pinball/ui/view/game/GameViewMediator.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
package com.pinball.ui.view.game
{
import com.pinball.ui.controls.Ball;
import flash.utils.setTimeout;

import robotlegs.starling.bundles.mvcs.Mediator;

Expand All @@ -22,12 +22,8 @@ package com.pinball.ui.view.game
{
super.initialize();

var ball:Ball = new Ball(10);
ball.x = 300;
ball.y = 20;
view.addChild(ball);

view.createField(10,15);
view.createField(10,10);
setTimeout(view.start, 1000);
}
}
}
43 changes: 43 additions & 0 deletions src/com/pinball/utils/Vector2dUtils.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Created by andrey on 25.06.2016.
*/
package com.pinball.utils
{
import flash.geom.Point;

public class Vector2dUtils
{
public function Vector2dUtils()
{
}

// find new vector bouncing from v2
public static function getBounceVector(ball:Point, dx:Number, dy:Number, lx:Number, ly:Number):Point
{
// projection of v1 on v2
var proj1:Point = projectVector(ball, dx, dy);
// projection of v1 on v2 normal
var proj2:Point = projectVector(ball, lx, ly);

// reverse projection on v2 normal
var lang:Number = Math.sqrt(proj2.x*proj2.x+proj2.y*proj2.y);
proj2.x = lx*lang;
proj2.y = ly*lang;

// add the projections
var proj:Point = new Point();
proj.x = proj1.x+proj2.x;
proj.y = proj1.y+proj2.y;
return proj;
}

public static function projectVector(v1:Point, dx:Number, dy:Number):Point
{
var dotProduct:Number = v1.x * dx + v1.y * dy;
var proj:Point = new Point();
proj.x = dotProduct * dx;
proj.y = dotProduct * dy;
return proj;
}
}
}

0 comments on commit ed86a16

Please sign in to comment.