Enforce world bounds for object positioning and update related tests
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Build Simulation and Test / Run All Tests (push) Successful in 30s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Build Simulation and Test / Run All Tests (push) Successful in 30s
				
			This commit is contained in:
		
							parent
							
								
									56415bb07a
								
							
						
					
					
						commit
						fe4e15eac1
					
				| @ -83,5 +83,9 @@ def test_tick_all_calls_tick(world): | |||||||
| 
 | 
 | ||||||
| def test_add_object_out_of_bounds(world): | def test_add_object_out_of_bounds(world): | ||||||
|     entity = DummyEntity(Position(x=1000, y=1000)) |     entity = DummyEntity(Position(x=1000, y=1000)) | ||||||
|     with pytest.raises(ValueError): | 
 | ||||||
|         world.add_object(entity) |     world.add_object(entity) | ||||||
|  | 
 | ||||||
|  |     entity = world.get_objects()[0] | ||||||
|  | 
 | ||||||
|  |     assert entity.position.x == 49 and entity.position.y == 49 | ||||||
|  | |||||||
| @ -151,4 +151,4 @@ class FoodObject(BaseEntity): | |||||||
| 
 | 
 | ||||||
|         :return: String representation. |         :return: String representation. | ||||||
|         """ |         """ | ||||||
|         return f"FoodObject({self.position}, decay={self.decay:.0f}, decay_rate={self.decay_rate * (1 + (self.neighbors / 10))})" |         return f"FoodObject({self.position}, decay={self.decay:.0f  }, decay_rate={self.decay_rate * (1 + (self.neighbors / 10))})" | ||||||
|  | |||||||
| @ -111,7 +111,9 @@ class World: | |||||||
|         # Ensure position is within world bounds, considering a center origin |         # Ensure position is within world bounds, considering a center origin | ||||||
|         if position.x < -self.world_size[0] / 2 or position.x >= self.world_size[0] / 2 or position.y < - \ |         if position.x < -self.world_size[0] / 2 or position.x >= self.world_size[0] / 2 or position.y < - \ | ||||||
|         self.world_size[1] / 2 or position.y >= self.world_size[1] / 2: |         self.world_size[1] / 2 or position.y >= self.world_size[1] / 2: | ||||||
|             raise ValueError(f"Position is out of world bounds. {position} is out of bounds.") |             # force position to be within bounds | ||||||
|  |             position.x = max(-self.world_size[0] / 2, min(position.x, self.world_size[0] / 2 - 1)) | ||||||
|  |             position.y = max(-self.world_size[1] / 2, min(position.y, self.world_size[1] / 2 - 1)) | ||||||
| 
 | 
 | ||||||
|         return int(position.x // self.partition_size), int(position.y // self.partition_size) |         return int(position.x // self.partition_size), int(position.y // self.partition_size) | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Sam
						Sam