Skip to content

Instantly share code, notes, and snippets.

@remzmike
Created August 9, 2021 15:55
Show Gist options
  • Select an option

  • Save remzmike/c188f8cc5e8970618a1a8d1f38780417 to your computer and use it in GitHub Desktop.

Select an option

Save remzmike/c188f8cc5e8970618a1a8d1f38780417 to your computer and use it in GitHub Desktop.
NOTES for "Bob Nystrom - Is There More to Game Architecture than ECS?" @ https://www.youtube.com/watch?v=JxI3Eu5DPwE
// "Bob Nystrom - Is There More to Game Architecture than ECS?"
// https://www.youtube.com/watch?v=JxI3Eu5DPwE
// Idea 0. We don't need ECS.
// Idea 1. Use components (regular components not ecs) to represent capabilities.
class Item {
Attack melee;
Attack ranged
Defense defense;
Use use;
}
class Attack {
int minDamage;
int maxDamage;
void hit() {}
}
class Defense {
int armor;
int dodgeBonus
void defend() {}
}
abstract class Use {
void use();
}
class HealUse extends Use {
void use() {
hero.health += 20;
}
}
class FireBallUse extends Use {
void use() {
}
}
var sword = Item(
melee: Attack(10, 20)
);
var crossbow = Item(
ranged: Attack(10, 20)
)
var shield = Item(
melee: Attack(5, 8),
defense: Defense(3, 0)
)
var healPotion = Item(
quaff: HealUse()
)
var fireSword = Item(
melee: Attack(30,40),
activate: FireBallUse()
)
// Idea 2. Game object types.
class Breed {
String name;
int maxHealth;
Attack attack;
List<Use> moves;
Set<String> flags;
Drop loot;
}
class Monster {
Breed breed;
int health;
int x, y;
}
// ...
class Actor {
int x, y;
}
class Hero extends Actor {
HeroClass heroClass;
}
class Monster extends Actor {
Breed breed;
}
// Idea 3. Command pattern.
abstract class Action {
ActionResult perform();
}
void gameLoop() {
for (var actor in actors) {
actor.gainEnergy(actor.speed);
if (actor.hasEnergy(actor.speed)) {
var action = actor.takeTurn();
action.perform();
}
}
}
class WalkAction extends Action {
Direction dir;
ActionResult perform() {
var pos = actor.pos + dir;
// see if actor there
var target = game.stage.actorAt(pos);
if (target != null) return alternate(AttackAction(target));
// see if it's a door
var tile = game.stage[pos];
if (tile.isDoor) return alternate(OpenDoor(pos));
// see if we can walk there
if (!actor.canOccupy(pos)) return fail("You hit the wall!");
actor.pos = pos;
// see if hero stepped on anything interestint
if (actor is Hero) {
for (var item in game.stage.itemsAt(pos).toList()) {
log("You are standing on $item.");
}
}
return succeed();
}
}
// example actions
/*
AttackAction
BarrierAction
BashAction
BlindAction
BoltAction
BurnActorAction
BurnFloorAction
BurningFloorAction
CloseDoorAction
ConditionAction
DiseaseAction
DropAction
EquipAction
HealAction
ItemAction
OpenChestAction
LightFloorAction
LosAction
SpawnAction
WalkAction
WindAction
...etc
*/
class Hero extends Actor {
Action takeTurn() {
switch(readKeyPress()) {
case "d": return DropAction();
case "u": return UseAction();
case "r": RestAction();
}
}
}
class Monster extends Actor {
Action takeTurn() {
// pathfinding, other ai...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment