Tile Engine: The Bullet

more moving sprites

The Bullet is the most simple of the sprites. Once it's placed on stage, it can only move in one direction and it is destroyed upon collision with the maze.

It inherits all the methods from the GameSprite class and it needs very few alterations. The main one being:


override public function checkMazeCollision ():void {
	if (reachedTile()) {
		_trashme = true;
	}
}

Which ensures that, as I said, the bullet is removed upon collision with the maze.

Then the Player and Ai versions of this class, simply changes the color of the sprite.

The BasicBullet Class

package tiletank.bullet {
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import tiletank.*;
	import tiletank.tank.BasicTank;
	import tiletank.dynamic.GameSprite;
	
	/**
	* Basic class for the Bullets
	*/
	public class BasicBullet extends GameSprite {
		
		protected var _tank:BasicTank;
		protected var _dir:String;
		protected var _stageBox:Rectangle;
		
		function BasicBullet (tank:BasicTank) {
			_tank = tank;
			_dir = tank.getDirection();
			_speed = 6;
			position();
			
		}
		
		public function get tank ():BasicTank {
			return _tank;
		}
		public function initCollision ():Boolean {
			return reachedTile();
		}
		
		override public function update ():void {
			if (_trashme) return;
			
			if (_dir == GameConstants.UP) _vy = -_speed;
			if (_dir == GameConstants.DOWN) _vy = _speed;
			if (_dir == GameConstants.LEFT) _vx = -_speed;
			if (_dir == GameConstants.RIGHT) _vx = _speed;
			_nextX = _nowX + _vx;
			_nextY = _nowY + _vy;
			
			
			if (stage) {
			if (_nextX > stage.stageWidth) _trashme = true;
			if (_nextX < 0) _trashme = true;
			if (_nextY > stage.stageHeight) _trashme = true;
			if (_nextY < 0) _trashme = true;
			}
			
		}
		
		override public function destroy ():void {
			if (stage) _gameStage.removeBullet(this);
		}
		/**
		* Position Bullet according to the direction the tank is pointing
		*/
		protected function position ():void {
			switch (_dir) {
				case GameConstants.UP:
					rotation = -90;
					x = _nowX = _tank.nowX;
					y = _nowY = _tank.nowY - _tank.height/2;
				break;
				case GameConstants.DOWN:
					rotation = 90;
					x = _nowX = _tank.nowX;
					y = _nowY = _tank.nowY + _tank.height/2;
				break;
				case GameConstants.LEFT:
					scaleX = -1;
					x = _nowX = _tank.nowX - _tank.width/2;
					y = _nowY = _tank.nowY;
				break;
				case GameConstants.RIGHT:
					x = _nowX = _tank.nowX + _tank.width/2;
					y = _nowY = _tank.nowY;
				break;
			}
		}
		/**
		* If bullet collides with MAZE, bullet is removed
		*/
		override public function checkMazeCollision ():void {
			if (reachedTile()) {
				_trashme = true;
			}
		}
		
		override protected function initMe (event:Event):void {
			super.initMe(event);
			_stageBox = _gameStage.gameBoundaries;
			_manager.addBody (this);
			
		}
		
		override protected function removeMe (event:Event):void {
			super.removeMe(event);
			_stageElements.removeElement(this);
		}
	}
}

The PlayerBullet class

package tiletank.bullet {
	
	import tiletank.tank.BasicTank;
	import tiletank.GameUtils;
	import tiletank.GameConstants;
	
	
	public class PlayerBullet extends BasicBullet {
		
		function PlayerBullet (tank:BasicTank) {
			super(tank);
			GameUtils.setColor(this, GameConstants.PLAYER_COLOR);
			
		}
	}
}

The AiBullet class

package tiletank.bullet {
	
	import tiletank.GameConstants;
	import tiletank.tank.BasicTank;
	import tiletank.tank.AiBrain;
	import tiletank.GameUtils;
	
	
	public class AiBullet extends BasicBullet {
		
		function AiBullet (tank:BasicTank) {
			super(tank);
			GameUtils.setColor(this, AiBrain.aiColor);
		}
		
	}
}

The Boom class (animation for explosion)


package tiletank.bullet {
	
	import flash.display.MovieClip;
	import flash.geom.ColorTransform;
	
	import tiletank.GameUtils;
	import tiletank.GameConstants;
	import tiletank.tank.AiBrain;
	
	public class Boom extends MovieClip {
		
		function Boom (type:String) {
			if (type == GameConstants.AI) {
				GameUtils.setColor (this, AiBrain.aiColor);
			} else {
				GameUtils.setColor (this, GameConstants.PLAYER_COLOR);
			}
		}

	}
}