added moveFwd() and rotate() to agent
This commit is contained in:
parent
3917fbf26d
commit
d77f74ca59
70
agent2.py
70
agent2.py
@ -1,8 +1,10 @@
|
|||||||
from mesa import Agent
|
from mesa import Agent
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
def dice(number):
|
def dice(number):
|
||||||
return random.randint(1,number)
|
return random.randint(1, number)
|
||||||
|
|
||||||
|
|
||||||
class Wall(Agent):
|
class Wall(Agent):
|
||||||
def __init__(self, unique_id, model):
|
def __init__(self, unique_id, model):
|
||||||
@ -11,28 +13,32 @@ class Wall(Agent):
|
|||||||
def step(self):
|
def step(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Box(Agent):
|
class Box(Agent):
|
||||||
def __init__(self, unique_id, model):
|
def __init__(self, unique_id, model):
|
||||||
super().__init__(unique_id, model)
|
super().__init__(unique_id, model)
|
||||||
self.gold=3*dice(6)
|
self.gold = 3 * dice(6)
|
||||||
self.isBox=True
|
self.isBox = True
|
||||||
self.isCreature=False
|
self.isCreature = False
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Weapon():
|
class Weapon():
|
||||||
def __init__(self,name,type,damage):
|
def __init__(self, name, type, damage):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.type = type
|
self.type = type
|
||||||
self.damage = damage
|
self.damage = damage
|
||||||
|
|
||||||
|
|
||||||
class Armor():
|
class Armor():
|
||||||
def __init__(self,name,defence,mp):
|
def __init__(self, name, defence, mp):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.defence = defence
|
self.defence = defence
|
||||||
self.mag_protection = mp
|
self.mag_protection = mp
|
||||||
|
|
||||||
|
|
||||||
class Creature(Agent):
|
class Creature(Agent):
|
||||||
def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g):
|
def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g):
|
||||||
super().__init__(unique_id, model)
|
super().__init__(unique_id, model)
|
||||||
@ -46,31 +52,30 @@ class Creature(Agent):
|
|||||||
self.weapon1 = weap
|
self.weapon1 = weap
|
||||||
self.armor = arm
|
self.armor = arm
|
||||||
self.isBox = False
|
self.isBox = False
|
||||||
self.isCreature=True
|
self.isCreature = True
|
||||||
|
|
||||||
|
def meleeAttack(self, opponent):
|
||||||
def meleeAttack(self,opponent):
|
|
||||||
attackValue = self.strength + dice(6)
|
attackValue = self.strength + dice(6)
|
||||||
defenseValue = opponent.strength + opponent.armor.defence
|
defenseValue = opponent.strength + opponent.armor.defence
|
||||||
damage = attackValue - defenseValue
|
damage = attackValue - defenseValue
|
||||||
if damage > 0:
|
if damage > 0:
|
||||||
opponent.health = opponent.health - (damage + self.weapon1.damage)
|
opponent.health = opponent.health - (damage + self.weapon1.damage)
|
||||||
|
|
||||||
def rangeAttack(self,opponent):
|
def rangeAttack(self, opponent):
|
||||||
attackValue = self.agility + dice(6)
|
attackValue = self.agility + dice(6)
|
||||||
defenseValue = opponent.agility
|
defenseValue = opponent.agility
|
||||||
damage = attackValue - defenseValue
|
damage = attackValue - defenseValue
|
||||||
if (damage > 0) and (damage + self.weapon1.damage - opponent.armor.defence > 0):
|
if (damage > 0) and (damage + self.weapon1.damage - opponent.armor.defence > 0):
|
||||||
opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.defence)
|
opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.defence)
|
||||||
|
|
||||||
def magicAttack(self,opponent):
|
def magicAttack(self, opponent):
|
||||||
attackValue = self.wisdom + dice(6)
|
attackValue = self.wisdom + dice(6)
|
||||||
defenseValue = opponent.wisdom
|
defenseValue = opponent.wisdom
|
||||||
damage = attackValue - defenseValue
|
damage = attackValue - defenseValue
|
||||||
if (damage > 0) and (damage + self.weapon1.damage - opponent.armor.mag_protection > 0):
|
if (damage > 0) and (damage + self.weapon1.damage - opponent.armor.mag_protection > 0):
|
||||||
opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.mag_protection)
|
opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.mag_protection)
|
||||||
|
|
||||||
def defaultAttack(self,opponent):
|
def defaultAttack(self, opponent):
|
||||||
if self.weapon1.type == "Meele":
|
if self.weapon1.type == "Meele":
|
||||||
self.meleeAttack(opponent)
|
self.meleeAttack(opponent)
|
||||||
elif self.weapon1.type == "Range":
|
elif self.weapon1.type == "Range":
|
||||||
@ -78,6 +83,7 @@ class Creature(Agent):
|
|||||||
else:
|
else:
|
||||||
self.magicAttack(opponent)
|
self.magicAttack(opponent)
|
||||||
|
|
||||||
|
|
||||||
class Player(Creature):
|
class Player(Creature):
|
||||||
def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g, w2, w3):
|
def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g, w2, w3):
|
||||||
super().__init__(unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g)
|
super().__init__(unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g)
|
||||||
@ -94,8 +100,26 @@ class Player(Creature):
|
|||||||
self.armor = arm
|
self.armor = arm
|
||||||
self.isBox = False
|
self.isBox = False
|
||||||
self.isCreature = False
|
self.isCreature = False
|
||||||
|
self.directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
|
||||||
|
self.direction = 0
|
||||||
|
|
||||||
def move(self):
|
def rotate(self, clockwise=True):
|
||||||
|
if clockwise:
|
||||||
|
self.direction = (self.direction + 1) % 4
|
||||||
|
else:
|
||||||
|
self.direction = (self.direction + 3) % 4
|
||||||
|
|
||||||
|
def moveFwd(self):
|
||||||
|
possible_steps = self.model.grid.get_neighborhood(
|
||||||
|
self.pos,
|
||||||
|
moore=False,
|
||||||
|
include_center=False)
|
||||||
|
new_position = (
|
||||||
|
self.pos[0] + self.directions[self.direction][0], self.pos[1] + self.directions[self.direction][1])
|
||||||
|
if new_position in possible_steps:
|
||||||
|
self.model.grid.move_agent(self, new_position)
|
||||||
|
|
||||||
|
def move(self): # OLD
|
||||||
possible_steps = self.model.grid.get_neighborhood(
|
possible_steps = self.model.grid.get_neighborhood(
|
||||||
self.pos,
|
self.pos,
|
||||||
moore=True,
|
moore=True,
|
||||||
@ -103,21 +127,21 @@ class Player(Creature):
|
|||||||
new_position = self.random.choice(possible_steps)
|
new_position = self.random.choice(possible_steps)
|
||||||
self.model.grid.move_agent(self, new_position)
|
self.model.grid.move_agent(self, new_position)
|
||||||
|
|
||||||
def meleeAttack(self,opponent):
|
def meleeAttack(self, opponent):
|
||||||
attackValue = self.strength + dice(6)
|
attackValue = self.strength + dice(6)
|
||||||
defenseValue = opponent.strength + opponent.armor.defence
|
defenseValue = opponent.strength + opponent.armor.defence
|
||||||
damage = attackValue - defenseValue
|
damage = attackValue - defenseValue
|
||||||
if damage > 0:
|
if damage > 0:
|
||||||
opponent.health = opponent.health - (damage + self.weapon1.damage)
|
opponent.health = opponent.health - (damage + self.weapon1.damage)
|
||||||
|
|
||||||
def rangeAttack(self,opponent):
|
def rangeAttack(self, opponent):
|
||||||
attackValue = self.agility + dice(6)
|
attackValue = self.agility + dice(6)
|
||||||
defenseValue = opponent.agility
|
defenseValue = opponent.agility
|
||||||
damage = attackValue - defenseValue
|
damage = attackValue - defenseValue
|
||||||
if (damage > 0) and (damage + self.weapon2.damage - opponent.armor.defence > 0):
|
if (damage > 0) and (damage + self.weapon2.damage - opponent.armor.defence > 0):
|
||||||
opponent.health = opponent.health - (damage + self.weapon2.damage - opponent.armor.defence)
|
opponent.health = opponent.health - (damage + self.weapon2.damage - opponent.armor.defence)
|
||||||
|
|
||||||
def magicAttack(self,opponent):
|
def magicAttack(self, opponent):
|
||||||
attackValue = self.wisdom + dice(6)
|
attackValue = self.wisdom + dice(6)
|
||||||
defenseValue = opponent.wisdom
|
defenseValue = opponent.wisdom
|
||||||
damage = attackValue - defenseValue
|
damage = attackValue - defenseValue
|
||||||
@ -181,7 +205,7 @@ class Player(Creature):
|
|||||||
opponent.model.grid.remove_agent(opponent)
|
opponent.model.grid.remove_agent(opponent)
|
||||||
print("Fight won")
|
print("Fight won")
|
||||||
|
|
||||||
def openChest(self,chest):
|
def openChest(self, chest):
|
||||||
self.gold = self.gold + chest.gold
|
self.gold = self.gold + chest.gold
|
||||||
print("Chest opened. Gold inside:", chest.gold)
|
print("Chest opened. Gold inside:", chest.gold)
|
||||||
chest.gold = 0
|
chest.gold = 0
|
||||||
@ -205,17 +229,17 @@ class Player(Creature):
|
|||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
if self.health > 0:
|
if self.health > 0:
|
||||||
self.move()
|
self.moveFwd()
|
||||||
|
self.rotate(False)
|
||||||
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
||||||
if len(cellmates) > 1:
|
if len(cellmates) > 1:
|
||||||
if isinstance(cellmates[0],Box):
|
if isinstance(cellmates[0], Box):
|
||||||
self.openChest(cellmates[0])
|
self.openChest(cellmates[0])
|
||||||
else:
|
else:
|
||||||
opponent = cellmates[0]
|
opponent = cellmates[0]
|
||||||
print("Fighting")
|
print("Fighting")
|
||||||
self.fightOrFlight(opponent)
|
self.fightOrFlight(opponent)
|
||||||
print("HP: "+str(self.health)+" / "+str(self.maxHealth))
|
print("HP: " + str(self.health) + " / " + str(self.maxHealth))
|
||||||
print("Gold: "+str(self.gold))
|
print("Gold: " + str(self.gold))
|
||||||
else:
|
else:
|
||||||
print("HP: 0 / "+str(self.maxHealth))
|
print("HP: 0 / " + str(self.maxHealth))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user