/// <summary> /// Uses the VisualServer to draw each of an entity's split sprites, by: /// 1. Obtaining the Node2D that is designated to play host to a given split sprite /// 2. Changing the Node2D's (TileMap) parent if required /// 3. Creating an RID for the canvas item (the sprite drawing) and storing it so it can be freed later /// </summary> /// <param name="tileMaps"></param> /// <param name="tileMapZIndex"></param> /// <param name="entityPos"></param> /// <param name="entityDrawPos">Offsets the sprite upwards. Used for drawing entities falling/jumping.</param> public void DrawSplitSprites(TileMapList tileMaps, int tileMapZIndex, Vector2 entityPos, float entityDrawPos /*State goes here*/) { List <SplitSpriteSheetValue> splitSprites = this._splitSpriteSheet.GetSplitSpriteInfoForAnim("idle", 0); //TODO match to entity anim state foreach (SplitSpriteSheetValue splitSprite in splitSprites) { Node2D splitNode2D = this._idToSplitNodes[splitSprite.splitIndex]; TileMap tileMap = tileMaps[tileMapZIndex + splitSprite.zIndex]; if (splitNode2D.GetParent() != tileMap) { if (splitNode2D.GetParent() != null) { splitNode2D.GetParent().RemoveChild(splitNode2D); } tileMap.AddChild(splitNode2D); } RID splitSpriteRID = VisualServer.CanvasItemCreate(); this._drawnSprites.Add(splitSpriteRID); VisualServer.CanvasItemSetParent(splitSpriteRID, splitNode2D.GetCanvasItem()); VisualServer.CanvasItemAddTextureRectRegion(splitSpriteRID, new Rect2(Vector2.Zero, splitSprite.size), this._splitSpriteSheet.GetSpriteSheetRID(), new Rect2(splitSprite.sheetPos, splitSprite.size), new Color(1, 1, 1, 1), false, this._splitSpriteSheet.GetSpriteSheetRID()); VisualServer.CanvasItemSetTransform(splitSpriteRID, new Transform2D(0, this._relativePos - new Vector2(Globals.TileWidth / 2, Globals.TileHeight / 2) + new Vector2(0, entityDrawPos))); splitNode2D.Position = entityPos + splitSprite.splitPos + new Vector2(Globals.TileWidth / 2, Globals.TileHeight / 2); } }
private void _connectTraversableTiles(Godot.Collections.Array tiles) { foreach (Vector2 tile in tiles) { // Add all neighbors for (int indexX = -1; indexX < 2; indexX++) { for (int indexY = -1; indexY < 2; indexY++) { Vector2 targetTile = new Vector2(tile.x + indexX, tile.y + indexY); int toId = getPointID((int)targetTile.x, (int)targetTile.y); int fromId = getPointID((int)tile.x, (int)tile.y); if (tile == targetTile || !_aStar.HasPoint(toId)) { // No need to add tile as it is the same continue; } // Caculate if diagonal point can be used // (this is to avoid to given a diagonal path to agent, but agent cannot pass as vertical/horizontal has obstacles) // Upper left/right diagonal (-1,1), (1,1) // which need (-1,0) (horizontal), (0,1) (veritcal), (1,0) (horizontal) // Bottom left/right diagonal (-1,-1), (1,-1) // which need (-1,0) (horizontal), (0,-1) (veritcal), (1,0) (horizontal) // this is to avoid stuck on corner if (indexX != 0 && indexY != 0) { int horizaontalId = getPointID((int)tile.x + indexX, (int)tile.y); int verticalId = getPointID((int)tile.x, (int)tile.y + indexY); // Upper diaonal connection depend on if there is obstacle for vertical/horizontal neighbors // If there are obstacles (i.e. point not exist, then not connect it) if (!_aStar.HasPoint(horizaontalId) || !_aStar.HasPoint(verticalId)) { continue; } } if (!_aStar.ArePointsConnected(fromId, toId)) { // Debug code if (debug) { Line2D line2d = new Line2D(); Vector2[] points = { (Vector2)_tilestoWorld[fromId], (Vector2)_tilestoWorld[toId] }; line2d.Points = points; _tileMap.AddChild(line2d); } _aStar.ConnectPoints(fromId, toId, true); } } } } }
private void _MakeEntityRise(Entity entity) { var currentTileMap = (TileMap)entity.GetParent(); TileMap higherTileMap = this._tileMaps[currentTileMap.ZIndex + 1]; entity.gravityComponent.SetAscendParameters(); currentTileMap.RemoveChild(entity); higherTileMap.AddChild(entity); this._HandleEntityZIndexChange(entity, higherTileMap.ZIndex - 1); }
private void _MakeEntityFall(Entity entity) { var currentTileMap = (TileMap)entity.GetParent(); TileMap lowerTileMap = this._tileMaps[currentTileMap.ZIndex - 1]; entity.gravityComponent.SetInAir(); currentTileMap.RemoveChild(entity); lowerTileMap.AddChild(entity); this._HandleEntityZIndexChange(entity, lowerTileMap.ZIndex - 1); }
public override void Start() { tileMap = new TileMap(); tileMap.LoadTileMap("Texts/MapBig"); AddChild(tileMap); player = new Player(); cameraPosition = player.GetPosition(); tileMap.AddChild(player); ListenForUpdate(Update); AddChild(hudShadow); AddChild(hudStuff); }
public override void _Ready() { _tilemap = this.GetNode <TileMap>("../Ground/SecondLayer"); _random = new RandomNumberGenerator(); _random.Randomize(); _currentAnimations = new Dictionary <string, AnimatedSprite>(); for (int i = 0; i < resources.Length; i++) { var resource = resources[i]; var resourceTriggerTileId = resourceTriggerTileIds[i]; var numberOfResource = numberOfResources[i]; for (var x = 1; x <= numberOfResource; x++) { var resourceKey = $"{resource}_{x}"; var resourceScene = ResourceLoader.Load <PackedScene>(resource); var resourceSceneInstance = (AnimatedSprite)resourceScene.Instance(); resourceSceneInstance.Position = GetAnimationRandomPosition(resourceTriggerTileId); resourceSceneInstance.Scale = new Vector2(0.3f, 0.3f); resourceSceneInstance.Connect("animation_finished", this, "_on_Animation_Finished", new Godot.Collections.Array { resourceTriggerTileId, resourceKey }); _tilemap.AddChild(resourceSceneInstance); _currentAnimations.Add(resourceKey, resourceSceneInstance); resourceSceneInstance.Play(); } } }