Пример #1
0
	def update(self, delta):
		if random.random() < .1 and self.state != STATE_DEAD:
			self.canseeplayer = len(physics.collisions(self.game.space, self.body.position, self.game.player.body.position, COLLISION_GROUND)) == 0

		if self.state != STATE_DEAD and self.health <= 0:
			self.game.player.enemies_killed += 1
			self.state = STATE_DEAD
		if self.state == STATE_WANDERING:
			self.body.velocity += pymunk.Vec2d(random.random()*10-5, random.random()*10-5)
			if self.canseeplayer:
				self.state = STATE_PREPARING
		if self.state == STATE_PREPARING:
			self.facing = self.body.position.x<self.game.player.body.position.x
			playerdist = (self.body.position-self.game.player.body.position).length
			if playerdist < self.range*2:
				self.attack_timer += delta
			if self.attack_timer > self.attack_time:
				self.attack_timer = 0
				self.state = STATE_ATTACKING
			flip = -1 if playerdist < self.range else 1
			self.body.velocity += flip*(self.body.position-self.game.player.body.position).normalized()*-self.speed/10
			if not self.canseeplayer:
				self.state = STATE_WANDERING
		if self.state == STATE_ATTACKING:
			self.facing = self.body.position.x<self.game.player.body.position.x
			self.body.velocity += (self.body.position-self.game.player.body.position).normalized()*-self.speed/2
			if not self.canseeplayer:
				self.state = STATE_WANDERING
	
		if self.state != STATE_DEAD:
			self.body.velocity = physics.limit(self.body.velocity, self.speed)
Пример #2
0
	def update(self, delta):
		self.keyboard = pymunk.Vec2d(0, 0)
		if self.health > 0:
			if self.hunger > 100:
				self.hunger = 100
			if self.hunger < 0:
				self.health += self.hunger
				self.hunger = 0
			if self.health < self.maxhealth:
				self.health += self.hunger*.01*delta

			if game.get_mouse_down(3) and self.melee_attack == 0:
				vec = (pymunk.Vec2d(self.game.camera.get_mouse_pos())-self.body.position).normalized()*(self.size+30)
				colls = physics.collisions(self.game.space, self.body.position, self.body.position+vec, COLLISION_ENEMY)
				for coll in colls:
					coll.shape.body.enemy.hit(self.melee_damage, self, vec)
				self.melee_attack = self.melee_attack_speed
				self.chomp = 1
			if self.melee_attack > 0:
				self.melee_attack -= delta
			else:
				self.melee_attack = 0
			if game.get_mouse_down(1) and self.ranged_attack == 0:
				self.game.objects.append(Bullet(self.game, self.body.position, (pymunk.Vec2d(self.game.camera.get_mouse_pos())-self.body.position).normalized()*200, self.ranged_damage, self))
				self.ranged_attack = self.ranged_attack_speed
				self.chomp = 1
			if self.ranged_attack > 0:
				self.ranged_attack -= delta
			else:
				self.ranged_attack = 0
			if self.flap > 0:
				self.flap -= delta
			else:
				self.flap = 0
			if self.chomp > 0:
				self.chomp -= delta*3
			else:
				self.chomp = 0

			if game.get_key_down(pygame.K_a):
				self.facing = False
				self.keyboard.x -= 1
			if game.get_key_down(pygame.K_d):
				self.facing = True
				self.keyboard.x += 1
			if game.get_key_down(pygame.K_w):
				self.keyboard.y += 1
			if game.get_key_down(pygame.K_s):
				self.keyboard.y -= 1

			self.fallthrough = self.keyboard.y < 0.0
			self.hunger -= 2*delta;

			if self.grounded:
				self.jumps = self.maxjumps
			if self.remainingBoost > 0:
				self.remainingBoost -= delta
			else:
				self.remainingBoost = 0
			if game.get_key_pressed(pygame.K_w) and self.jumps > 0:
				jump_v = pow(2.0*self.jumpheight/2*GRAVITY, .5)
				#self.body.velocity += (self.groundNormal if self.groundNormal.length > 0 else pymunk.Vec2d(0, 1))*jump_v
				self.body.velocity = pymunk.Vec2d(0, jump_v)
				self.remainingBoost = self.jumpheight/jump_v
				self.jumps -= 1
				self.flap = 1

			self.distance_travelled += self.body.velocity.length*delta
			self.movin += self.body.velocity.x*delta
			self.time += delta
			if(self.distance_travelled > 1000):
				self.upgrade_movement += 1
				self.distance_travelled = 0
			if(self.enemies_killed > 5):
				self.upgrade_attack += 1
				self.enemies_killed = 0
			if(self.time > 30):
				self.upgrade_utility += 1
				self.time = 0