public void attack(Actor owner, Actor target) { if (target != null) { Destructible destructible = (Destructible)target.getComponent(typeof(Destructible)); if (destructible != null && !destructible.isDead()) { if (power - destructible.defense > 0) { if (owner != Program.engine.player) { if (target != Program.engine.player) { Message.WriteLineC(TCODColor.red, "The {0} attacks the {1} for {2} hit points.", owner.name, target.name, power - destructible.defense); } else { Message.WriteLineC(TCODColor.red, "The {0} attacks {1} for {2} hit points.", owner.name, target.name, power - destructible.defense); } } else { Message.WriteLineC(TCODColor.green, "You attack the {1} for {2} hit points.", owner.name, target.name, power - destructible.defense); } } else { if (owner != Program.engine.player) { Message.WriteLineC(TCODColor.red, "the {0} attacks the {1} but it has no effect!\n", owner.name, target.name); } else { Message.WriteLineC(TCODColor.green, "You attack the {1} for {2} hit points.\n", owner.name, target.name, power - destructible.defense); } } destructible.takeDamage(target, power); if (destructible.hp < 0) { destructible.hp = 0; } } else { if (owner != Program.engine.player) { Message.WriteLineC(TCODColor.red, "The {0} attacks {1} in vain.\n", owner.name, target.name); } else { Message.WriteLineC(TCODColor.green, "You attack the {1} for {2} hit points.\n", owner.name, target.name, power - destructible.defense); } } } }
public override void load(Actor owner) { base.load(owner); player = (Player)owner; destructible = (Destructible)player.getComponent(typeof(Destructible)); }
public bool isDead() { Components.Destructible d = (Components.Destructible)getComponent(typeof(Components.Destructible)); if (d != null && !d.isDead()) { return(false); } return(true); }
public PlayerAI(Player player) : base(player) { try { Dictionary <char, string> c = fastJSON.JSON.ToObject <Dictionary <char, string> >(File.ReadAllText("data/config/controls.json")); if (c.Count >= controls.Count) { controls = c; } else { Dictionary <char, string> missingControls = c; foreach (char ch in controls.Keys) { if (!c.ContainsValue(controls[ch])) { missingControls.Add(ch, controls[ch]); } } if (missingControls.Count > 0) { File.WriteAllText("data/config/controls.json", fastJSON.JSON.ToJSON(missingControls)); } } } catch (DirectoryNotFoundException e) { Console.WriteLine("Directory Settings was not found, using default values"); Directory.CreateDirectory("data/config/"); File.WriteAllText("data/config/controls.json", fastJSON.JSON.ToJSON(controls)); } catch (FileNotFoundException e) { Console.WriteLine("Controls.json not found, using default values"); File.WriteAllText("data/config/controls.json", fastJSON.JSON.ToJSON(controls)); } this.player = player; this.owner = player; destructible = (Destructible)player.getComponent(typeof(Destructible)); }
public Actor getClosestMonster(int x, int y, float range) { Actor closest = null; float bestDistance = float.MaxValue; for (int i = 0; i < Program.engine.currentLevel.actorHandler.actors.Count; i++) { Actor actor = Program.engine.currentLevel.actorHandler.actors[i]; Components.Destructible d = actor.getDestructible(); if (actor != Program.engine.player && d != null && !d.isDead()) { float distance = actor.getDistance(x, y); if (distance < bestDistance && (distance <= range || range == 0.0f)) { bestDistance = distance; closest = actor; } } } return(closest); }
public void update() { if (engine.gameStatus == GameStatus.STARTUP || engine.gameStatus == GameStatus.IDLE) { if (engine.gameStatus == GameStatus.STARTUP) { engine.player.fov.updateFov(); } engine.gameStatus = GameStatus.IDLE; if (engine.key.Character == '|' || engine.key.Character == '`' || locked) { Message.WriteLine(">"); string s = getInput(record[index]).ToLower(); locked = false; engine.player.paused = false; if (s == "|") { Message.unwriteLastLine(); return; } if (s != "" && s != "up" && s != "down") { record[index] = s; record.Add(""); index = record.Count - 1; } string[] coms = s.Split(' '); string com = ""; string[] args = new string[0]; if (coms.Length > 0) { com = coms[0]; if (coms.Length > 1) { args = new string[coms.Length - 1]; } } for (int i = 0; i < args.Length; i++) { args[i] = coms[i + 1]; } switch (com) { case "setposition": case "position": case "setpos": case "pos": case "p": { setPos(); break; } case "save": { string file = "save"; if (args.Length > 0) { file = args[0].ToString(); } bool success = Saver.saveGame(file); if (success) { Message.WriteLine("Saving complete"); } else { Message.WriteLine("Saving failed"); } break; } case "load": { if (args.Length > 0) { bool success = Saver.loadGame((args[0].ToString())); if (success) { Message.WriteLine("Loading complete"); } else { Message.WriteLine("Could not find load file"); } } break; } case "generatemap": case "generate": case "gen": case "g": { Console.WriteLine("Generating Map..."); engine.map.generate(); engine.gameStatus = GameStatus.STARTUP; break; } case "kill": { if (args.Length > 0) { if (args[0] == "me") { DestructiblePlayer d = (DestructiblePlayer)engine.player.getDestructible(); d.hp = 0; d.die(engine.player); } else if (args[0] == "all") { string pluralname = ""; if (args.Length > 1) //Kill one type of actors { pluralname = args[1]; } for (int i = 0; i < engine.actorHandler.actors.Count; i++) { if (i < engine.actorHandler.actors.Count) { Actor a = engine.actorHandler.actors[i]; if (a != engine.player && (pluralname == "" || pluralname == a.pluralName)) { Janus.Engine.Components.Destructible d = a.getDestructible(); if (d != null && d.hp > 0) { d.hp = 0; d.die(a); } } } } } } else { Message.WriteLine("Missing argument"); } break; } case "noclip": case "ncl": case "n": { PlayerAI ai = (PlayerAI)engine.player.getComponent(typeof(PlayerAI)); if (ai != null) { ai.noclip = !ai.noclip; if (ai.noclip) { Message.WriteLineC(TCODColor.yellow, "DEBUG: Noclip activated"); } else { Message.WriteLineC(TCODColor.yellow, "DEBUG: Noclip deactivated"); } } break; } case "godmode": case "gm": { DestructiblePlayer d = (DestructiblePlayer)engine.player.getComponent(typeof(DestructiblePlayer)); if (d != null) { d.defense = int.MaxValue; } break; } case "spawn": case "s": case "create": { spawn(args); break; } case "give": { give(args); break; } case "showall": { engine.map.showAllTiles = !engine.map.showAllTiles; if (engine.map.showAllTiles) { Message.WriteLineC(TCODColor.yellow, "DEBUG: Showing all tiles"); } else { Message.WriteLineC(TCODColor.yellow, "DEBUG: Not showing all tiles"); } break; } case "changelevel": case "level": { Message.WriteLine("Insert level number:"); try { int number = int.Parse(getInput()); engine.changeLevel(number); engine.player.x = engine.currentLevel.map.startx; engine.player.y = engine.currentLevel.map.starty; } catch (Exception e) { Message.WriteLine("Invalid number"); } Message.unwriteLastLine(); break; } case "": { Message.unwriteLastLine(); break; } case "up": { if (index > 0) { index--; } else { index = record.Count - 1; } Message.unwriteLastLine(); locked = true; engine.player.paused = true; break; } case "down": { if (index < record.Count - 1) { index++; } else { index = 0; } Message.unwriteLastLine(); locked = true; engine.player.paused = true; break; } default: { Message.WriteLine("Unknown or incorrect command"); break; } } } if (engine.key.Character == 'S') { Saver.saveGame("newSave"); } if (engine.key.Character == 'L') { Saver.loadGame("newSave"); } } }
public bool moveOrAttack(Actor owner, int targetx, int targety) { if (Program.engine.map.isWall(targetx, targety)) { if (dig) { if (targetx < Program.engine.map.width - 1 && targety < Program.engine.map.height - 1 && targetx > 1 && targety > 1) { Program.engine.map.dig(targetx, targety, targetx, targety); Program.engine.map.updateFov = true; } else if (!noclip) { return(false); } } else if (!noclip) { return(false); } } else if (!noclip) { dig = false; } for (int i = 0; i < Program.engine.actorHandler.actors.Count; i++) { Actor a = Program.engine.actorHandler.actors[i]; Destructible d = (Destructible)a.getComponent(typeof(Destructible)); if (a.x == targetx && a.y == targety) { if (d != null && !d.isDead() && a.blocks) { if (player.mainToolSlot.item != null) { Components.Pickables.Wearable wearable = (Components.Pickables.Wearable)player.mainToolSlot.component; if (wearable != null && wearable.activeEffects.Count > 0) { foreach (Effect e in wearable.activeEffects) { e.applyTo(a); } } } Attacker att = (Attacker)a.getComponent(typeof(Attacker)); att.attack(owner, a); return(false); } if ((d != null && !d.isDead()) || a.getComponent(typeof(Components.Pickable)) != null) { Message.WriteLine("There's a {0} here", a.name); } Components.Blocks.Activatable activatable = (Blocks.Activatable)a.getComponent(typeof(Components.Blocks.Activatable)); if (activatable != null) { bool exit = activatable.activate(player); if (exit) { return(false); } } if (a.blocks) { if (autoOpen) { Blocks.Door door = (Blocks.Door)a.getComponent(typeof(Blocks.Door)); if (door != null && !door.open) { if (door.lockedLevel == 0) { door.open = true; Program.engine.map.updateFov = true; } else { Message.WriteLineC(TCODColor.lightGrey, "The door is locked"); } } open = false; } if (!noclip) { return(false); } } else { } } } // look for corpses player.x = targetx; player.y = targety; open = false; return(true); }