All pastes #2133283 Raw Edit

Untitled

public text v1 · immutable
#2133283 ·published 2012-03-28 15:39 UTC
rendered paste body
package classes.weapons 
{
	import flash.geom.Point;
	import flash.display.*;
	import flash.events.*;
	import classes.data.*;
	import classes.objects.*;
	import classes.core.Physics;
	import classes.utils.*;
	import flash.utils.*;
	import classes.weapons.animsets.*;

	public class MeleeWeapon extends Weapon
	{
		public var id:int = 200;
		public var weaponOffset:Point = new Point();//offset from weapon holder, in pixels
		public var firingPointOffset:Point = new Point();//location of tip of barrel, as offset from origin of weapon, in pixels
		public var weaponSize:Point = new Point();
		public var classname:String;
		public var graphicname:String;
		public var verboseName:String;
		
		public var mass:Number;
		public var baseDamage:Number;
		public var cleaveDecay:Number;
		public var damageType:Number;
		public var force:Number;
		public var armourPen:Number;
		public var critChance:Number;
		public var critMultiplier:Number;
		public var attackDelay:Number;
		public var reach:Number;
		public var minReach:Number;
		public var swingSpeed:Number;
		public var animset:*;
		
		public var target:Point = new Point();
		public var actorData:Array = new Array();
		public var attackState:int = 0;//0 = ready. 1 = during attack, before strike. 2 = during attack, after strike.. 3 = attack finished, cooling down. then goes back to 0
		public var cooldownTime:Number = 0;//minimum time between attacks. Starts conting from the timeof impact.
		var timepassed:Number;//time since start of the animation. Used to coordinate events
		public var animParameters:Array;//holds parameters about the attack we're currently doing.

		public static var ID:int = 0;
		public static var CENTREANGLE:int = 1;//Angle of a line pointing down the centre of attack cone.
		public static var ARCLENGTH:int = 2;//Total angle of the melee arc, centred on the centreangle
		public static var FRAME:int = 3;//The frame of the character that the animation is on./**/
		public static var ANIMLENGTH:int = 4;//Time in seconds before another animation can be used
		public static var IMPACTPOINT:int = 5;//Time into the animation that the melee arc hits, or that bullet fires. if -1, there will be no event
		public static var IMPACTDURATION:int = 6;//lifespan of the melee arc, in seconds. 
		public static var DAMAGEMULT:int = 7;
		public static var STATES:int = 8;//Moving, Sprinting, Crouching, Onground
		public static var WEAPONHIDE:int = 9;//boolean. if true, we hide the melee weapon during this attack


		public function MeleeWeapon() 
		{
			// constructor code
			//addEventListener(Event.ADDED_TO_STAGE, beginClass);
			addEventListener(Event.ENTER_FRAME, cooldown);
			addEventListener(CustomEvent.CONTROL_ATTACK, attackHandler);
			Twohanded;
		}
		
		override public function attackHandler(e:CustomEvent)
		{
			if (ready && weaponFunctionState == true && attackState == 0)
			{
				startAttack();
			}
		}
		
		function startCooling()
		{
			trace("Melee weapon starting cooling: "+attackDelay);
			ready = false;
			cooldownTime = attackDelay;
			addEventListener(Event.ENTER_FRAME, cooldown);
		}
		
		function cooldown(e:Event)
		{
			cooldownTime -= globals.frametime;
			if (cooldownTime <= 0.0)
			{
				ready = true;
				cooldownTime = 0;
				removeEventListener(Event.ENTER_FRAME, cooldown);
			}
		}

		/*public function beginClass(e:Event)
		{
			
		}*/
		
		override public function setup()
		{
			trace("Setup beginning: "+meleeWeaponData);
			var element:int = id - 200;
			trace("Meleesetup element: "+element);
			mass = meleeWeaponData.data[element][meleeWeaponData.WEAPONMASS];
			weaponOffset = meleeWeaponData.data[element][meleeWeaponData.GRAPHICOFFSET];
			weaponSize = meleeWeaponData.data[element][meleeWeaponData.GRAPHICSIZE];
			baseDamage = meleeWeaponData.data[element][meleeWeaponData.DAMAGE];
			cleaveDecay = meleeWeaponData.data[element][meleeWeaponData.CLEAVEDAMAGEDECAY];
			damageType = meleeWeaponData.data[element][meleeWeaponData.DAMAGETYPE];
			force = meleeWeaponData.data[element][meleeWeaponData.FORCE];
			armourPen = meleeWeaponData.data[element][meleeWeaponData.ARMOURPENETRATION];
			critChance = meleeWeaponData.data[element][meleeWeaponData.CRITICALCHANCE];
			critMultiplier = meleeWeaponData.data[element][meleeWeaponData.CRITICALMULTIPLIER];
			attackDelay = meleeWeaponData.data[element][meleeWeaponData.ATTACKDELAY];
			reach = meleeWeaponData.data[element][meleeWeaponData.REACH];
			minReach = meleeWeaponData.data[element][meleeWeaponData.MINREACH];
			swingSpeed = meleeWeaponData.data[element][meleeWeaponData.SWINGSPEED];
			weaponOffset = meleeWeaponData.data[element][meleeWeaponData.GRAPHICOFFSET];
			weaponSize = meleeWeaponData.data[element][meleeWeaponData.GRAPHICSIZE];
			trace("MELEE SETUP: Main stats block done");
			//-------------
			 	var animsetName:String = meleeWeaponData.data[element][meleeWeaponData.ANIMSET];//use this string to instantiate an animset instance
				animsetName = "classes.weapons.animsets."+animsetName;
				trace("Running getdefinition "+animsetName);
				var symbolClass:Class = getDefinitionByName(animsetName) as Class;
				trace("Getdefinition successful: "+symbolClass);
				animset = new symbolClass(this);
				//animset = new Twohanded(this);
				
		}
		
		public function setTarget()
		{
			trace("WEAPON SETTING TARGET: "+actorReference+"   "+actorReference.targetPos);
			target.x = actorReference.targetPos.x;
			target.y = actorReference.targetPos.y;
			trace("WEAPON SETTING TARGET: "+actorReference+"   "+actorReference.targetPos);
			//target = _root.camera.screenToLevel(target);
		}
		
		function gatherActorData()
		{
			//we need to get four things:
			//1 moving: Whether the actor has any self-propelled locomotion at all, including air control. (from physics)
			//2 Whether the actor is sprinting. (from actor)
			//3 Whether the actor is crouching (from actor)
			//4 whether the actor is standing on the ground (from physics)
			var moving:int = 0;
			var sprinting:int = 0;
			var crouching:int = 0;
			var onground:int = 0;
			
			var temp:Array = physics.getLocomotion(actorReference.physicsID);
			if (temp.length != 0)moving = 1;
			
			if (actorReference.motionType == 3)sprinting = 1;
			
			if (actorReference.crouching == true)crouching = 0;
			
			var temp2:Boolean = physics.checkProperty(actorReference.physicsID, physics.ONGROUND);
			onground = boolToInt(temp2);
			
			actorData = [moving, sprinting, crouching, onground];
			
		}
		
		function getAttackAnim()
		{
			animParameters = animset.getAnim([target,actorData]);
		}
		
		function startAnim()
		{
			actorReference.playAnim(animParameters[FRAME]);//anim number here
			actorReference.canAttack = false;//prevents shooting or additional melee attacks while this is running.
			actorReference.animLock = true;//Prevents state animations from playing. standing, walking, running, etc.
			actorReference.animRunning = true;//specifically prevents the idle animation. will be left true even when this attack ends.
		}
		
		function boolToInt(input:Boolean):int
		{
			if (input == true)return 1;
			else return 0;
		}
		
		function startAttack():void
		{
			attackState = 1;
			gatherActorData();
			getAttackAnim();
			startAnim();
			addEventListener(Event.ENTER_FRAME, timer);
			/*
				Once we've gotten the anim?
				1. Start it immediately. Set a variable on the actorreference to say that it is now locked into an animation. This will prevent other animation events, like walking, standing, etc.
				2. Start a per frame event, to count time for events in the attack.
					2a. Each frame we will add the current frametime to a total "time since start of attack"
				3. We set the weapon into a state of being busy, where it cannot be told to do another attack.
				4. Wait for the timer to tick up until one of the major events occurs
				5. When the strike time comes, place the meleeArc. set a variable indicating it has been placed to ensure we don't do it twice. 
					5a. Pass to the meleearc, it's lifespan
				6. Wait for the release event, to indicate that the mandatory part of the attack is completed. 
					6a. When the event comes, unset the busy variable so that we're open to new attacks.
					6b. Unset the locked variable on the actor reference, indicating that it can now do other animations if it desires.
				7. Note that at this point, an animation may still be running. the sort of "attack wind down". this part of the animation is optional, and it will play either tunil the animation finishes, or until some other animation plays over it.
					7a. When the animation finishes, it will set a variable on the actor reference, indicating that no animation is now running. The actor reference will be checking this every frame, and will take a changing of that variable to false, as a symbol to reassess the current animation.
						7a.a. This will result in the actor activating a standing idle animation, or similar.
					
				//what about stopping the animation ? we should have a stop function in the last frame of the anim, to stop it, and to set a variable on the parent indicating it has stopped.
				
				In the actor object, we should have two variables. One for indicating whether or not an animation is running, and one for if an animation is locked in or not.
			*/
			timepassed = 0;
		}
		
		function timer(e:Event)
		{
			timepassed += globals.frametime;
			if (attackState == 1)
			{
				//before strike
				if (timepassed >= animParameters[IMPACTPOINT])
				{
					//do melee arc now
					trace("Melee strike time is now");
				}
			}
			else if (attackState == 2)
			{
				cooldownTime += globals.frametime;
				if (cooldownTime >= attackDelay)
				{
					finishAttack();
				}
			}
		}
		
		function finishAttack()
		{
			//set us ready to recieve another attack. Set the actor ready to make one. stop any timers/events/listeners. reinitialise some variables.
		}
	}
}