Moved physics engines to Mob.

This commit is contained in:
Cameron Smart
2020-04-23 18:50:40 -07:00
parent 43ddaadc32
commit 127738743b
3 changed files with 16 additions and 17 deletions

View File

@@ -47,7 +47,7 @@ class Config(object):
DEBUG = False DEBUG = False
# Monster Count to be spawned # Monster Count to be spawned
MONSTER_COUNT = 30 MONSTER_COUNT = 6
class Enums(Enum): class Enums(Enum):

View File

@@ -54,10 +54,10 @@ class Game(arcade.Window):
# Game Objects # Game Objects
self.dungeon = None self.dungeon = None
self.prev_keypress = [] # A list that assists with tracking keypress events self.prev_keypress = [] # A list that assists with tracking keypress events
self.physics_engine = None # Our physics engine
# Used to keep track of our scrolling # Used to keep track of our scrolling
self.view_bottom = self.view_left = 0 self.view_bottom = self.view_left = 0
self.Recipe = [] self.Recipe = []
self.enemies_in_range = []
arcade.set_background_color(arcade.color.BLACK) arcade.set_background_color(arcade.color.BLACK)
@@ -80,11 +80,11 @@ class Game(arcade.Window):
level = random.choice(self.dungeon.levelList) level = random.choice(self.dungeon.levelList)
self.player.center_x, self.player.center_y = level.center() self.player.center_x, self.player.center_y = level.center()
self.player.cur_recipe = self.Recipe.active self.player.cur_recipe = self.Recipe.active
# x, y = level.center() self.player.monster_collisions = arcade.PhysicsEngineSimple(self.player, self.dungeon.getWalls())
# Set up monsters # Set up monsters
self.Mobs = MobHandler() self.Mobs = MobHandler()
self.enemy_list = self.Mobs.setup(0, 1, self.player, self.dungeon) self.enemy_list = self.Mobs.setup(Config.MONSTER_COUNT, Config.MONSTER_COUNT, self.player, self.dungeon)
self.active_enemies = self.Mobs.active_enemies self.active_enemies = self.Mobs.active_enemies
# Setup viewport # Setup viewport
@@ -95,9 +95,6 @@ class Game(arcade.Window):
self.view_bottom, self.view_bottom,
Config.SCREEN_HEIGHT + self.view_bottom) Config.SCREEN_HEIGHT + self.view_bottom)
# Create the 'physics engine'
self.physics_engine = arcade.PhysicsEngineSimple(self.player, self.dungeon.getWalls())
def on_draw(self): def on_draw(self):
""" Render the screen. """ """ Render the screen. """
try: try:
@@ -229,7 +226,7 @@ class Game(arcade.Window):
""" Movement and game logic """ """ Movement and game logic """
# Move the player with the physics engine # Move the player with the physics engine
self.physics_engine.update() self.player.monster_collisions.update()
self.player.update_animation() self.player.update_animation()
changed = False # Track if we need to change the viewport changed = False # Track if we need to change the viewport
@@ -267,6 +264,7 @@ class Game(arcade.Window):
Config.SCREEN_WIDTH + self.view_left, Config.SCREEN_WIDTH + self.view_left,
self.view_bottom, self.view_bottom,
Config.SCREEN_HEIGHT + self.view_bottom) Config.SCREEN_HEIGHT + self.view_bottom)
# Update Mobs
self.Mobs.update() self.Mobs.update()
# Projectile updates # Projectile updates

View File

@@ -34,6 +34,7 @@ class MobHandler(arcade.SpriteList):
mob.target = self.player mob.target = self.player
mob.scale = 4 mob.scale = 4
mob.monster_type = 'ghost' mob.monster_type = 'ghost'
mob.monster_collisions = arcade.PhysicsEngineSimple(mob, self.active_enemies)
self.enemy_list.append(mob) self.enemy_list.append(mob)
for count in range(frogs): for count in range(frogs):
mob = Enemy(filename="resources/images/monsters/frog/frog1.png", dungeon=self.dungeon) mob = Enemy(filename="resources/images/monsters/frog/frog1.png", dungeon=self.dungeon)
@@ -41,6 +42,7 @@ class MobHandler(arcade.SpriteList):
mob.target = self.player mob.target = self.player
mob.scale = 4 mob.scale = 4
mob.monster_type = 'frog' mob.monster_type = 'frog'
mob.monster_collisions = arcade.PhysicsEngineSimple(mob, self.active_enemies)
self.enemy_list.append(mob) self.enemy_list.append(mob)
return self.enemy_list return self.enemy_list
@@ -53,13 +55,13 @@ class MobHandler(arcade.SpriteList):
for enemy in reversed(self.enemy_list): for enemy in reversed(self.enemy_list):
# TODO replace with distance checking # TODO replace with distance checking
distance = self.get_distance(enemy) distance = self.get_distance(enemy)
if (distance < 500): if (distance < 300):
self.active_enemies.append(enemy) self.active_enemies.append(enemy)
self.enemy_list.remove(enemy) self.enemy_list.remove(enemy)
enemy.active = True
try: try:
for enemy in self.active_enemies: for enemy in self.active_enemies:
monster_collisions = arcade.PhysicsEngineSimple(enemy, self.active_enemies) enemy.monster_collisions.update()
monster_collisions.update()
path = enemy.get_path() path = enemy.get_path()
enemy.tick(path) enemy.tick(path)
except Exception: except Exception:
@@ -71,11 +73,8 @@ class MobHandler(arcade.SpriteList):
start_y = enemy.center_y start_y = enemy.center_y
end_x = self.player.center_x end_x = self.player.center_x
end_y = self.player.center_y end_y = self.player.center_y
try:
distance = math.sqrt(math.pow(start_x - end_x, 2) + math.pow(start_y - end_y, 2)) distance = math.sqrt(math.pow(start_x - end_x, 2) + math.pow(start_y - end_y, 2))
return distance return distance
except:
return 0
class Mob(arcade.Sprite): class Mob(arcade.Sprite):
@@ -94,7 +93,7 @@ class Mob(arcade.Sprite):
self.up_textures = [] self.up_textures = []
self.down_textures = [] self.down_textures = []
self.cur_texture = 0 self.cur_texture = 0
self.monster_collisions = None
self.dungeon = dungeon self.dungeon = dungeon
self.target = None self.target = None
self.collisions = None self.collisions = None
@@ -182,6 +181,7 @@ class Enemy(Mob):
def __init__(self, *args, **kwargs) -> None: def __init__(self, *args, **kwargs) -> None:
super(Enemy, self).__init__(*args, **kwargs) super(Enemy, self).__init__(*args, **kwargs)
self.monster_type = '' self.monster_type = ''
self.active = False
def nearestPosition(self) -> Tuple[int, int]: def nearestPosition(self) -> Tuple[int, int]:
""" """
@@ -196,6 +196,7 @@ class Enemy(Mob):
""" """
A on_update function, the Mob should decide it's next actions here. A on_update function, the Mob should decide it's next actions here.
""" """
curpos, nextpos = self.nearestPosition(), path[1] curpos, nextpos = self.nearestPosition(), path[1]
# print(curpos, nextpos) # print(curpos, nextpos)