public static List<Location> lerp(Location from, Location to) { double deltaX = to.x - from.x; double deltaY = from.y - to.y; double error = 0.0; double deltaerr = Math.Abs(deltaY/deltaX); if (Math.Abs(deltaX) < 0.1) return lerpVert(from, to); List<Location> lerp = new List<Location>(); int y = from.y; int sx = (from.x < to.x) ? 1 : -1; int sy = (from.y < to.y) ? 1 : -1; for (int x = from.x; x != to.x; x += sx) { lerp.Add(new Location(x, y)); error += deltaerr; while (error >= 0.5) { y = y + sy; error--; lerp.Add(new Location(x, y)); } } return lerp; }
public double distance(Location other) { int dx = other.x - x; int dy = other.y - y; return Math.Sqrt(dx*dx + dy*dy); }
public Entity(DrawTag drawTag, string name) { _location = new Location(0, 0); this.name = name; _drawTag = drawTag; discovered = false; lit = LIT_DIM; }
public static Effect buildEffect(EffectDescription description, Location heroLocation) { switch (description.type) { case EffectType.Explosion: return buildExplosion(description, heroLocation); } return null; }
public static Location getFirstStep(Location start, Location end, Level level) { AStarNode lastStep = aStarSearch(start, end, level); while (lastStep.parent != null && lastStep.parent.parent != null) { lastStep = lastStep.parent; } return lastStep.location; }
protected Effect(int interval, DrawTag drawTag, Location coord) { TimerCallback callback = atInterval; timer = new System.Threading.Timer(callback, null, 0, interval); this.drawTag = drawTag; this.coord = coord; finished = false; }
private static List<Location> lerpVert(Location from, Location to) { List<Location> lerp = new List<Location>(); int dy = (from.y < to.y) ? 1 : -1; for (int y = from.y; y != to.y; y += dy) { lerp.Add(new Location(from.x, y)); } return lerp; }
private static Explosion buildExplosion(EffectDescription description, Location heroLocation) { Location location = description.startLocation; location.x -= heroLocation.x; location.y -= heroLocation.y; location.x *= LevelView.TILE_SIZE; location.y *= LevelView.TILE_SIZE; Explosion explosion = new Explosion(location); return explosion; }
private static void scanOctant2(Level level, Location heroLocation, int depth, double startSlope, double endSlope) { if (depth >= Hero.MAX_SIGHT_DISTANCE) { return; } int y = heroLocation.y - depth; int x = heroLocation.x + (int)(startSlope * depth); while (getSlope(x, y, heroLocation.x, heroLocation.y, false) <= endSlope) { if (level.blocksSight(new Location(x, y))) { if (!level.blocksSight(new Location(x + 1, y))) { double newEndSlope = getSlope(x + 0.5, y + 0.5, heroLocation.x, heroLocation.y, false); scanOctant2(level, heroLocation, depth + 1, startSlope, newEndSlope); } } else { if (level.blocksSight(new Location(x + 1, y))) { startSlope = -getSlope(x + 0.5, y - 0.5, heroLocation.x, heroLocation.y, false); } } level.setLit(new Location(x, y), 0); x--; } x++; if (depth < Hero.MAX_SIGHT_DISTANCE && !level.blocksSight(new Location(x, y))) { scanOctant2(level, heroLocation, depth + 1, startSlope, endSlope); } }
private void drawEntity(SpriteBatch spriteBatch, Texture2D atlas, Entity entity, Location screenLocation) { if (!entity.discovered) return; Rectangle source = imagePositions[entity.drawTag]; Rectangle dest = new Rectangle(screenLocation.x * TILE_SIZE, screenLocation.y * TILE_SIZE, TILE_SIZE, TILE_SIZE); spriteBatch.Draw(atlas, dest, source, Color.White); int alpha = entity.lit; Color color = new Color(0, 0, 0, alpha); spriteBatch.Draw(Game1.alphaOverlay, dest, color); if (entity is Actor) { drawHealthBar(spriteBatch, (Actor)entity, dest); } }
public static void generateDungeon(Level level, int width, int height) { Rect dungeonRect = new Rect(0, 0, width, height); level.bounds = dungeonRect; carveRoom(level, dungeonRect, true, TileType.Dungeon_Wall); Location center = new Location(width / 2, height / 2); Rect innerRoom = createRect(center, Direction.West, Feature.Room); carveRoom(level, innerRoom, false, TileType.Dungeon_Floor); for (int i = 0; i < 100; i++) { Location doorLoc = findDoor(level, dungeonRect); //randomly choose a feature to add to the dungeon Feature[] features = {Feature.Corridor, Feature.Room}; Feature feature = Util.chooseRandomElement(features); tryAddFeature(level, doorLoc, feature); } addEntranceAndExit(level, dungeonRect); addChests(level, dungeonRect); addMonsters(level, dungeonRect); initLighting(level, dungeonRect); }
public void addActor(Actor actor, Location location) { actor.location = location; _actors.Add( actor); }
public EffectDescription(EffectType type, Location startLocation) { this.type = type; this.startLocation = startLocation; }
public void setLit(Location location, int lit) { List<Entity> entities = getEntities(location); foreach (Entity entity in entities) { entity.lit = lit; } }
private int getLit(Location location) { Tile tile = getTile(location); if (tile == null) return -1; return tile.lit; }
public Entity getWalkable(Location location) { return _walkables.ContainsKey(location) ? _walkables[location] : null; }
public void addWalkable(Entity entity, Location location) { entity.location = location; if (_walkables.ContainsKey(location)) { _walkables.Remove(location); } _walkables.Add(location, entity); }
public void draw(SpriteBatch spriteBatch, Texture2D atlas, int screenWidth, int screenHeight) { int screenVertTiles = screenHeight/TILE_SIZE; int screenHorizTiles = screenWidth/TILE_SIZE; Location heroLocation = _level.getHeroLocation(); int x1 = heroLocation.x - (screenHorizTiles / 2); int x2 = heroLocation.x + (screenHorizTiles / 2); int y1 = heroLocation.y - (screenVertTiles / 2); int y2 = heroLocation.y + (screenVertTiles / 2); bool redraw = _level.hero.visibleRect.Equals(new Rect(0, 0, 0, 0)); _level.hero.visibleRect = new Rect(x1, y1, x2, y2); _level.hero.visibleRect.expand(); for (int x = x1; x <= x2; x++) { for (int y = y1; y <= y2; y++) { Location location = new Location(x, y); Location offset = new Location(x - x1, y - y1); List<Entity> entities = _level.getEntities(location); foreach (Entity entity in entities) { drawEntity(spriteBatch, atlas, entity, offset); } } } if (redraw) { _level.updatePlayerVision(); draw(spriteBatch, atlas, screenWidth, screenHeight); } }
private static void tryAddFeature(Level level, Location doorLoc, Feature feature) { Direction[] directions = { Direction.North, Direction.South, Direction.West, Direction.East }; foreach (Direction direction in directions) { Rect rect = createRect(doorLoc, direction, feature); rect.expand(); if (rectIsClear(level, rect)) { rect.compress(); if (feature == Feature.Room) { carveRoom(level, rect, false, TileType.Dungeon_Floor); } else if (feature == Feature.Corridor) { carveCorridor(level, rect, false, TileType.Dungeon_Floor); } level.addTile(new Tile(false, TileType.Dungeon_Floor), doorLoc); level.addWalkable(new Door(), doorLoc); break; } } }
public void addItem(Item item, Location location) { item.location = location; _items.Add(item.location, item); }
public Actor getActor(Location location) { foreach (Actor actor in _actors) { if (actor.location.Equals(location)) return actor; } return null; }
public bool blocksSight(Location location) { Tile tile = getTile(location); Entity walkable = getWalkable(location); if (tile != null && tile.blocksMovement) return true; if (walkable != null) { if (walkable is Door && ((Door) walkable).closed) return true; if (walkable is Chest) return true; } return false; }
public Explosion(Location coord) : base(50, DrawTag.Explosion_1, coord) { _curSprite = 0; }
public void removeItem(Location location) { if (_items.ContainsKey(location)) { _items.Remove(location); } }
public List<Entity> getEntities(Location location) { //TODO add the other entites from the other layers to the list Entity[] entities = { getTile(location), getWalkable(location), getItem(location), getActor(location) }; List<Entity> result = new List<Entity>(); foreach (Entity entity in entities) { if (entity != null) { result.Add(entity); } } return result; }
public void addTile(Tile tile, Location location) { tile.location = location; if (_tiles.ContainsKey(location)) { _tiles.Remove(location); } _tiles.Add(tile.location, tile); }
public AStarNode(Location location) { this.location = location; g = 1000; }
public Item getItem(Location location) { return _items.ContainsKey(location) ? _items[location] : null; }
private static AStarNode aStarSearch(Location start, Location end, Level level) { HashSet<AStarNode> open = new HashSet<AStarNode>(); HashSet<AStarNode> closed = new HashSet<AStarNode>(); AStarNode startNode = new AStarNode(start); startNode.g = 0; startNode.h = start.distance(end); open.Add(startNode); while (open.Count > 0) { AStarNode q = new AStarNode(new Location(0, 0)); double min = 10000; foreach (AStarNode node in open) { if (node.f() < min) { min = node.f(); q = node; } } open.Remove(q); Direction[] directions = {Direction.North, Direction.South, Direction.East, Direction.West}; for (int i = 0; i < 4; i++) { Location adj = q.location.getAdjLocation(directions[i]); if (level.getTile(adj).blocksMovement) continue; AStarNode node = new AStarNode(adj); if (closed.Contains(node)) continue; node.setParent(q); if (adj.Equals(end)) return node; node.g = q.g + 1; node.h = adj.distance(end); bool add = true; foreach (AStarNode openNode in open) { if (openNode.location.Equals(node.location) && openNode.f() < node.f()) add = false; } foreach (AStarNode closedNode in closed) { if (closedNode.location.Equals(node.location) && closedNode.f() < node.f()) add = false; } if (add) { open.Add(node); } } closed.Add(q); } return startNode; }
public Tile getTile(Location location) { return _tiles.ContainsKey(location) ? _tiles[location] : null; }