/// <summary> /// Rotates object by radians /// </summary> /// <param name="radians"></param> public void Rotate(float radians) { localTransform.RotateZ(radians); UpdateTransform(); }
/// <summary> /// moves the tank and performs actions based on player location and randomised numbers /// </summary> /// <param name="deltaTime"></param> public override void OnUpdate(float dT) { //find angle from the turret to the player float turretAngle = (float)Math.Atan2(turret.Position.y - Game.player.Position.y, turret.Position.x - Game.player.Position.x); //getParent angle in radians relative to world space float parentAngle = GetRotate(); //set the turret's rotation to the turretAngle - parentAngle to compensate for hierachy. Also flip it cuz it ends up backwards otherwise turret.SetRotate(turretAngle + (float)Math.PI - parentAngle); //if we are near the middle of the screen if (collider.Overlaps(Game.arenaBox)) { if (shootTimer > 0) { //keep waiting to shoot shootTimer--; } else { //ready to shoot Shoot(); } if (moveTimer > 0) { //keep waiting to change direction moveTimer--; //keep moving to in the same direction Move(dT); } else { //randomise movement directions move = Game.Random.Next(0, 2); turn = Game.Random.Next(0, 2); //reset waiting period moveTimer = Game.Random.Next(30, 100); } } //if we are leaving the screen else { //create new temperary matrix3 Matrix3 angleToCenter = new Matrix3(); //rotate in 180 degrees angleToCenter.SetRotateZ((float)Math.PI); //get angle we need to go to angleToCenter.RotateZ((float)Math.Atan2(Position.y - Game.arenaBox.Center.y, Position.x - Game.arenaBox.Center.x)); //determine if we need to turn left or right if (angleToCenter.GetRotateZ() < GetRotate()) { //turn left quickly Rotate(-rotationSpeed * 3); } else { //turn right quickly Rotate(rotationSpeed * 3); } //move forward Vector3 facing = new Vector3(localTransform.m1, localTransform.m2, 1) * dT * 100; Translate(facing * speed); } //if we are completely outside of the map if (!collider.Overlaps(Game.totalMapBox)) { //flip 180 to point at home Rotate(3.14f); //move forwards quickly Vector3 facing = new Vector3(localTransform.m1, localTransform.m2, 1) * dT * 100; Translate(facing * speed * 4); } //update tank collider collider.Fit(cornersGlobalPosition); //update turret collider turret.collider.Fit(turret.cornersGlobalPosition); }