97 lines
3.6 KiB
Python
97 lines
3.6 KiB
Python
import pygame
|
|
import random
|
|
from scene import SceneObject, Vector3
|
|
from tractor import Waypoint
|
|
|
|
class Terrain:
|
|
flags = []
|
|
|
|
def __init__(self, width, height, w_width, w_height):
|
|
self.width = width
|
|
self.height = height
|
|
self.w_width = w_width
|
|
self.w_height = w_height
|
|
self.resolution = w_width / width
|
|
# quality of the soil
|
|
self.soilmap = [[100]*self.width for i in range(self.height)]
|
|
self.colliders = [[0]*self.width for i in range(self.height)]
|
|
self.set_collider(5, 1, 1)
|
|
self.set_collider(5, 2, 1)
|
|
self.set_collider(5, 3, 1)
|
|
self.set_collider(7, 7, 1)
|
|
self.set_collider(7, 8, 1)
|
|
self.set_collider(7, 9, 1)
|
|
self.set_collider(7, 10, 1)
|
|
|
|
flag_img = pygame.transform.scale(pygame.image.load(
|
|
'resources/sprites/flag_red.png'), (50, 50))
|
|
|
|
def set_collider(self, x, y, value):
|
|
self.colliders[x][y] = value
|
|
|
|
def get_point_neighbours(self, point):
|
|
result = []
|
|
if type(point) != Waypoint:
|
|
return None
|
|
if point.x + 1 < self.width and self.colliders[point.x + 1][point.y] == 0:
|
|
result.append(Waypoint(point.x + 1, point.y))
|
|
if point.y - 1 >= 0 and self.colliders[point.x][point.y - 1] == 0:
|
|
result.append(Waypoint(point.x, point.y - 1))
|
|
if point.x - 1 >= 0 and self.colliders[point.x - 1][point.y] == 0:
|
|
result.append(Waypoint(point.x - 1, point.y))
|
|
if point.y + 1 < self.height and self.colliders[point.x][point.y + 1] == 0:
|
|
result.append(Waypoint(point.x, point.y + 1))
|
|
|
|
return result
|
|
|
|
def set_soil(self):
|
|
bad_spots_count = random.randint(2, 5)
|
|
padding = 3
|
|
for i in range(bad_spots_count):
|
|
x, y = random.randint(
|
|
padding, self.width-1-padding), random.randint(padding, self.height-1-padding)
|
|
self.soilmap[x][y] = 50
|
|
pos = Vector3(x/self.width*self.w_width, y /
|
|
self.height*self.w_height, 0)
|
|
flag = SceneObject(name="flag "+str(x) + ", "+str(y),
|
|
show_in_inspector=True, img=self.flag_img)
|
|
flag.set_pos(pos)
|
|
self.flags.append(flag)
|
|
return
|
|
for i in self.soilmap:
|
|
for j in i:
|
|
i = 100
|
|
tile_size = 120
|
|
|
|
def res_to_pos(self, x, y):
|
|
return int(x/self.w_width * self.width), int(y/self.w_height * self.height)
|
|
|
|
def pos_to_res(self, x, y):
|
|
return x/self.width * self.w_width, y/self.height * self.w_height
|
|
|
|
ground_img = pygame.transform.scale(pygame.image.load(
|
|
'resources/textures/dirt.jpg'), (tile_size, tile_size))
|
|
|
|
def render(self, SCREEN):
|
|
ground_tile = self.ground_img
|
|
for i in range(10):
|
|
for j in range(10):
|
|
SCREEN.blit(ground_tile, (i*self.tile_size, j*self.tile_size))
|
|
self.render_flags(SCREEN)
|
|
|
|
def render_flags(self, SCREEN):
|
|
img = pygame.transform.scale(pygame.image.load(
|
|
'resources/sprites/flag_red.png'), (50, 50))
|
|
spot = pygame.transform.scale(pygame.image.load(
|
|
'resources/sprites/spot.png'), (150, 150))
|
|
|
|
for i in range(len(self.flags)):
|
|
SCREEN.blit(spot, (self.flags[i].pos.x-75, self.flags[i].pos.y-75))
|
|
if self.flags[i].is_visible:
|
|
SCREEN.blit(img, (self.flags[i].pos.x, self.flags[i].pos.y))
|
|
|
|
def raycast_tile(self, x, y):
|
|
new_x = int(x / self.w_width * self.width)
|
|
new_y = int(y / self.w_height * self.height)
|
|
return new_x, new_y, self.soilmap[new_x][new_y]
|