added moveFwd() and rotate() to agent
This commit is contained in:
parent
3917fbf26d
commit
d77f74ca59
32
agent2.py
32
agent2.py
@ -1,9 +1,11 @@
|
||||
from mesa import Agent
|
||||
import random
|
||||
|
||||
|
||||
def dice(number):
|
||||
return random.randint(1, number)
|
||||
|
||||
|
||||
class Wall(Agent):
|
||||
def __init__(self, unique_id, model):
|
||||
super().__init__(unique_id, model)
|
||||
@ -11,6 +13,7 @@ class Wall(Agent):
|
||||
def step(self):
|
||||
pass
|
||||
|
||||
|
||||
class Box(Agent):
|
||||
def __init__(self, unique_id, model):
|
||||
super().__init__(unique_id, model)
|
||||
@ -21,18 +24,21 @@ class Box(Agent):
|
||||
def step(self):
|
||||
pass
|
||||
|
||||
|
||||
class Weapon():
|
||||
def __init__(self, name, type, damage):
|
||||
self.name = name
|
||||
self.type = type
|
||||
self.damage = damage
|
||||
|
||||
|
||||
class Armor():
|
||||
def __init__(self, name, defence, mp):
|
||||
self.name = name
|
||||
self.defence = defence
|
||||
self.mag_protection = mp
|
||||
|
||||
|
||||
class Creature(Agent):
|
||||
def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g):
|
||||
super().__init__(unique_id, model)
|
||||
@ -48,7 +54,6 @@ class Creature(Agent):
|
||||
self.isBox = False
|
||||
self.isCreature = True
|
||||
|
||||
|
||||
def meleeAttack(self, opponent):
|
||||
attackValue = self.strength + dice(6)
|
||||
defenseValue = opponent.strength + opponent.armor.defence
|
||||
@ -78,6 +83,7 @@ class Creature(Agent):
|
||||
else:
|
||||
self.magicAttack(opponent)
|
||||
|
||||
|
||||
class Player(Creature):
|
||||
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)
|
||||
@ -94,8 +100,26 @@ class Player(Creature):
|
||||
self.armor = arm
|
||||
self.isBox = 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(
|
||||
self.pos,
|
||||
moore=True,
|
||||
@ -205,7 +229,8 @@ class Player(Creature):
|
||||
|
||||
def step(self):
|
||||
if self.health > 0:
|
||||
self.move()
|
||||
self.moveFwd()
|
||||
self.rotate(False)
|
||||
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
||||
if len(cellmates) > 1:
|
||||
if isinstance(cellmates[0], Box):
|
||||
@ -218,4 +243,3 @@ class Player(Creature):
|
||||
print("Gold: " + str(self.gold))
|
||||
else:
|
||||
print("HP: 0 / " + str(self.maxHealth))
|
||||
|
Loading…
Reference in New Issue
Block a user