/* * Checks whatever the line of sight between two game entities is clear or not * precision describes how many iterations of collision checks are done per call * */ public static bool checkLineOfSight(GameEntity start, GameEntity end, int precision) { //create a vector2 with a position of an "imaginary bullet" - a bullet on its path from the entity //to the position of the player Vector2 imaginaryBulletPosition = start.Position; //divide the distance into 10 steps - not super precise,but enough for quick detection Vector2 iStep = (end.Position - start.Position) / (float)precision; //do the collision check for each step for (int i = 0; i < precision; i++) { if (Collisions.checkWallsCollisionsSimple(imaginaryBulletPosition, MapManager.Instance.currentMap)) { return(false); } imaginaryBulletPosition += iStep; } //return true if there was no collision along the way return(true); }
public override void Tick(float dt) { base.Tick(dt); GamePadData data = GamePad.GetData(0); float analogX = 0.0f; float analogY = 0.0f; //d-pad movement,emulating analog movement of the sticks if (Input2.GamePad0.Right.Down) { analogX = 1.0f; } else if (Input2.GamePad0.Left.Down) { analogX = -1.0f; } if (Input2.GamePad0.Up.Down) { analogY = -1.0f; } else if (Input2.GamePad0.Down.Down) { analogY = 1.0f; } //if the left stick is moving,then use values read from the stick if (data.AnalogLeftX > 0.2f || data.AnalogLeftX < -0.2f || data.AnalogLeftY > 0.2f || data.AnalogLeftY < -0.2f) { analogX = data.AnalogLeftX; analogY = data.AnalogLeftY; } //calculate the position /*if(analogX>0) * { * Position = new Vector2 (Position.X + 0.1f, Position.Y); * }else if(analogX<0) * { * Position = new Vector2 (Position.X - 0.1f, Position.Y); * } * * if(analogY>0) * { * Position = new Vector2 (Position.X + Position.Y- 0.1f); * }else if(analogY<0) * { * Position = new Vector2 (Position.X, Position.Y + 0.1f); * }*/ Vector2 proposedChange; if (analogX != 0.0f) { proposedChange = new Vector2(analogX / 10f, 0.0f); if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange)) { Position += proposedChange; } } if (analogY != 0.0f) { proposedChange = new Vector2(0.0f, -analogY / 10f); if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange)) { Position += proposedChange; } } //rotate according to the right analog stick, or if it's not moving, then according the the left stick // so basically if you are not pointing the player in any direction with the right stick he is going to point in the walking direction //or if both sticks are not moving,then use the analogX and analogY values(d-pad movement) // OR do autoaim if enabled if (data.AnalogRightX > 0.2f || data.AnalogRightX < -0.2f || data.AnalogRightY > 0.2f || data.AnalogRightY < -0.2f) { var angleInRadians = FMath.Atan2(-data.AnalogRightX, -data.AnalogRightY); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else if (!Game.autoAim && (data.AnalogLeftX > 0.2f || data.AnalogLeftX < -0.2f || data.AnalogLeftY > 0.2f || data.AnalogLeftY < -0.2f)) { var angleInRadians = FMath.Atan2(-data.AnalogLeftX, -data.AnalogLeftY); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else if (!Game.autoAim && (analogX != 0.0f || analogY != 0.0f)) { var angleInRadians = FMath.Atan2(-analogX, -analogY); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else if (Game.autoAim) { GameEntity e; if (Collisions.findNearestEnemy(Player.Instance, 8.0f, out e)) { Vector2 distance = (Player.Instance.Position - e.Position).Normalize(); var angleInRadians = FMath.Atan2(distance.X, -distance.Y); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else { var angleInRadians = FMath.Atan2(-analogX, -analogY); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } } }
public override void Tick(float dt) { //base.Tick(dt); if (Player.Instance == null) { } else { setCameraPosition(); } //check bullet delay if (Bullet.bulletDelay > 0) { Bullet.bulletDelay--; } //check buttons if (Input2.GamePad0.Cross.Down || Input2.GamePad0.R.Down) { if (Bullet.bulletDelay == 0 && Player.Instance.ammo > 0) { SoundSystem.Instance.Play("shot.wav"); Bullet bullet = new Bullet(); bulletList.Add(bullet); World.AddChild(bullet); Bullet.bulletDelay = 2; Player.Instance.ammo--; //update player's sprite Player.Instance.playerBodySprite.TileIndex1D = Player.Instance.animationFrame; Player.Instance.animationFrame = (Player.Instance.animationFrame + 1) % Player.Instance.playerBodySprite.TextureInfo.NumTiles.X; } } //check buttons if (Input2.GamePad0.Cross.Release || Input2.GamePad0.R.Release) { //update player's sprite Player.Instance.playerBodySprite.TileIndex1D = 0; } //check start button(for pause menu) if (Input2.GamePad0.Start.Press) { this.paused = true; SceneManager.Instance.pushScene(PauseScene.Instance); } // pinch-to-zoom //only check for multitouch points if device supports more than one touch point if (Input2.Touch.MaxTouch > 1) { //only proceed if the user is actually touching both points if (Input2.Touch.GetData(0)[0].Down && Input2.Touch.GetData(0)[1].Down) { //first touch point Vector2 touch1 = Director.Instance.CurrentScene.GetTouchPos(0); //second touch point Vector2 touch2 = Director.Instance.CurrentScene.GetTouchPos(1); //reset the distance measure if the screen has just been touched if (Input2.Touch.GetData(0)[0].Press || Input2.Touch.GetData(0)[1].Press) { multitouchDistance = touch1.Distance(touch2); } else { var newDistance = touch1.Distance(touch2); cameraHeight += (multitouchDistance - newDistance); this.Camera2D.SetViewFromHeightAndCenter(cameraHeight, Player.Instance.Position); } } } //check if the player has collected any ammo packs AmmoItem ammoItemToRemove; if (Collisions.checkAmmoPackCollisions(Player.Instance, ammoList, out ammoItemToRemove)) { Game.Instance.EffectsLayer.AddChild(new ammoMarker(ammoItemToRemove.Position)); SoundSystem.Instance.Play("ammoclip.wav"); World.RemoveChild(ammoItemToRemove, true); ammoList.Remove(ammoItemToRemove); Player.Instance.ammo = (int)FMath.Clamp((Player.Instance.ammo + 50), 100, 100); ammoItemToRemove.Die(); } if (Player.Instance.Health <= 0) { Background.RemoveAllChildren(true); Foreground.RemoveAllChildren(true); EffectsLayer.RemoveAllChildren(true); World.RemoveAllChildren(true); Interface.RemoveAllChildren(true); SceneManager.Instance.changeSceneTo(GameOverScene.Instance); //Sce.PlayStation.HighLevel.GameEngine2D.Scheduler.Instance.Unschedule(Scene, gameTick); //Sce.PlayStation.HighLevel.GameEngine2D.Scheduler.Instance.Schedule(Scene, gameoverTick, 0.0f, false); } }
public override void Tick(float dt) { Enemy tempEnemy; //check the collision with the walls if (Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref step)) { //play a sound SoundSystem.Instance.Play("wallhit2.wav"); float scale = 0.5f; //try a texture in place of the bullethole SpriteUV bulletHole = new SpriteUV(); bulletHole.TextureInfo = new TextureInfo(fireTexture); bulletHole.Scale = new Vector2(0.15f, 0.15f); bulletHole.Color = Colors.Black; bulletHole.CenterSprite(new Vector2(0.5f, 0.5f)); bulletHole.Position = Position + step; //remove the bulletHole after half a second bulletHole.ScheduleInterval((at) => bulletHole.Parent.RemoveChild(bulletHole, true), 0.5f, -1); //add the bullethole to the background Game.Instance.Background.AddChild(bulletHole); //remove this bullet this.Parent.RemoveChild(this, true); Game.Instance.bulletList.Remove(this); //die this.Die(); } else if (Collisions.checkEnemiesCollisions(this, Game.Instance.enemyList, step, out tempEnemy)) { //reduce the health of the enemy the bullet hit tempEnemy.health -= bulletDamage; float scale = 0.5f; //particles! //create and start the particle emmiter /*Particles fire_node= new Particles( 10 ); * ParticleSystem fire = fire_node.ParticleSystem; * * fire.TextureInfo = new TextureInfo( fireTexture ); * fire.Emit.Velocity = step; * fire.Emit.VelocityVar = new Vector2( 2.0f, 2.0f ); * fire.Emit.ForwardMomentum = 0.0f; * fire.Emit.AngularMomentun = 0.0f; * fire.Emit.LifeSpan = 0.5f; * fire.Emit.WaitTime = 0.0f; * float s = 0.0f; * fire_node.Position = Position+step; * fire.Emit.PositionVar = new Vector2(s,s); * fire.Emit.ColorStart = Colors.Red; * fire.Emit.ColorStartVar = new Vector4(0.0f,0.0f,0.0f,0.0f); * fire.Emit.ColorEnd = Colors.Red; * fire.Emit.ColorEndVar = new Vector4(0.2f,0.0f,0.0f,0.0f); * fire.Emit.ScaleStart = 0.3f * scale; * fire.Emit.ScaleStartRelVar = 0.2f; * fire.Emit.ScaleEnd = 0.6f * scale; * fire.Emit.ScaleEndRelVar = 0.2f; * fire.Simulation.Fade = 0.5f; * fire.Simulation.Gravity = 0.0f; * * fire.Emit.Transform = fire_node.GetWorldTransform(); * fire.Emit.TransformForVelocityEstimate = fire_node.GetWorldTransform(); * fire.RenderTransform = Director.Instance.CurrentScene.GetWorldTransform(); // most probably identity * * Director.Instance.CurrentScene.AddChild(fire_node); * * Director.Instance.CurrentScene.RegisterDisposeOnExit(fire); * * //remove the particle emmiter after 0.5 seconds * fire_node.ScheduleInterval((at) => Director.Instance.CurrentScene.RemoveChild(fire_node,true),0.5f,1); */ this.Parent.RemoveChild(this, true); Game.Instance.bulletList.Remove(this); this.Die(); } else { //no collisions with anything,move the bullet Position += step; } }
public BasicEnemy(Vector2 pos, TextureInfo tex) { Position = pos; sprite = new SpriteTile(); sprite.TextureInfo = tex; sprite.Position = pos; sprite.TileIndex2D = new Vector2i(0, 0); sprite.CenterSprite(new Vector2(0.4f, 0.5f)); sprite.Scale = new Vector2(2.0f, 2.0f); //set up the random vector randomMovement = Support.rand.NextVector2(-0.01f, 0.01f); //update the animation frames sprite.Schedule((dt) => { if (FrameCount % 2 == 0) { //if attacking,use all animation frames if (attacking) { //deal damage to the player if (FrameCount % damageDelay == 0) { Player.Instance.Health -= damage; } animationFrame = (animationFrame + 1) % 25; } else { //if not,then use first three animation frames animationFrame = (animationFrame + 1) % 4; } //assign the correct tileindex sprite.TileIndex1D = animationFrame; //if close to the player,then follow,otherwise move randomly if (Player.Instance.Position.Distance(sprite.Position) < 6.0f && Collisions.checkLineOfSight(this, Player.Instance)) { isMovingRandomly = false; step = (Player.Instance.Position - sprite.Position).Normalize() / 15.0f; //check if should be attacking if (Collisions.checkCollisionBetweenEntities(this, Player.Instance)) { //check if is not attacking already if (attacking == false) { attacking = true; //manualy skip the frames animationFrame = 4; } } else { //make sure that attacking is set to false attacking = false; } } else { //player is not within range, move randomly isMovingRandomly = true; step = randomMovement; } } //advance the position by the given Vector2 step sprite.Position += step; //only move when not attacking if (!attacking) { //collision detection on the X axis //create a vector 2 containing a proposed change to the position Vector2 proposedChange = new Vector2(step.X, 0.0f) * speedModifier; //temporary game entity to contain the entity the enemy might have collided with GameEntity tempEntity; //check wall collisions and then collisions with other enemies if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange) && !Collisions.efficientCollisionsCheck(this, proposedChange, out tempEntity)) { //no collision, so we can change the position Position += proposedChange / speedModifier; } else if (isMovingRandomly) { //collided with something,but if the enemy should be moving randomly, then let it move randomMovement.X = -randomMovement.X; } //collision detection on the Y axis proposedChange = new Vector2(0.0f, step.Y) * speedModifier; if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange) && !Collisions.efficientCollisionsCheck(this, proposedChange, out tempEntity)) { Position += proposedChange / speedModifier; } else if (isMovingRandomly) { randomMovement.Y = -randomMovement.Y; } //position changed, so we need to remove the entity from the QuadTree and add again //this is because the entity might be in the wrong place in the QuadTree and removing and re-adding is the only way to fix that Game.Instance.quadTree.removeEntity(this); Game.Instance.quadTree.insert(this); } //rotate the sprite to face the direction of walking var angleInRadians = -FMath.Atan2(step.X, step.Y); sprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); //correct for the fact that the sprite is rotated in the texture file sprite.Rotation = sprite.Rotation.Rotate(90.0f); }, -1); //create a shadow texture - temporarily disabled //SpriteUV shadow = new SpriteUV(new TextureInfo(Bullet.fireTexture)); //shadow.CenterSprite(new Vector2(0.5f,0.5f)); //shadow.Color = Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.SetAlpha(Colors.Black,0.5f); //calculate the bounds for the entire sprite bounds = new Bounds2(); sprite.GetlContentLocalBounds(ref bounds); bounds = new Bounds2(bounds.Min * 0.5f, bounds.Max * 0.5f); }