/// <summary> /// Remove entity from an entity list at its old map location, and put it in the list at its new location /// </summary> /// <param name="entity">Entity to move</param> /// <param name="oldPos">Old position</param> public static void UpdateEntityMapPos(EntityID entity, Vector2i oldPos) { var e = entity.e; if (e == null) { return; } if (oldPos.x >= 0 && oldPos.y >= 0) { var entityList = RB.MapDataGet <List <EntityID> >(C.LAYER_TERRAIN, oldPos); if (entityList != null) { entityList.Remove(entity); } } Vector2i newPos = e.pos; if (newPos.x >= 0 && newPos.y >= 0) { var entityList = RB.MapDataGet <List <EntityID> >(C.LAYER_TERRAIN, newPos); if (entityList != null) { entityList.Add(entity); } } }
private bool LoadMap() { mTitleMap.Load("Demos/SuperFlagRun/TitleMap"); mGameMap.Load("Demos/SuperFlagRun/GameMap"); if (mTitleMap != null && mTitleMap.status == RB.AssetStatus.Ready) { mTitleMap.LoadLayer("Terrain", SuperFlagRun.MAP_LAYER_TITLE_TERRAIN); mTitleMap.LoadLayer("Deco", SuperFlagRun.MAP_LAYER_TITLE_DECO); mTitleMap.LoadLayer("Sky", SuperFlagRun.MAP_LAYER_TITLE_SKY); } if (mGameMap != null) { mGameMap.LoadLayer("Sky", SuperFlagRun.MAP_LAYER_SKY); mGameMap.LoadLayer("Clouds", SuperFlagRun.MAP_LAYER_CLOUDS); mGameMap.LoadLayer("Terrain", SuperFlagRun.MAP_LAYER_TERRAIN); mGameMap.LoadLayer("Background", SuperFlagRun.MAP_LAYER_BACKGROUND); } RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_TITLE_TERRAIN, assets.spriteSheetTerrain); RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_TITLE_DECO, assets.spriteSheetDeco); RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_TITLE_SKY, assets.spriteSheetDeco); RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_SKY, assets.spriteSheetDeco); RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_CLOUDS, assets.spriteSheetDeco); RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_TERRAIN, assets.spriteSheetTerrain); RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_BACKGROUND, assets.spriteSheetDeco); RB.SpriteSheetSet(assets.spriteSheetTerrain); if (mGameMap != null) { mGameMapSize = mGameMap.size; // Convert TMXProperties to simple ColliderInfo.ColliderType, for faster access for (int x = 0; x < mGameMapSize.width; x++) { for (int y = 0; y < mGameMapSize.height; y++) { var tilePos = new Vector2i(x, y); var tileProps = RB.MapDataGet <TMXMapAsset.TMXProperties>(MAP_LAYER_TERRAIN, tilePos); if (tileProps != null) { RB.MapDataSet <ColliderInfo.ColliderType>(MAP_LAYER_TERRAIN, tilePos, (ColliderInfo.ColliderType)tileProps.GetInt("ColliderType")); } else { RB.MapDataSet <ColliderInfo.ColliderType>(MAP_LAYER_TERRAIN, tilePos, ColliderInfo.ColliderType.NONE); } } } } return(true); }
public bool isPointBlocked(Vector2i tilePos) { if (tilePos.x < 0 || tilePos.y < 0) { return(true); } if (tilePos.x >= mapSize.width || tilePos.y >= mapSize.height) { return(true); } tilePos /= 32; foreach (LayerMap m in mapLayers) { var tileProps = RB.MapDataGet <TMXProperties>(m.layer, tilePos); if (tileProps != null && tileProps.GetBool("blocked")) { return(true); } } return(false); }
/// <summary> /// Reset all entity lists for all tiles. This is expensive and should not be done often. /// </summary> public void UpdateAllEntityMapPositions() { // Clear all entity lists for (int x = 0; x < size.width; x++) { for (int y = 0; y < size.height; y++) { var entityList = RB.MapDataGet <List <EntityID> >(C.LAYER_TERRAIN, new Vector2i(x, y)); if (entityList == null) { entityList = new List <EntityID>(); RB.MapDataSet(C.LAYER_TERRAIN, new Vector2i(x, y), entityList); } else { entityList.Clear(); } } } for (int i = 0; i < EntityStore.entities.Count; i++) { var e = EntityStore.entities[i].e; if (e == null) { continue; } if (e.pos.x < 0 || e.pos.y < 0 || e.pos.x >= size.width || e.pos.y >= size.height) { continue; } var entityList = RB.MapDataGet <List <EntityID> >(C.LAYER_TERRAIN, e.pos); entityList.Add(e.id); } }
/// <summary> /// The heart of platformer physics, resolves any collisions /// </summary> /// <param name="newPos">New position to resolve collisions for</param> /// <param name="axis">Axis to resolve on 0 = X, 1 = Y</param> private void ResolveCollisions(Vector2 newPos, int axis) { Rect2i rect = mEntity.ColliderInfo.Rect.Offset(new Vector2i((int)newPos.x, (int)newPos.y)); int leftTile = (int)Mathf.Floor((int)rect.min.x / RB.SpriteSheetGet().grid.cellSize.width) - 1; int rightTile = (int)Mathf.Ceil((int)rect.max.x / RB.SpriteSheetGet().grid.cellSize.width); int topTile = (int)Mathf.Ceil((int)rect.min.y / RB.SpriteSheetGet().grid.cellSize.height) - 1; int bottomTile = (int)Mathf.Floor((int)rect.max.y / RB.SpriteSheetGet().grid.cellSize.height); /* If we're calculating Y axis then reset the on ground flag. It will be set * to true if we hit something from the top */ if (axis == 1) { mIsOnGround = false; } // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { var tile = RB.MapSpriteGet(0, new Vector2i(x, y)); if (tile != RB.SPRITE_EMPTY) { var collisionType = RB.MapDataGet <ColliderInfo.ColliderType>(SuperFlagRun.MAP_LAYER_TERRAIN, new Vector2i(x, y)); if (collisionType == ColliderInfo.ColliderType.NONE) { continue; } Rect2i tileRect = new Rect2i(x * RB.SpriteSheetGet().grid.cellSize.width, y * RB.SpriteSheetGet().grid.cellSize.height, RB.SpriteSheetGet().grid.cellSize.width, RB.SpriteSheetGet().grid.cellSize.height); bool collided; Vector2 depth; newPos = ResolveCollisionForRects(rect, tileRect, newPos, collisionType, null, axis, out collided, out depth); // Perform further collisions with the new bounds. rect = mEntity.ColliderInfo.Rect.Offset(new Vector2i((int)newPos.x, (int)newPos.y)); if (!mIsOnGround) { Rect2i groundCheckRect = rect; groundCheckRect.y += 1; groundCheckRect.height -= 1; depth = groundCheckRect.IntersectionDepth(tileRect); if (depth.y < 0) { mIsOnGround = true; } } } } } mPreviousRect = mEntity.ColliderInfo.Rect.Offset(new Vector2i((int)newPos.x, (int)newPos.y)); mEntity.Pos = newPos; mJumped = false; }
/// <summary> /// Get all entities at the given position /// </summary> /// <param name="pos">Position to check</param> /// <returns>List of entities at position</returns> public static List <EntityID> GetEntitiesAtPos(Vector2i pos) { return(RB.MapDataGet <List <EntityID> >(C.LAYER_TERRAIN, pos)); }