/// <summary> /// corrects the heading for a collision /// </summary> /// <returns></returns> private BehaviorReturnCode correctHeadingForCollision() { MapCollidable collidable = (MapCollidable)w_ColidableMapper.get(w_ThisEntity); Heading heading = (Heading)w_HeadingMapper.get(w_ThisEntity); Vector2 dir = collidable.ResponseVector; dir.Normalize(); heading.setHeading(dir); return(BehaviorReturnCode.Success); }
protected override void process(Entity entity) { //get colidable entity's position Position pos = (Position)m_PositionMapper.get(entity); ViewPort camView = (ViewPort)m_ViewPortMapper.get(m_Camera); MapCollidable mapCollide = (MapCollidable)m_MapCollidableMapper.get(entity); //reset collision detection mapCollide.Collided = false; //get the coliding entities position Vector2 colPos = pos.Pos; //create a polygon based at this entity's current position, using standard tileSize for height and width Polygon colPoly = createSquarePolygon(colPos, m_TileSize, m_TileSize); //set the center of the screen (used later for creating the the tile's polygon in "createPolysFromTiles") m_Center = camView.getDimensions() / 2; //find colliding tile polygons by finding a list of tiles that colide with the coliding entity's polygon List <Polygon> polys = createPolysFromTiles(findColidingTiles(colPoly)); //if no poly's were found return now if (polys == null) { return; } //collided mapCollide.Collided = true; Vector2 response; //for each tile polygon that we have collided with, find the heading correction vector //and update the position with it. The sum of the corrections, will resolve our position //to all collisions for (int i = 0; i < polys.Count; i++) { response = correctHeading(colPoly, polys[i]); mapCollide.ResponseVector += response; colPos += response; } //set the corrected position pos.Pos = colPos; }
/// <summary> /// set heading away from target /// </summary> /// <returns></returns> private BehaviorReturnCode headingAwayTarget() { Heading heading = (Heading)w_HeadingMapper.get(w_ThisEntity); Position position = (Position)w_PositionMapper.get(w_ThisEntity); Position tPosition = (Position)w_PositionMapper.get(w_Target); MapCollidable collide = (MapCollidable)w_ColidableMapper.get(w_ThisEntity); Vector2 dir = (tPosition.Pos) - (position.Pos); if (collide.Collided) { dir = Vector2.Negate(collide.ResponseVector);//VectorHelper.rotateVector(, (float)w_Random.NextDouble()); } dir.Normalize(); heading.setHeading(Vector2.Negate(dir)); return(BehaviorReturnCode.Success); }
/// <summary> /// has the entity collided /// </summary> /// <returns>returns true if it did</returns> private bool hasCollided() { MapCollidable collidable = (MapCollidable)w_ColidableMapper.get(w_ThisEntity); return(collidable.Collided); }
protected override void process(Entity entity) { Projectile projectile = (Projectile)p_ProjectileMapper.get(entity); projectile.ElapsedTime += ecs_instance.ElapsedTime; //is it time for the projectile to die? if (projectile.ElapsedTime >= projectile.LifeTime) { ecs_instance.delete_entity(entity); return; } Position position = (Position)p_PositionMapper.get(entity); Velocity velocity = (Velocity)p_VelocityMapper.get(entity); Heading heading = (Heading)p_HeadingMapper.get(entity); SpatialPartition spatial = (SpatialPartition)p_SpatialMapper.get(p_Spatial); Vector2 pos = position.Pos; //List<Entity> locals = spatial.QuadTree.retrieveContentsAtLocation(pos); List <Entity> locals = spatial.QuadTree.findAllWithinRange(pos, 32); //anything retrieved? if (locals != null) { //anyone aruond? if (locals.Count > 0) { //for all the locals see if we should do anything for (int i = 0; i < locals.Count; i++) { //dont interact with whom fired you if (locals[i] == projectile.Originator) { continue; } Life life = (Life)p_LifeMapper.get(locals[i]); //if no life, uh, don't check for it... if (life == null) { continue; } //if target is dead, dont worry if (!life.IsAlive) { continue; } //is there an interaction available? Interactable interaction = (Interactable)p_InteractionMapper.get(locals[i]); if (interaction != null) { //get this local's position Position localPosition = (Position)p_PositionMapper.get(locals[i]); //are we close to it? //23 - minimal radial distance for collision to occur if (Vector2.Distance(pos + position.Offset, localPosition.Pos + localPosition.Offset) < 23) { //can we do anything to it? if (interaction.SupportedInteractions.PROJECTILE_COLLIDABLE && interaction.SupportedInteractions.ATTACKABLE) { Factions lfactions = (Factions)p_FactionMapper.get(locals[i]); Factions pfactions = (Factions)p_FactionMapper.get(projectile.Originator); if (lfactions.OwnerFaction.FactionType == pfactions.OwnerFaction.FactionType) { continue; } //UtilFactory.createAttack(projectile.Originator, locals[i], AttackType.Projectile); ActionDef def = GameConfig.ActionDefs["RANGED_DMG"]; ActionFactory.createAction(def, projectile.Originator, locals[i]); //destory yourself ecs_instance.delete_entity(entity); return; } } } } } } MapCollidable mapCollide = (MapCollidable)p_MapCollidableMapper.get(entity); if (mapCollide != null) { if (mapCollide.Collided) { /* * Vector2 norm = mapCollide.ResponseVector; * norm.Normalize(); * Vector2 reflect = Vector2.Reflect(heading.getHeading(), norm); * reflect.Normalize(); * * //Transform trans = (Transform)p_TransformMapper.get(entity); * * //trans.Rotation = -VectorHelper.getAngle2(new Vector2(1,0), reflect); * * heading.setHeading(reflect); */ UtilFactory.createSound("audio\\effects\\hitwall", true, 0.5f); ecs_instance.delete_entity(entity); } } pos += heading.getHeading() * velocity.Vel; position.Pos = pos; }