class EnemyLogic : ZilchComponent { [Property] var Speed : Real = 3.0; [Property] var BulletSpeed : Real; [Property] var Damage : Integer = 5; var ChaseTriggerDistance: Real = 2.0; var DistanceFromTarget: Real = 0.0; var TargetIsWithinRange: Boolean = false; var Cats: Real = 0.0; [Property] var NextShot: Real = 2.0; [Property] var BulletSpawnDistanceFromPlayer : Real = 1.0; [Property] var ShootDelay: Real = 1.0; var ChaseDirection2 : Real3= Real3(); [Property] var ChaseSpeed: Real = 1.0; [Dependency] var Transform : Transform; [Dependency] var Orientation : Orientation; var OriginRoot : Cog = null; function Initialize(init : CogInitializer) { Zero.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate); Zero.Connect(this.Owner, Events.CollisionPersisted, this.OnCollisionPersisted); } function OnLogicUpdate(event : UpdateEvent) { if(this.Space.FindObjectByName("Player") != null) { var player = this.Space.FindObjectByName("Player"); var playerPos = player.Transform.Translation; this.Owner.Orientation.LookAtPoint(playerPos); var myPos = this.Orientation.WorldForward; var movement = Real3(0.0, 0.0, 0.0); movement += (myPos * this.Speed); this.Owner.Transform.Translation += movement * event.Dt; //this.CalculateChaseDirectionAndDistance(); // Evaluate whether Ai should chase, shoot or pace this.TargetIsWithinRange = (this.DistanceFromTarget <= this.ChaseTriggerDistance); if (this.TargetIsWithinRange) { this.ShootAtTarget(event); } else { } } } function OnCollisionPersisted(collisionEvent : CollisionEvent) { var otherObject = collisionEvent.OtherObject; var otherDamageOnCollide = otherObject.DamageOnCollide; // Also get the root in the hierarchy. var otherRoot = otherObject.FindRoot(); // Don't damage the person we were created from. if(otherRoot == this.OriginRoot) { return; } if(otherRoot.Name == "Enemy") { return; } //If what we hit has a health component then damage it. var health = otherObject.Health; if(health != null) { var firstPoint = collisionEvent.FirstPoint; var damageEvent = DamageEvent(); damageEvent.Normal = firstPoint.WorldNormalTowardsOther; damageEvent.Damage = this.Damage; damageEvent.WorldPoint = firstPoint.WorldPoint; otherObject.DispatchEvent(Events.ApplyDamage, damageEvent); Console.WriteLine("Health at `otherObject.Health.CurrentHealth`%"); } } function ShootAtTarget(event: UpdateEvent) { this.Cats += event.Dt; // Don't shoot until the next shot timer is reached if (this.Cats > this.NextShot) { // Update next shot timer this.NextShot = this.Cats + this.ShootDelay; var direction = this.Owner.Orientation.WorldForward; direction = Math.Normalize(direction); var position = this.Owner.Transform.WorldTranslation + (direction * this.BulletSpawnDistanceFromPlayer); // Create a projectile originating from the enemy's position var projectileObject = this.Space.CreateAtPosition(Archetype.Find("EnemyBullet"), position); if (projectileObject != null) { projectileObject.RigidBody.Velocity = direction * this.BulletSpeed; // Shoot in the direction of the target (which is the same as the chase Direction) //projectileObject.Projectile.Direction = this.ChaseDirection2; // Make sure projectile moves faster than the enemy //projectileObject.Projectile.Speed = 15.0 + this.ChaseSpeed; } } } }