Skip to content

Commit

Permalink
Abstract map updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TeshariEnjoer committed Aug 12, 2024
1 parent ae04995 commit e2334df
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 50 deletions.
2 changes: 1 addition & 1 deletion code/__DEFINES/laplas_defines/abstract_map.dm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define AM_RESPONSE_SUCESS "sucess"
#define AM_RESPONSE_FAILED "failed"


#define ABSTRACT_MAP_PING "/"
#define ABSTRACT_MAP_SET_KEY "set_key"
#define ABSTRACT_MAP_RESET_KEY "reset_key"
#define ABSTRACT_MAP_INIT "init"
Binary file added laplas/abstract_map/assets/planet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added laplas/abstract_map/assets/ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions laplas/abstract_map/globals.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
import math

VISUALISED_OBJECTS = list()
PROCESSING_OBJECTS = list()

VISUALISED = True
CAMERA = any

def get_distance(first_object, second_object):
return math.sqrt((first_object.x - second_object.x) ** 2 + (first_object.y - second_object.y) ** 2)

def get_speed(object):
return round(math.sqrt(object.velocity.x ** 2 + object.velocity.y ** 2), 1)
26 changes: 15 additions & 11 deletions laplas/abstract_map/objects/camera.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
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__()
import globals
from objects.processing import Iprocessing
from globals import PROCESSING_OBJECTS


class camera (Iprocessing):
def __init__(self, screen, new_x, new_y, width, height) -> None:
self.width = width
self.height = height


self.dragging = False
self.last_mouse_x = 0
self.last_mouse_y = 0
self.zoom_level = 1.0
self.x = new_x
self.y = new_y

self.screen = screen
self.render_surface = surface

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


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

if mouse_buttons[0]:
if not self.dragging:
# Начало перетаскивания
self.dragging = True
self.last_mouse_x = mouse_x
self.last_mouse_y = mouse_y
Expand All @@ -46,6 +46,10 @@ def __process__(self):
else:
self.dragging = False

def __update__(self):
def draw(self):
self.screen.fill((0, 0, 0))
self.screen.blit(self.render_surface, (0, 0), self.camera_view)
rendered_surface = pygame.Surface((self.camera_view.x + self.width, self.camera_view.y + self.height))
for obj in globals.PROCESSING_OBJECTS:
obj.__update__(rendered_surface)
self.screen.blit(rendered_surface, (0, 0), self.camera_view)
del rendered_surface
76 changes: 76 additions & 0 deletions laplas/abstract_map/objects/gravitational.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from objects.object import IgravitationWell, object
import globals
from pygame.locals import *
import math
import pygame

class grivitational_oject (object, IgravitationWell):
def __init__(self, name, id, map, x, y, width, height, iconpath="assets/obj.png"):
object.__init__(self, name, id, map, x, y, width, height, iconpath)
IgravitationWell.__init__(self)

self.G = 1
self.gravitation_radius = 500
self.critical_orbit = 200
self.static = True

def set_gravitaion_forces(self, new_g, new_gradius, critical):
self.G = new_g
self.gravitation_radius = new_gradius
self.critical_orbit = critical

def __process__(self):
super().__process__()
self.__gravityCapture__()
if(not self.orbiting_objects.__len__):
return

for obj in self.orbiting_objects:
self.set_stable_orbit(obj)

def set_stable_orbit(self, obj: object):
dx = obj.rect.centerx - self.rect.centerx
dy = obj.rect.centery - self.rect.centery
distance = math.sqrt(dx**2 + dy**2)

if distance == 1e-6:
print(f"Ojbect {obj.name} - {obj.id}, crashed intro {self.name} - {self.id}")
return

velocity_magnitude = math.sqrt(self.G * self.mass / distance)

if distance < self.critical_orbit:
angle = math.atan2(dy, dx)
velocity_x = -velocity_magnitude * math.sin(angle)
velocity_y = velocity_magnitude * math.cos(angle)
obj.velocity = pygame.math.Vector2(velocity_x, velocity_y)
# elif self.can_escape(obj):
# # Apply acceleration towards the gravitational source
# direction = pygame.math.Vector2(-dx, -dy).normalize()
# acceleration = self.G / obj.mass
# obj.apply_force(direction * acceleration)

def can_escape(self, object : object) -> bool:
if(globals.get_speed(object) > self.G):
return True
return False

def __gravityCapture__(self):
for obj in self.map.all_objects:
obj = self.map.all_objects[obj]
if globals.get_distance(self, obj) >= self.gravitation_radius or obj.static:
continue
if obj.mass >= 10 and (not self.can_escape(obj)):
self.orbiting_objects.append(obj)
obj.set_gravity_source(self, self.G)


def spawn_gsource(name: str,
id: int,
map,
new_x: int, new_y: int,
width: int, height: int,
iconpath, G, radius, critical):
new_obj = grivitational_oject(name, id, map, new_x, new_y, width, height, iconpath)
new_obj.set_gravitaion_forces(G, radius, critical)
return new_obj
97 changes: 81 additions & 16 deletions laplas/abstract_map/objects/object.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import pygame
import os
from objects.processing import processing
from objects.processing import Iprocessing, Ivisualised
import math

class object (Iprocessing, Ivisualised, pygame.sprite.Sprite):
MINIMUM_SPEED = 0.5
MAXIMUM_SPEED = 5

class object (processing):
def __init__(self, name, id, map, x, y, width, height, iconpath = "assets/obj.png"):
super().__init__()
Iprocessing.__init__(self)
Ivisualised.__init__(self)
pygame.sprite.Sprite.__init__(self)

self.name = name
self.id = id
Expand All @@ -25,6 +31,11 @@ def __init__(self, name, id, map, x, y, width, height, iconpath = "assets/obj.pn
print(f"Created new object {self.name}, with id {self.id}, with icon {self.iconpath}, on {self.x}: {self.y}")
# Physical params:

# Should we process physics parametrs
self.static = False
# Once how many ticks this object is allowed to move
self.movement_colldown = 60
self.movement_tick = 0
# Our current velocity.
self.velocity = pygame.Vector2(0, 0)
# A temporary acceleration that we get.
Expand Down Expand Up @@ -56,6 +67,20 @@ def load_texture(self):

self.image = pygame.image.load(final_path)
self.original_texture = self.image.copy()
self.image = pygame.transform.scale(self.original_texture, (self.rect.width, self.rect.height))

def set_DMdatum(self, datum: str):
self.DM_datum = datum

def get_summary(self) -> str:
summary += ""
summary += f"id={self.id},"
summary += f"name={self.name},"
summary += f"x={self.x},"
summary += f"y={self.y},"
summary += f"angle={self.angle}"
summary += f"rotatation={self.rotation_speed}"
return summary

def set_color(self, color):
self.image.fill(color)
Expand All @@ -65,6 +90,11 @@ def set_gravity_source(self, gsource, gforce):
self.gsource = gsource
self.gforce = gforce

def set_mass(self, new_mass):
if new_mass <= 0:
self.mass = 1
return
self.mass = new_mass

def apply_force(self, force: pygame.Vector2):
self.acceleration += force / self.mass
Expand All @@ -79,20 +109,30 @@ def apply_brake(self, brake_force):
direction = pygame.Vector2(1, 0).rotate(self.angle)
self.apply_force(-direction * brake_force)

def set_rotation(self, force):
self.angle += force

def rotate(self, force):
pass
self.rotation_speed += force

def set_rotation(self, force):
pass
def limit_velocity(self):
if self.velocity.length() > self.MAXIMUM_SPEED:
self.velocity.scale_to_length(self.MAXIMUM_SPEED)

def draw_id(self):
def draw_id(self, surface):
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)
surface.blit(text_surface, text_rect)

def __process__(self):
if self.static:
return

while self.movement_tick < self.movement_colldown:
self.movement_tick += 1

if self.gsource:
distance = pygame.Vector2(self.gsource.rect.center) - pygame.Vector2(self.rect.center)
distance_length = distance.length()
Expand All @@ -101,27 +141,52 @@ def __process__(self):
gravitational_acceleration = gravitational_force * distance.normalize()
self.apply_force(gravitational_acceleration)

if(self.rotation_speed):
self.angle += self.rotation_speed

self.velocity += self.acceleration
self.rect.center += self.velocity
angle_radians = math.radians(self.angle)
acceleration_rotated = pygame.Vector2(self.acceleration.x * math.cos(angle_radians) - self.acceleration.y * math.sin(angle_radians),
self.acceleration.x * math.sin(angle_radians) + self.acceleration.y * math.cos(angle_radians))
self.velocity += acceleration_rotated
self.acceleration = pygame.Vector2(0, 0)

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

self.x = self.velocity.x
self.y = self.velocity.y
self.limit_velocity()

self.x = self.rect.x
self.y = self.rect.y
# Actually move
self.rect.move_ip(self.velocity)
self.movement_tick = 0

def __update__(self):
self.draw_id()
self.map.blit(self.image, (self.x, self.y))
def __update__(self, surface: pygame.Surface):
if super().__draw__(self.rect):
self.draw_id(surface)
surface.blit(self.image, self.rect)

def spawn_object(name: str, id: int, map: pygame.surface, new_x: int, new_y: int, width: int, height: int, iconpath):
def spawn_object(name: str, id: int, map, 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()


class IgravitationWell:
def __init__(self) -> None:
self.G = 1
self.gravitation_radius = 0
self.orbiting_objects = list()
pass

def __gravityCapture__(self):
pass

def set_stable_orbit(self, object):
pass

def can_escape(self, object):
pass
20 changes: 18 additions & 2 deletions laplas/abstract_map/objects/processing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import globals

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

Expand All @@ -11,6 +11,22 @@ def __del__(self):
def __process__(self):
pass


from objects.camera import camera
import pygame

class Ivisualised:
def __init__(self) -> None:
pass

def __del__(self):
pass

# Update visual state
def __update__(self):
def __update__(self, surface: pygame.Surface):
pass

def __draw__(self, our_rect: pygame.Rect):
if globals.CAMERA.camera_view.contains(our_rect):
return True
return False
11 changes: 11 additions & 0 deletions laplas/abstract_map/objects/ship.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from objects.object import object

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

def set_rotation(self, force):
self.angle += force

def rotate(self, force):
self.rotation_speed += force
Loading

0 comments on commit e2334df

Please sign in to comment.