sc endless scene wz - player

code for so called endless scenes with zoom  - player
 
Here we make our own collision system without using colision events but rather making comparison with actors in the range.

package scripts;
import com.stencyl.Engine;
import com.stencyl.behavior.ActorScript;
import com.stencyl.behavior.Script;
import com.stencyl.behavior.TimedTask;
import com.stencyl.models.Actor; 
import com.stencyl.models.actor.Collision;

class Player_torso_behav extends ActorScript
{
public var diag_text: Dtext = new Dtext(); var diag_rh:RH = new RH();  
public var diag_col:String = ""; 
public var anim_cycle:Float =0; public var player_speed = 1; 
public var posx:Float; public var posy:Float; // player position anti-aliasing
public var old_posx:Float; public var old_posy:Float; // for continious collision purpose. 
public var move_x: Float =0; public var move_y : Float =0; 

public var move_east:Bool = false; public var move_north:Bool = false; public var move_south = false; public var move_west = false; 
public var direction:Int =0; public var moving = false; 
public var arm_left:Actor = null; public var arm_right:Actor = null; 
public var ear_left:Actor = null; public var ear_right:Actor = null;
public var head:Actor = null; public var tail:Actor; 

public static var speed:Float = 1; 
public function new (a,b,c){super(b);}
     
override public function init()
{
this.diag_text.red_pwr =0; this.diag_text.gre_pwr =0; this.diag_text.blu_pwr =0; 
this.posx = actor.getX(); this.posy = actor.getY(); 
addKeyStateListener("a",press_west); addKeyStateListener("d",press_east);
addKeyStateListener("w",press_north); addKeyStateListener("s",press_south);
this.def(); actor.whenDrawingListeners.push(diag_drawing);
}

public function col(): Void // pairs with actors in list. 
{
this.diag_col = "no collisions detected";
var pl_dimension:Float = 512 * 0.8 * S_gameplay_behav.zoom; 
var pl_left_bound:Float = this.posx + 256 - (pl_dimension * 0.5); 
var pl_right_bound:Float = this.posx + 256 + (pl_dimension * 0.5); 
var pl_upper_bound:Float = this.posy + 256 - (pl_dimension * 0.5); 
var pl_lower_bound:Float = this.posy + 256 + (pl_dimension * 0.5); 
var xmin:Int = Std.int(Math.floor(S_gameplay_behav.disp_xcen)) -1; 
var xmax:Int = Std.int(Math.ceil(S_gameplay_behav.disp_xcen)) +2; 
var ymin:Int = Std.int(Math.floor(S_gameplay_behav.disp_ycen)) -1; 
var ymax:Int = Std.int(Math.ceil(S_gameplay_behav.disp_ycen)) +2; 
var y:Int = ymin; 
var x:Int = xmin; while(x < xmax)
{
y = ymin; while(y < ymax)
{
if(Std.is(S_gameplay_behav.obdat.actors[x][y],Actor))
{
var obj_left_bound:Float = S_gameplay_behav.obdat.actors[x][y].getX() + 256 - 256 * S_gameplay_behav.zoom; 
var obj_right_bound:Float = S_gameplay_behav.obdat.actors[x][y].getX() + 256 + 256 * S_gameplay_behav.zoom; 
var obj_upper_bound:Float = S_gameplay_behav.obdat.actors[x][y].getY() + 256 - 256 * S_gameplay_behav.zoom; 
var obj_lower_bound:Float = S_gameplay_behav.obdat.actors[x][y].getY() + 256 + 256 * S_gameplay_behav.zoom;

if(pl_right_bound > obj_left_bound && pl_left_bound < obj_right_bound && pl_lower_bound > obj_upper_bound && pl_upper_bound < obj_lower_bound)
{
this.diag_col = "collision detected";
var dx:Float = this.posx - S_gameplay_behav.obdat.actors[x][y].getX(); 
var dy:Float = this.posy - S_gameplay_behav.obdat.actors[x][y].getY();

if(dx < 0)
{
if(dy < 0) // upper left corner
{
if(dx < dy)
{
this.diag_col = "left side";
this.posx = Math.min(this.posx,obj_left_bound -  256 - (pl_dimension * 0.5));
}
else
{
this.diag_col = "upper side";
this.posy = Math.min(this.posy,obj_upper_bound -  256 - (pl_dimension * 0.5));
}
}
else // lower left corner, dx is negative, dy is positive. 
{
if(dx + dy < 0) // remains left side
{
this.diag_col = "left side";
this.posx = Math.min(this.posx,obj_left_bound-  256 - (pl_dimension * 0.5));
}
else
{
this.diag_col = "bottom side";
this.posy = Math.max(this.posy,obj_lower_bound - 256 + (pl_dimension * 0.5));
}
}
}
else
{
if(dy < 0) // uppper right, dx is positive, 
{
if(dx + dy < 0) // remains upper side
{
this.diag_col = "upper side";
this.posy = Math.min(this.posy,obj_upper_bound -  256 - (pl_dimension * 0.5));
}
else
{
this.diag_col = "rignt side";
this.posx = Math.max(this.posx,obj_right_bound - 256 + (pl_dimension * 0.5));
}
}
else
{
if(dx < dy)
{
this.diag_col = "bottom side";
this.posy = Math.max(this.posy,obj_lower_bound - 256 + (pl_dimension * 0.5));
}
else
{
this.diag_col = "right side";
this.posx = Math.max(this.posx,obj_right_bound - 256 + (pl_dimension * 0.5));
}
}
}
}
}
y = y+1; 
}
x = x+1; 
}
}

public function def():Void
{
var tt:TimedTask = new TimedTask(this.next_frame,20,false,null); 
if(this.moving)
{
this.old_posx = this.posx; this.old_posy = this.posy;
var dx:Float = move_x * Player_data.speed; // todo list. 
var dy:Float = move_y * Player_data.speed; 
this.posx += dx * S_gameplay_behav.zoom; this.posy += dy* S_gameplay_behav.zoom; 
this.col();
actor.setX(this.posx); actor.setY(this.posy);
Engine.cameraX = actor.getX() - Engine.screenWidth/2 + actor.cacheWidth/2 ; 
Engine.cameraY = actor.getY() - Engine.screenHeight/2 + actor.cacheHeight/2 ; 
S_gameplay_behav.disp_xcen  += (this.posx-this.old_posx) /512/ S_gameplay_behav.zoom; 
S_gameplay_behav.disp_ycen  += (this.posy-this.old_posy) /512/ S_gameplay_behav.zoom; 
}
Engine.engine.tasks.push(tt);
}
public function diag_drawing()
{
var dx:Float = actor.getX() - Engine.cameraX; 
var dy:Float = actor.getY() - Engine.cameraY; 

var pl_left_bound:Float = this.posx + 256 - (256 * 0.8 * S_gameplay_behav.zoom)- Engine.cameraX; 
var pl_right_bound:Float = this.posx + 256 + (256 * 0.8 * S_gameplay_behav.zoom)- Engine.cameraX; 
var pl_upper_bound:Float = this.posy + 256 - (256 * 0.8 * S_gameplay_behav.zoom)- Engine.cameraY; 
var pl_lower_bound:Float = this.posy + 256 + (256 * 0.8 * S_gameplay_behav.zoom)- Engine.cameraY; 

var rh_width = pl_right_bound - pl_left_bound;
var rh_height = pl_lower_bound - pl_upper_bound; 
this.diag_rh.draw(pl_left_bound,pl_upper_bound,rh_width,rh_height); 
this.diag_text.drawString("movex: "+ this.move_x, dx,dy);
this.diag_text.drawString("movey: "+ this.move_y, dx,dy+20);
this.diag_text.drawString("moving:" + this.moving,dx,dy + 40);
this.diag_text.drawString("col: " + this.diag_col,dx,dy + 60);
}
public function get_anim(a:Float):String 
{
var b:Int = Std.int(a); 
if(b == 0){return "neutral";}
return "neutral"; 
}

public function next_frame(a:TimedTask):Void{this.def();}
public function press_east(p:Bool,r:Bool,u0)
{
if(p)
{
this.moving = true; 
this.move_east = true; 
if(this.move_west)
{
if(this.move_north && !(this.move_south)){this.direction =0; this.move_x =0; this.move_y = -1;}
else if(!(this.move_north) && this.move_south){this.direction =180; this.move_x =0; this.move_y = 1;}
else {this.direction = 90;}
}
else 
{
if(this.move_north && !(this.move_south)){this.direction = 45;this.move_x =0.707; this.move_y = -0.707;}
else if(!(this.move_north) && this.move_south){this.direction = 135;this.move_x =0.707; this.move_y = 0.707;}
else {this.direction = 90; this.move_x = 1; this.move_y = 0;}
}
}
else if(r)
{
this.move_east = false; 
if(this.move_west)
{
if(this.move_north && !(this.move_south)){this.direction = 315;this.move_x =-0.707; this.move_y = -0.707;}
else if(!(this.move_north) && this.move_south){this.direction = 225;this.move_x =-0.707; this.move_y = 0.707;}
else {this.direction = 270; this.move_x = -1; this.move_y = 0;}
}
else
{
if(this.move_north && !(this.move_south)){this.direction = 0; this.move_x = 0; this.move_y = -1;}
else if(!(this.move_north) && this.move_south){this.direction = 180;this.move_x = 0; this.move_y = 1;}
else this.stop_player_movement();  
}
}
actor.tweenProps.angle.tween(actor.getAngleInDegrees(),this.direction,null,0);
}
public function press_north(p:Bool,r:Bool,u0) // showing that this is no typo
{
if(p)
{
this.moving = true; 
this.move_north = true; 
if(this.move_south)
{
if(this.move_east && !(this.move_west)){this.direction =90;this.move_x = 1; this.move_y = 0;}
else if(!(this.move_east) && this.move_west){this.direction =270;this.move_x = -1; this.move_y = 0;}
else {this.direction = 0;}  
}
else
{
if(this.move_east && !(this.move_west)){this.direction = 45;this.move_x =0.707; this.move_y = -0.707;}
else if(!(this.move_east) && this.move_west){this.direction = 315;this.move_x =-0.707; this.move_y = -0.707;}
else {this.direction = 0; this.move_x =0; this.move_y =-1;}  // direction work, but movement did not
}
}
else if(r)
{
this.move_north = false; 
if(this.move_south)
{
if(this.move_east && !(this.move_west)){this.direction = 135;this.move_x =0.707; this.move_y = 0.707;}
else if(!(this.move_east) && this.move_west){this.direction = 225;this.move_x =-0.707; this.move_y = 0.707;}
else {this.direction = 180;this.move_x = 0; this.move_y = 1;}
}
else
{
if(this.move_east && !(this.move_west)){this.direction = 90;this.move_x = 1; this.move_y = 0;}
else if(!(this.move_east) && this.move_west){this.direction = 270;this.move_x = -1; this.move_y = 0;}
else this.stop_player_movement();  
}
}
actor.tweenProps.angle.tween(actor.getAngleInDegrees(),this.direction,null,0);
}
public function press_west(p:Bool,r:Bool,u0)
{
if(p)
{
this.moving = true; 
this.move_west = true; 
if(this.move_east)
{
if(this.move_north && !(this.move_south)){this.direction = 0;this.move_x = 0; this.move_y = -1;}
else if(!(this.move_north) && this.move_south){this.direction = 180;this.move_x = 0; this.move_y = 1;}
else {this.direction = 270;}
}
else
{
if(this.move_north && !(this.move_south)){this.direction = 315;this.move_x =-0.707; this.move_y = -0.707;}
else if(!(this.move_north) && this.move_south){this.direction = 225;this.move_x =-0.707; this.move_y = 0.707;}
else {this.direction = 270;this.move_x = -1; this.move_y = 0;}
}
}
else if(r)
{
this.move_west = false; 
if(this.move_east)
{
if(this.move_north && !(this.move_south)){this.direction = 45;this.move_x =-0.707; this.move_y = 0.707;}
else if(!(this.move_north) && this.move_south){this.direction = 135;this.move_x =0.707; this.move_y = 0.707;}
else {this.direction = 90;this.move_x = 1; this.move_y = 0;}
}
else
{
if(this.move_north && !(this.move_south)){this.direction = 0;this.move_x = 0; this.move_y = -1;}
else if(!(this.move_north) && this.move_south){this.direction = 180;this.move_x = 0; this.move_y = 1;}
else this.stop_player_movement();  
}
}
actor.tweenProps.angle.tween(actor.getAngleInDegrees(),this.direction,null,0);
}
public function press_south(p:Bool,r:Bool,u0)
{
if(p)
{
this.moving = true; 
this.move_south = true; 
if(this.move_north)
{
if(this.move_east && !(this.move_west)){this.direction = 90;this.move_x = 1; this.move_y = 0;}
else if(!(this.move_east) && this.move_west){this.direction = 270;this.move_x = -1; this.move_y = 0;}
else {this.direction = 180;}
}
else
{
if(this.move_east && !(this.move_west)){this.direction = 135;this.move_x =0.707; this.move_y = 0.707;}
else if(!(this.move_east) && this.move_west){this.direction = 225;this.move_x =-0.707; this.move_y = 0.707;}
else {this.direction = 180;this.move_x = 0; this.move_y = 1;}
}
}
else if(r)
{
this.move_south = false; 
if(this.move_north)
{
if(this.move_east && !(this.move_west)){this.direction = 45;this.move_x =0.707; this.move_y = -0.707;}
else if(!(this.move_east) && this.move_west){this.direction = 315;this.move_x = -0.707; this.move_y = -0.707;}
else {this.direction = 0;this.move_x = 0; this.move_y = -1;}
}
else
{
if(this.move_east && !(this.move_west)){this.direction = 90;this.move_x = 1; this.move_y = 0;}
else if(!(this.move_east) && this.move_west){this.direction = 270;this.move_x = -1; this.move_y = 0;}
else this.stop_player_movement();
}
}
actor.tweenProps.angle.tween(actor.getAngleInDegrees(),this.direction,null,0);
}

public function set_zoom(){actor.growTo(S_gameplay_behav.zoom * 0.8,S_gameplay_behav.zoom*0.8,0,null);}
public function stop_player_movement(){this.moving = false; this.move_east = false; this.move_north = false; this.move_south = false; this.move_west = false;}


public function shift_nx(){this.posx -= Engine.sceneWidth/2;}
public function shift_ny(){this.posy -= Engine.sceneHeight/2;}
public function shift_px(){this.posx += Engine.sceneWidth/2;}
public function shift_py(){this.posy += Engine.sceneHeight/2;}
}

No comments:

Post a Comment