Skip to content

Commit

Permalink
Abstract overmap:
Browse files Browse the repository at this point in the history
- Finished HTTP server
- New class object
- Processing system
- Graviation system
- Base camere framework
  • Loading branch information
TeshariEnjoer committed Aug 10, 2024
1 parent 6b03836 commit ae04995
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 103 deletions.
Binary file added laplas/abstract_map/assets/object.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions laplas/abstract_map/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BLACK = (0, 0, 0)
GRAY = (127, 127, 127)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
5 changes: 5 additions & 0 deletions laplas/abstract_map/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
URL = "http://127.0.0.1"
PORT = 5000
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 800
FPS = 60
1 change: 1 addition & 0 deletions laplas/abstract_map/globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROCESSING_OBJECTS = list()
77 changes: 41 additions & 36 deletions laplas/abstract_map/objects/camera.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
import pygame
from objects.processing import processing
import colors

class overmap_view (processing):

def __init__(self, screen, surface, new_x, new_y, width, height) -> None:
super().__init__()


self.width = width
self.height = height


class camera:
def __init__(self, screen, surface, new_x, new_y) -> None:
self.width = 800
self.height = 600
self.dragging = False
self.last_mouse_x = 0
self.last_mouse_y = 0

self.x = new_x
self.y = new_y

self.screen = screen
self.render_surface = surface
self.camera_rect = pygame.Rect(self.x, self.y, self.width, self.height)

print(f"Camera initialized at ({self.x}, {self.y})") # Отладочный вывод

def process(self):
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.dragging = True
self.last_mouse_x, self.last_mouse_y = event.pos
print(f"Started dragging at ({self.last_mouse_x}, {self.last_mouse_y})")

if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.dragging = False
print("Stopped dragging")

if event.type == pygame.MOUSEMOTION:
if self.dragging:
mouse_x, mouse_y = event.pos
dx = mouse_x - self.last_mouse_x
dy = mouse_y - self.last_mouse_y
self.x -= dx
self.y -= dy
self.last_mouse_x, self.last_mouse_y = mouse_x, mouse_y

print(f"Dragging to ({self.x}, {self.y})")

self.camera_rect.topleft = (self.x, self.y)
self.screen.blit(self.render_surface, (0, 0), self.camera_rect)
print(f"Camera rect top-left at ({self.camera_rect.x}, {self.camera_rect.y})") # Отладочный вывод

self.camera_view = pygame.Rect(self.x, self.y, self.width, self.height)
print(f"Camera initialized at ({self.x}, {self.y})")

def __process__(self):
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_buttons = pygame.mouse.get_pressed()

if mouse_buttons[0]:
if not self.dragging:
# Начало перетаскивания
self.dragging = True
self.last_mouse_x = mouse_x
self.last_mouse_y = mouse_y
else:
dx = mouse_x - self.last_mouse_x
dy = mouse_y - self.last_mouse_y

self.camera_view.x -= dx
self.camera_view.y -= dy

self.last_mouse_x = mouse_x
self.last_mouse_y = mouse_y
else:
self.dragging = False

def __update__(self):
self.screen.fill((0, 0, 0))
self.screen.blit(self.render_surface, (0, 0), self.camera_view)
121 changes: 100 additions & 21 deletions laplas/abstract_map/objects/object.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,127 @@
import pygame
import os
from objects.processing import processing

class object:
DM_datum = "datum/overmap"
class object (processing):
def __init__(self, name, id, map, x, y, width, height, iconpath = "assets/obj.png"):
super().__init__()

def __init__(self, x, y, width, height, icon = ""):
self.iconpath = self.texture_path + icon
# Draw parametrs
self.original_texute = pygame.image.load(self.iconpath).convert_alpha()
self.texute = self.original_texute
self.rect = self.image.get_rect(center=(x, y))
self.rect.width = width
self.rect.height = height
self.name = name
self.id = id
self.DM_datum = "datum/overmap"
self.iconpath = iconpath
# Draw params:

# Physical parametr
self.rect = pygame.Rect(x, y, width, height)
self.original_texture = any
self.load_texture()

# Our current velocity
# Positional params:

self.x = x
self.y = y
self.map = map

print(f"Created new object {self.name}, with id {self.id}, with icon {self.iconpath}, on {self.x}: {self.y}")
# Physical params:

# Our current velocity.
self.velocity = pygame.Vector2(0, 0)
# A temporary acceleration that we get
# A temporary acceleration that we get.
self.acceleration = pygame.Vector2(0, 0)

# A gravitational object that affects us. E.t.c Planet, star, blackhole
self.gsource = None
self.gforce = None
# Our mass, effects on gravity forcess
self.mass = 1
# Our angle of inclination, relative to it will be set velocity.
self.angle = 0
# The speed at which we're spinning. Positive values are rotation to the right, negative values are rotation to the left.
self.rotation_speed = 0

self.font = pygame.font.SysFont(None, 12)

def destroy(self):
del self.original_texture
del self.image
del self.rect
del self

def load_texture(self):
current_directory = os.path.dirname(os.path.abspath(__file__))
parent = os.path.dirname(current_directory)

final_path = os.path.join(parent, self.iconpath)
final_path = final_path.replace("\\", "/")

self.image = pygame.image.load(final_path)
self.original_texture = self.image.copy()

def set_color(self, color):
self.image.fill(color)

# Sets new gravitation source
def set_gravity_source(self, gsource, gforce):
self.gsource = gsource
self.gforce = gforce


# Applied new temporary acceleration
def apply_force(self, force: pygame.Vector2):
self.acceleration += force
self.acceleration += force / self.mass


def apply_thrust(self, thrust):
direction = pygame.Vector2(1, 0).rotate(self.angle)
self.apply_force(direction * thrust)


def apply_brake(self, brake_force):
direction = pygame.Vector2(1, 0).rotate(self.angle)
self.apply_force(-direction * brake_force)

def update(self):
def rotate(self, force):
pass

def set_rotation(self, force):
pass

def draw_id(self):
text = f"{self.name}: {self.id} "
text += f"X: {self.x}, Y:{self.y}"
text_surface = self.font.render(text, True, (255, 255, 255))
text_rect = text_surface.get_rect(center=(self.rect.centerx, self.rect.bottom + 10))
self.map.blit(text_surface, text_rect)

def __process__(self):
if self.gsource:
distance = pygame.Vector2(self.gsource.rect.center) - pygame.Vector2(self.rect.center)
distance_length = distance.length()
if distance_length > 0:
gravitational_force = self.gforce * self.mass / (distance_length ** 2)
gravitational_acceleration = gravitational_force * distance.normalize()
self.apply_force(gravitational_acceleration)


self.velocity += self.acceleration
self.rect.center += pygame.Vector2(self.velocity.x, self.velocity.y)
self.rect.center += self.velocity
self.acceleration = pygame.Vector2(0, 0)

self.angle %= 360
self.image = pygame.transform.rotate(self.original_texute, -self.angle)
self.image = pygame.transform.rotate(self.original_texture, -self.angle)
self.rect = self.image.get_rect(center=self.rect.center)

def draw(self, screen):
pygame.draw.rect(screen, self.rect)
self.x = self.velocity.x
self.y = self.velocity.y
# Actually move
self.rect.move_ip(self.velocity)

def __update__(self):
self.draw_id()
self.map.blit(self.image, (self.x, self.y))

def spawn_object(name: str, id: int, map: pygame.surface, new_x: int, new_y: int, width: int, height: int, iconpath):
new_obj = object(name, id, map, new_x, new_y, width, height, iconpath)
return new_obj

def remove_object(obj: object):
obj.destroy()
16 changes: 16 additions & 0 deletions laplas/abstract_map/objects/processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import globals

class processing:
def __init__(self) -> None:
globals.PROCESSING_OBJECTS.append(self)

def __del__(self):
globals.PROCESSING_OBJECTS.remove(self)

# Update physics
def __process__(self):
pass

# Update visual state
def __update__(self):
pass
56 changes: 24 additions & 32 deletions laplas/abstract_map/overmap.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import pygame
from objects import object
from objects import camera

from objects.object import object
from objects.object import spawn_object, remove_object
from pygame.locals import *
import colors
import config
from objects.processing import processing
from objects.camera import overmap_view

camera_width, camera_height = 800, 600

class overmap:
class overmap (processing):
def __init__(self, size_x: int, size_y: int, visual: bool) -> None:
super().__init__()
# A map full size
self.size_x = size_x
self.size_y = size_y
Expand All @@ -19,45 +24,32 @@ def __init__(self, size_x: int, size_y: int, visual: bool) -> None:
self.all_planets = dict()

self.screen = pygame.display.set_mode((camera_width, camera_height))
self.create_map()
self.create_camera()

self.clock = pygame.time.Clock()
pygame.display.set_caption("Large Map with Coordinate System")
self.camera = camera
if(not self.create_map()):
print("Map creation failed")
self.camera = overmap_view

## Overmap functions

# Actually creates a non physical map, ans setups a cordinates system
def create_map(self):
self.map_holder = pygame.Surface((self.size_x, self.size_y))
self.map_holder.fill((25, 25, 25))

self.map_holder.fill(colors.BLACK)

def create_camera(self):
camera_x, camera_y = 1, 1
# self.camera = camera(self.map_holder, self.screen, camera_x, camera_y)
self.camera = overmap_view(self.screen, self.map_holder, 5000, 5000, 800, 600)
obj = spawn_object("Test object", "1", self.map_holder, 5000, 5000, 32, 32, "assets/object.png")
obj.apply_force(pygame.Vector2(1, 0))
return True

## Function for manipulate with objects
def create_object(self, obj, args):
pass
def create_object(self, name, id, x, y, path):
spawn_object("Test object", "1", self.map_holder, 5000, 5000, 32, 32, "assets/object.png")

def remove_object(self):
pass

def adjust_speed(self):
def destroy_object(self):
pass

def process(self):
# self.camera.process(self.camera)
self.clock.tick(60)

if(not self.all_object.__len__()):
return
for obj in self.all_object:
obj.process()

def __process__(self):
pass

def process_map(state: bool):
while(state):
overmap.process()
def __update__(self):
pass
Loading

0 comments on commit ae04995

Please sign in to comment.