Advertisement
Limekys

LimekysSimpleCamera

Mar 28th, 2023
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Game Maker 5.97 KB | Source Code | 0 0
  1. //Simple Camera Manager by Limekys (require UsefulFunctions script) (This script has MIT Licence)
  2. #macro LIME_CAMERA_MANAGER_VERSION "2023.03.20"
  3. #macro LIME_CAMERA _LimeGetCamera()
  4.  
  5. function _LimeGetCamera() {
  6.     static Camera = function() constructor {
  7.         self.x = 0;
  8.         self.y = 0;
  9.         self.start_width = 1920;
  10.         self.start_height = 1080;
  11.         self.width = 1920;
  12.         self.height = 1080;
  13.         self.target_object = noone;
  14.         self.x_offset = 0;
  15.         self.y_offset = 0;
  16.         self.width_half = floor(self.width / 2);
  17.         self.height_half = floor(self.height / 2);
  18.         self.x1 = self.x - self.width_half;
  19.         self.y1 = self.y - self.height_half;
  20.         self.x2 = self.x + self.width_half;
  21.         self.y2 = self.y + self.height_half;
  22.         self.view_index = 0;
  23.         self.camera_view = view_camera[self.view_index];
  24.         self.smoothness = 8;
  25.         self.max_distance = 512;
  26.        
  27.         self.debug_enabled = false;
  28.        
  29.         //Shake
  30.         self.shake = 0;
  31.         self.shake_length = 0;
  32.         self.shake_length_max = 0;
  33.         self.shake_magnitude = 0;
  34.        
  35.         //Zoom
  36.         self.camera_zoom = 1.0;
  37.         self.camera_zoom_target = 1.0;
  38.        
  39.         //Debug camera view sizes
  40.         self.camera_width_offset = 1.0;
  41.         self.camera_height_offset = 1.0;
  42.        
  43.         ///@func Init(width, height, target_object)
  44.         static Init = function(width, height, target_object = undefined) {
  45.             self.start_width = width;
  46.             self.start_height = height;
  47.             self.width = width;
  48.             self.height = height;
  49.             self.width_half = floor(self.width / 2);
  50.             self.height_half = floor(self.height / 2);
  51.             self.target_object = target_object;
  52.             self.camera_zoom = 1.0;
  53.             self.camera_zoom_target = 1.0;
  54.            
  55.             camera_set_view_size(self.camera_view, self.width, self.height);
  56.             view_set_wport(self.view_index, self.width);
  57.             view_set_hport(self.view_index, self.height);
  58.            
  59.             return self;
  60.         }
  61.        
  62.         static Update = function() {
  63.            
  64.             //Smoothnest zoom
  65.             if self.camera_zoom != self.camera_zoom_target {
  66.                 self.camera_zoom = clamp(SmoothApproachDelta(self.camera_zoom, self.camera_zoom_target, 4), 0.1, 2.0);
  67.                
  68.                 self.width = self.start_width / self.camera_zoom;
  69.                 self.height = self.start_height / self.camera_zoom;
  70.                
  71.                 self.width_half = floor(self.width / 2);
  72.                 self.height_half = floor(self.height / 2);
  73.                
  74.                 camera_set_view_size(self.camera_view, self.width, self.height);
  75.                 view_set_wport(self.view_index, self.width);
  76.                 view_set_hport(self.view_index, self.height);
  77.             }
  78.            
  79.             //Follow target object
  80.             if self.target_object != undefined && instance_exists(self.target_object) {
  81.                 self.x = SmoothApproachDelta(self.x, self.target_object.x + self.x_offset, self.smoothness, 0);
  82.                 self.y = SmoothApproachDelta(self.y, self.target_object.y + self.y_offset, self.smoothness, 0);
  83.                 //Clamp max distance from target object
  84.                 if point_distance(self.x, self.y, self.target_object.x, self.target_object.y) > self.max_distance {
  85.                     var _dir = point_direction(self.target_object.x, self.target_object.y, self.x, self.y);
  86.                     self.x = self.target_object.x + lengthdir_x(self.max_distance, _dir);
  87.                     self.y = self.target_object.y + lengthdir_y(self.max_distance, _dir);
  88.                 }
  89.             }
  90.            
  91.             //Shaking
  92.             if self.shake > 0 {
  93.                 self.x += random_range(-self.shake, self.shake);
  94.                 self.y += random_range(-self.shake, self.shake);
  95.                 self.shake = self.shake_magnitude * (self.shake_length / self.shake_length_max);
  96.                 self.shake_length -= DT;
  97.             }
  98.            
  99.             //Update view camera
  100.             camera_set_view_pos(self.camera_view, self.x - self.width_half, self.y - self.height_half);
  101.            
  102.             //Update camera vars position
  103.             if self.debug_enabled {
  104.                 var _test_w = camera_get_view_width(self.camera_view);
  105.                 var _test_h = camera_get_view_height(self.camera_view);
  106.                 self.x1 = self.x - _test_w*0.5;
  107.                 self.y1 = self.y - _test_h*0.5;
  108.                 self.x2 = self.x1 + _test_w;
  109.                 self.y2 = self.y1 + _test_h;
  110.             } else {
  111.                 self.x1 = self.x - self.width_half;
  112.                 self.y1 = self.y - self.height_half;
  113.                 self.x2 = self.x + self.width_half;
  114.                 self.y2 = self.y + self.height_half;
  115.             }
  116.            
  117.             //Debug
  118.             if keyboard_check_pressed(vk_numpad6) self.camera_width_offset += 0.1;
  119.             if keyboard_check_pressed(vk_numpad4) self.camera_width_offset -= 0.1;
  120.             if keyboard_check_pressed(vk_numpad8) self.camera_height_offset += 0.1;
  121.             if keyboard_check_pressed(vk_numpad2) self.camera_height_offset -= 0.1;
  122.            
  123.             if keyboard_check_pressed(vk_pageup) self.camera_zoom_target += 0.1;
  124.             if keyboard_check_pressed(vk_pagedown) self.camera_zoom_target -= 0.1;
  125.         }
  126.        
  127.         ///@func SetViewSize(scale)
  128.         static SetViewSize = function(scale) {
  129.             self.camera_zoom_target = scale;
  130.             return self;
  131.         }
  132.        
  133.         ///@func SetPosition(x, y)
  134.         static SetPosition = function(x, y) {
  135.             self.x = x;
  136.             self.y = y;
  137.             return self;
  138.         }
  139.        
  140.         ///@func SetOffset(x_offset, y_offset)
  141.         static SetOffset = function(x_offset, y_offset) {
  142.             self.x_offset = x_offset;
  143.             self.y_offset = y_offset;
  144.             return self;
  145.         }
  146.        
  147.         ///@func Shake(magnitude,seconds)
  148.         static Shake = function(magnitude, seconds) {
  149.             if (magnitude > self.shake) {
  150.                 self.shake = magnitude;
  151.                 self.shake_magnitude = magnitude;
  152.                 self.shake_length = seconds;
  153.                 self.shake_length_max = seconds;
  154.             }
  155.             return self;
  156.         }
  157.        
  158.         ///@func SetSmoothness(value = 8)
  159.         static SetSmoothness = function(value = 8) {
  160.             self.smoothness = value;
  161.             return self;
  162.         }
  163.        
  164.         ///@func SetMaxDistance(value = 8)
  165.         static SetMaxDistance = function(value = 512) {
  166.             self.max_distance = value;
  167.             return self;
  168.         }
  169.     }
  170.     static inst = new Camera();
  171.     return inst;
  172. }
  173.  
  174. ///@func ObjectInView([offset])
  175. function ObjectInView(offset = 32) {
  176.     return (bbox_right+offset > LIME_CAMERA.x1
  177.     && bbox_left-offset < LIME_CAMERA.x2
  178.     && bbox_bottom+offset > LIME_CAMERA.y1
  179.     && bbox_top-offset < LIME_CAMERA.y2)
  180. }
  181.  
  182. ///@func CheckInView(x1, y1, x2, y2, [offset])
  183. function CheckInView(x1, y1, x2, y2, offset = 0) {
  184.     return (x1 - offset <= LIME_CAMERA.x2
  185.     && x2 + offset >= LIME_CAMERA.x1
  186.     && y1 - offset <= LIME_CAMERA.y2
  187.     && y2 + offset >= LIME_CAMERA.y1)
  188. }
Tags: Game Maker
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement