1. Tao Chen
  2. sha-qmark

Commits

Tao Chen  committed 0ca9823

basic bullet done

  • Participants
  • Parent commits 5e6fd37
  • Branches default

Comments (0)

Files changed (5)

File woot/Bullet.as

View file
  • Ignore whitespace
+package woot 
+{
+	import org.flixel.FlxSprite;
+	
+	/**
+	 * ...
+	 * @author lilo
+	 */
+	public class Bullet extends FlxSprite 
+	{
+		public function Bullet()
+		{
+			width = 24;
+			height = 24;
+			super( -100, -100);
+			fill(0xFF00FF00);
+			centerOffsets();
+			
+			// kill on start
+			kill();
+		}
+		
+		public function setup(type_:uint, dir:uint, speed:int, x_:Number, y_:Number) : void
+		{
+			// TODO use frame to set type
+			reset(x_, y_);
+			switch (dir) {
+				case 3:
+				case 6:
+				case 9:
+					velocity.x = speed;
+					break;
+				case 7:
+				case 4:
+				case 1:
+					velocity.x = -speed;
+					break;
+				default:
+					velocity.x = 0;
+					break;
+			}
+			
+			switch (dir) {
+				case 1:
+				case 2:
+				case 3:
+					velocity.y = speed;
+					break;
+				case 7:
+				case 8:
+				case 9:
+					velocity.y = -speed;
+					break;
+				default:
+					velocity.y = 0;
+					break;
+			}
+		}
+		
+		override public function update() : void
+		{
+			if (!onScreen())
+			{
+				kill();
+			}
+			
+			super.update();
+		}
+		
+	}
+
+}

File woot/BulletMan.as

View file
  • Ignore whitespace
+package woot 
+{	
+	import org.flixel.*;
+	
+	/**
+	 * ...
+	 * @author lilo
+	 */
+	public class BulletMan
+	{
+		static public var playerBullet : FlxGroup;
+		static public var enemyBullet : FlxGroup;
+		
+		static public function static_init() : void
+		{
+			playerBullet = new FlxGroup();
+			enemyBullet = new FlxGroup();
+		}
+		
+		static public function AddPlayerBullet(dir:uint, player:Player) :void
+		{
+			var obj : FlxBasic = playerBullet.getFirstDead();
+			var bullet : Bullet;
+			if (!obj)
+			{
+				bullet = new Bullet();
+				playerBullet.add(bullet);
+			}
+			else
+			{
+				bullet = obj as Bullet;
+			}
+			
+			bullet.setup(0, dir, 200, player.x, player.y);
+		}
+		
+		
+	}
+
+}

File woot/Comm.as

View file
  • Ignore whitespace
 	public class Comm 
 	{
 		static public var GRAVITY : int = 600;
+		
+		static public function rad2deg(r : Number) : Number
+		{
+			return r * 180 / Math.PI;
+		}
+		
+		static public function deg2dir(d : Number) : uint
+		{
+			// x to right, y to DOWN!
+			var dir : uint  = 3;
+			if (d <= 180 && d > 180 - 22.5) {
+				d = 4;
+			} else if (d <= 180 - 22.5 && d > 90 + 22.5) {
+				d = 1;
+			} else if (d <= 90 + 22.5 && d > 90 - 22.5) {
+				d = 2;
+			} else if (d <= 90 - 22.5 && d > 22.5) {
+				d = 3;
+			} else if (d <= 22.5 && d > -22.5) {
+				d = 6
+			} else if (d <= -22.5 && d > -90 + 22.5) {
+				d = 9;
+			} else if (d <= -90 + 22.5 && d > -90 - 22.5 ) {
+				d = 8;
+			} else if (d <= -90 - 22.5 && d > -180 + 22.5) {
+				d = 7
+			} else {
+				d = 4;
+			}
+			
+			return d;
+		}
 	}
 
 }

File woot/PlayState.as

View file
  • Ignore whitespace
 		override public function create():void
 		{
 			FlxG.bgColor = 0xFFFFFFFF;
+			BulletMan.static_init();
+			
 			map = new Level();
 			player1 = new Player(1);
 			
 			add(map);
 			add(player1);
+			add(BulletMan.playerBullet);
+			add(BulletMan.enemyBullet);
+			
+			
+			// DEBUG
+			FlxG.watch(BulletMan.playerBullet, "length");
 		}
 		
 		override public function update():void

File woot/Player.as

View file
  • Ignore whitespace
 				{
 					velocity.y = -JUMP;
 				}
+				
+				if (FlxG.mouse.justPressed())
+				{
+					var dir : uint;
+					var dx : Number = FlxG.mouse.screenX - x;
+					var dy : Number = FlxG.mouse.screenY - y;
+					var deg : Number = Comm.rad2deg(Math.atan2(dy, dx));
+					FlxG.log(deg);
+					FlxG.log(Comm.deg2dir(deg));
+					BulletMan.AddPlayerBullet(Comm.deg2dir(deg), this);
+				}
 			}
 			
 			
 			if (!solid && y - _fallingStart > 32)
 			{
-				FlxG.log("STOPING");
 				solid = true;
 				allowCollisions = FlxObject.DOWN;
 			}