Permalink
Browse files

Added camera movement.

There is a bug in here that makes the screen shake or become blurred. I
still haven't found out what that is.
  • Loading branch information...
1 parent 6d03913 commit f7f3e5bcc85f9acefb528f25eb2947501565f811 Shedo Surashu committed Feb 19, 2012
Showing with 89 additions and 17 deletions.
  1. +1 −1 src/Main.as
  2. +1 −1 src/actors/Player.as
  3. +66 −0 src/system/Camera.as
  4. +7 −3 src/system/GC.as
  5. +0 −1 src/system/Map.as
  6. +14 −11 src/worlds/Game.as
View
@@ -7,7 +7,7 @@ package
import worlds.*;
- [SWF(width="800", height="450", frameRate="60")]
+ [SWF(width="640", height="480", frameRate="60")]
public class Main extends Engine
{
public function Main()
View
@@ -32,7 +32,7 @@ package actors
y = position.y;
// Temporary until I can think of a way to make this dynamic.
- setHitbox(32,32,0,-16);
+ setHitbox(32,48,0,0);
}
View
@@ -0,0 +1,66 @@
+package system
+{
+ import actors.Player;
+
+ import net.flashpunk.FP;
+
+ public class Camera
+ {
+ private var debugCoord:int;
+
+ public function Camera(){}
+
+ public function adjust(mapWidth:int, mapHeight:int, player:Player):void {
+ var newCameraX:int = ((player.x + player + player.width)/2) - (FP.width/2);
+ var newCameraY:int = ((player.y + player + player.height)/2) - (FP.height/2);
+
+ // Checks for outbound locations then sets them inside the limit.
+ if (newCameraX < 0) {
+ newCameraX = 0;
+ } else if ((newCameraX + FP.width) > mapWidth) {
+ newCameraX = mapWidth - FP.width;
+ }
+ if (newCameraY < 0) {
+ newCameraY = 0;
+ } else if ((newCameraY + FP.height) > mapHeight) {
+ newCameraY = mapHeight - FP.height;
+ }
+
+ FP.camera.x = newCameraX;
+ FP.camera.y = newCameraY;
+ }
+
+ public function followPlayer(mapWidth:int, mapHeight:int, player:Player):void {
+
+ // Horizontal Checking
+ if ((player.x - FP.camera.x) < GC.CD_OFFSET) {
+ if (FP.camera.x > 0) {
+ // Will execute if the left-most edge is not yet reached.
+ FP.camera.x -= GC.CD_SPEED;
+ }
+ } else if (((FP.camera.x + FP.width) - (player.x + player.width)) < GC.CD_OFFSET) {
+ if ((FP.camera.x + FP.width) < mapWidth) {
+ // Will execute if the right-most edge is not yet reached.
+ FP.camera.x += GC.CD_SPEED;
+ }
+ }
+
+ // Vertical Checking
+ if ((player.y - FP.camera.y) < GC.CD_OFFSET) {
+ if (FP.camera.y > 0) {
+ // Will execute if the top-most edge is not yet reached.
+ FP.camera.y -= GC.CD_SPEED;
+ }
+ } else if (((FP.camera.y + FP.height) - (player.y + player.height)) < GC.CD_OFFSET) {
+ if ((FP.camera.y + FP.height) < mapHeight) {
+ // Will execute if the bottom-most edge is not yet reached.
+ FP.camera.y += GC.CD_SPEED;
+ }
+ }
+
+
+ FP.log("Camera: x="+FP.camera.x.toString()+" y="+FP.camera.y.toString());
+
+ }
+ }
+}
View
@@ -17,8 +17,8 @@ package system
{
// Game Details
public static const GD_NAME:String = "Blaarpgh";
- public static const GD_WIDTH:int = 800;
- public static const GD_HEIGHT:int = 450;
+ public static const GD_WIDTH:int = 640;
+ public static const GD_HEIGHT:int = 480;
public static const GD_FPS:int = 60;
public static const GD_SCALE:int = 2; // Minimum value is 1.
public static const GD_FLASHPUNKCONSOLE:Boolean = true;
@@ -46,7 +46,11 @@ package system
public static const AD_PLAYERSPRITEWIDTH:int = 32;
public static const AD_PLAYERSPRITEHEIGHT:int = 48;
public static const AD_PLAYERDEFAULTANIM:String = "stand_down";
- public static const AD_PLAYERDEFAULTSPEED:int = 1;
+ public static const AD_PLAYERDEFAULTSPEED:int = 2;
+
+ // Camera Details
+ public static const CD_OFFSET:int = 200;
+ public static const CD_SPEED:int = 2;
public function GC(){}
}
View
@@ -14,7 +14,6 @@ package system
public function Map(_resource:Class) {
resource = _resource;
xml = FP.getXML(resource);
-// FP.log("Map: "+resource.toString());
width = (xml.@width * xml.@tilewidth);
height = (xml.@height * xml.@tileheight);
View
@@ -32,18 +32,29 @@ package worlds
public var currentMapIndex:int = 0;
public var currentMap:Map;
+ // Camera
+ public var gameCamera:Camera;
+
public function Game()
{
- player = new Player(new Point(50,50));
- npc_italy01 = new NPC(new Point(100,50));
+ player = new Player(new Point(500,250));
+// npc_italy01 = new NPC(new Point(100,50));
maps = Maps.list;
+ // Set default camera location
+ gameCamera = new Camera();
+
MapLoad();
}
+ override public function update():void {
+ gameCamera.followPlayer(currentMap.width, currentMap.height, player);
+ super.update();
+ }
public function MapLoad():void {
currentMap = maps[currentMapIndex];
+ gameCamera.adjust(currentMap.width, currentMap.height, player);
mapMarkerCollisions = new _Collisions(currentMap.xml);
mapMarkerTriggers = new _Triggers(currentMap.xml);
@@ -64,16 +75,8 @@ package worlds
add(player);
add(mapAboveEntity);
add(mapAboveAll);
- FP.log("Character test started. Currently testing: America");
-
-// RPGFP.currentMap = RPGFP.maps[RPGFP.currentMapIndex];
-// add(new UnderGround(currentMap.xml,TILE_SET));
-// FP.world.add(new Ground(currentMap.xml, TILE_SET));
-// FP.world.add(new AboveGround(currentMap.xml, TILE_SET));
-// FP.world.add(new Player(new Point(50,50)));
-// FP.world.add(new AboveEntity(currentMap.xml, TILE_SET));
-// FP.world.add(new AboveAll(currentMap.xml, TILE_SET));
+ FP.log("Map has been loaded.");
}
}

0 comments on commit f7f3e5b

Please sign in to comment.