/// <summary> /// This entity is colliding into another. This entity is the one doing the colliding. /// </summary> /// <param name="collidingEntity"></param> public void Collision(GameEntity collidingEntity) { Vector collisionVector = new Vector(this.position.x - collidingEntity.GetPosition().x + this.speed.X, this.position.x - collidingEntity.GetPosition().x + this.speed.Y); string firingGuy = ""; if(this.GetTypeName() == "Projectile") { //the projectile should not hit the entity that fired it Projectile projEnt = this.As<Projectile>(); if (collidingEntity == projEnt.GetFiringEntity()) { return; } firingGuy = projEnt.GetFiringEntity().GetName(); } //perform custom collision events first if (this.collisionEvent != null) { this.collisionEvent.Invoke(collidingEntity); } if (collidingEntity.collisionEvent != null) { collidingEntity.collisionEvent.Invoke(collidingEntity); } // Determine where the collision happens Point collisionPosition = new Point(0, 0); if (Math.Abs(this.speed.X) > Math.Abs(collidingEntity.speed.X)) { collisionPosition.x = (Math.Abs(collidingEntity.speed.X) / Math.Abs(this.speed.X)) * Math.Abs(this.position.x - collidingEntity.position.x); } else { collisionPosition.x = (Math.Abs(this.speed.X) / Math.Abs(collidingEntity.speed.X)) * Math.Abs(collidingEntity.position.x - this.position.x); } // If the colliding entity is neither above nor below, check X collision. if (!collidingEntity.GetArea().Above(this.GetArea()) && !collidingEntity.GetArea().Below(this.GetArea())) { if ((this.position.x + this.speed.X > collidingEntity.position.x + collidingEntity.speed.X && this.position.x + this.speed.X < collidingEntity.position.x + collidingEntity.speed.X + collidingEntity.Width) || (this.position.x + this.speed.X < collidingEntity.position.x + collidingEntity.speed.X && this.position.x + this.speed.X + this.Width > collidingEntity.position.x + collidingEntity.speed.X)) { //newSpeed.x = 0; //colNewSpeed.x = 0; } } // If colliding entity is neither to the left nor the right, check Y collision if (!(collidingEntity.GetArea().Right < this.GetArea().Left) && !(collidingEntity.GetArea().Left > this.GetArea().Right)) { // If colliding from above... // "Minimum" speed is entity position minus colliding entity top. if (this.position.y + this.speed.Y < collidingEntity.position.y + collidingEntity.speed.Y && this.position.y + this.speed.Y + this.Height > collidingEntity.position.y + collidingEntity.speed.Y) { this.SetPosition(this.GetPosition().x, collidingEntity.GetArea().Top); } // If colliding from below, // "Maximum" speed is colliding entity top minus entity bottom if (this.position.y + this.speed.Y > collidingEntity.position.y + collidingEntity.speed.Y && this.position.y + this.speed.Y < collidingEntity.position.y + collidingEntity.speed.Y + collidingEntity.Height) { this.SetPosition(this.GetPosition().x, collidingEntity.GetArea().Bottom - this.GetArea().height); } } // Collision means speed = 0; this.SetSpeed(new Vector(0, 0)); collidingEntity.SetSpeed(new Vector(0, 0)); // Colliding with a tile. if (collidingEntity.GetTypeName() == "Tile") { // Debug.log(collisionVector.ToString()); } // Well, we don't need to deal damage from a terrain collider. if (this.GetTypeName() == "TerrainCollider") { return; } int damage = 10; //deal damage to the other entity if (collidingEntity.GetTypeName() == "LivingGameEntity") { LivingGameEntity livingTarget = collidingEntity.As<LivingGameEntity>(); /* Debug.log(this.GetName() + " " + this.GetId() + " at " + this.GetPosition().x + ", " + this.GetPosition().y + " dealing " + damage + " damage to " + livingTarget.GetName() + " (" + livingTarget.GetId() + ") at " + livingTarget.GetPosition().x + ", " + livingTarget.GetPosition().y + ". IsProjectile: " + isProj.ToString() + ", firingGuy is " + firingGuy + ", This type is " + this.GetType().Name.ToString()); */ Debug.log("Damaging " + livingTarget.GetName() + " by " + damage); livingTarget.Damage(damage); } //collidingEntity.dam }
public void AddGameEntity(GameEntity gameEntityToAdd) { if (gameEntityToAdd == null) return; if (gameEntityToAdd.GetTypeName() == "LivingGameEntity" || gameEntityToAdd.GetTypeName() == "Projectile") // || GameEntityToAdd.GetTypeName() == "Tile") { //LivingGameEntity entityToAdd = gameEntityToAdd.As<LivingGameEntity>(); LivingGameEntity entityToAdd = (LivingGameEntity) gameEntityToAdd; //this.livingEntities.Add(gameEntityToAdd.As<LivingGameEntity>()); if (this.livingEntities.Contains(entityToAdd)) return; this.livingEntities.Add(entityToAdd); } else if (gameEntityToAdd.GetTypeName() == "TerrainCollider") { //Debug.log("Here. This. Do not want."); if (this.terrainColliders.Contains(gameEntityToAdd.As<TerrainCollider>())) return; terrainColliders.Add((TerrainCollider)gameEntityToAdd); } else { if (this.stageEntities.Contains(gameEntityToAdd)) return; stageEntities.Add(gameEntityToAdd); } gameEntityToAdd.SetParentStage(this); }
public void RemoveGameEntity(GameEntity gameEntityToRemove) { if (gameEntityToRemove.GetTypeName() == "LightSource") { LightSource lightToRemove = gameEntityToRemove.As<LightSource>(); if (this.stageLights.Contains(lightToRemove)) { this.stageLights.Remove(lightToRemove); } } else if (gameEntityToRemove .GetTypeName() == "Tile") { Tile tileToRemove = gameEntityToRemove.As<Tile>(); if (this.stageTiles.Contains(tileToRemove)) { this.stageTiles.Remove(tileToRemove); } } else if (gameEntityToRemove .GetTypeName() == "LivingGameEntity" || gameEntityToRemove .GetTypeName() == "Projectile") { LivingGameEntity livingEntityToRemove = gameEntityToRemove.As<LivingGameEntity>(); if (this.livingEntities.Contains(livingEntityToRemove)) { this.livingEntities.Remove(livingEntityToRemove); } else { Debug.log("Warning: Living entity list does not contain " + livingEntityToRemove.GetId() + " ( " + livingEntityToRemove.GetName() + " )"); } } else { if (this.stageEntities.Contains(gameEntityToRemove)) { this.stageEntities.Remove(gameEntityToRemove); } } }
//!Need to add better support for custom styles public void RenderGameEntity(GameEntity gent, View view) { HtmlElement gentlement = null; if (elementsByGameObjectId.ContainsKey(gent.GetId())) { gentlement = elementsByGameObjectId[gent.GetId()].As<HtmlElement>(); } //if it's not rendered, render it if (gentlement == null) { gentlement = document.createElement("div").As<HtmlElement>(); gentlement.id = gent.GetId(); //later, we may want to add iterating through base types to get their type name and add it... this.AddClass(gentlement, "Entity"); this.AddClass(gentlement, gent.GetType().Name); document.getElementById("gameBoard").appendChild(gentlement); foreach (string style in gent.GetCustomStyles()) { this.AddClass(gentlement, style); } if (gent.GetSprite() != null && gent.GetSprite().ScaleToSize == true) { gentlement.style.backgroundSize = gent.GetSprite().Size.width + "px " + gent.GetSprite().Size.height + "px "; } elementsByGameObjectId[gent.GetId()] = gentlement; } if (gent.GetTypeName() == "Tile") { tilesRendrin++; Color tileCol = gent.As<Tile>().GetLightLevel(); gentlement.style.backgroundColor = "rgb(" + tileCol.red + ", " + tileCol.green + ", " + tileCol.blue + ");"; if (gent.GetSize().width != Stage.CurrentStage.GetTileSize().width || gent.GetSize().height != Stage.CurrentStage.GetTileSize().height) { gentlement.style.width = Stage.CurrentStage.GetTileSize().width + "px"; gentlement.style.height = Stage.CurrentStage.GetTileSize().height + "px"; } } if(this.gameEntitiesToUpdate.Contains(gent.GetId())) { //reposition the element based on game position gentlement.style.left = (gent.GetPosition().x * Stage.CurrentStage.GetTileSize().width) + "px"; gentlement.style.top = ((Stage.CurrentStage.Height - gent.GetPosition().y - gent.Height) * Stage.CurrentStage.GetTileSize().height) + "px"; gentlement.style.opacity = gent.GetOpacity().ToString(); this.gameEntitiesToUpdate.Remove(gent.GetId()); } //if the entity doesn't have a sprite, we're done with all rendering for now if (gent.GetSprite() == null) { gentlement.style.border = "1px dashed blue"; return; } //call the sprite's animate function //it will return the id of the frame that the sprite and its animation are currently on string frameId = gent.GetSprite().Animate(); Debug.Watch("Animation count", gent.GetSprite().GetCurrentAnimation().animationCount.ToString(), true); //if that result is different from the value we have stored as this GameEntity's current display frame, if (frameId != gent.GetSprite().CurrentRenderFrame) { //update this thing's CSS to the new frame //jQueryObject GameEntityDiv = jQuery.FromElement(GameEntityDiv); this.RemoveClass(gentlement, gent.GetSprite().CurrentRenderFrame); this.AddClass(gentlement, frameId); derp = gentlement.className; //and update our current frameid gent.GetSprite().CurrentRenderFrame = frameId; gentlement.style.width = gent.GetSprite().Size.width + "px"; gentlement.style.height = gent.GetSprite().Size.height + "px"; } }