Create Event
GML:
///Resolution Stats
#macro ResolutionScale 1
#macro ResolutionWidth 1024/ResolutionScale
#macro ResolutionHeight 768/ResolutionScale
///Movement
Object = oPlayer;
Smoothness = .1;
///Camera Shake Variables
Shake = 0;
ShakePower = 1;
ShakeRotation = 0;
ShakeSpeed = .1;
///Zoom Variables
ZoomSpeed = .02;
///Create Camera
#region <Create Camera>
//Enable views
view_enabled = true;
view_visible[0] = true;
//Create Camera
CameraID = camera_create_view(0,0,ResolutionWidth,ResolutionHeight);
view_set_camera(0,CameraID);
//Resize Window & Application Surface
window_set_size(ResolutionWidth*ResolutionScale,ResolutionHeight*ResolutionScale);
surface_resize(application_surface,ResolutionWidth*ResolutionScale,ResolutionHeight*ResolutionScale);
display_set_gui_size(ResolutionWidth,ResolutionHeight);
//Center Window
var _DisplayWidth = display_get_width();
var _DisplayHeight = display_get_height();
var _WindowWidth = ResolutionWidth*ResolutionScale;
var _WindowHeight = ResolutionHeight*ResolutionScale;
window_set_position(_DisplayWidth/2 - _WindowWidth/2,_DisplayHeight/2 - _WindowHeight/2);
#endregion
GML:
#region <Calculate Movement>
//Current Camera Position
var _xCurrent = camera_get_view_x(CameraID);
var _yCurrent = camera_get_view_y(CameraID);
var _CamWidth = camera_get_view_width(CameraID);
var _CamHeight = camera_get_view_height(CameraID);
//Set Target Position
var _xTarget = Object.x - _CamWidth/2;
var _yTarget = Object.y - _CamHeight/2;
//Clamp to Room Boundries
_xTarget = clamp(_xTarget,0,room_width - _CamWidth);
_yTarget = clamp(_yTarget,0,room_height - _CamHeight);
//Smooth Movement
_xCurrent = lerp(_xCurrent,_xTarget,Smoothness);
_yCurrent = lerp(_yCurrent,_yTarget,Smoothness);
#endregion
///Zoom
#region <Zoom>
var _ZoomInput = keyboard_check(vk_down) - keyboard_check(vk_up);
_ZoomInput *= ZoomSpeed;
if (_ZoomInput != 0) {
_CamWidth += _CamWidth*_ZoomInput;
_CamHeight += _CamHeight*_ZoomInput;
_xCurrent -= (_CamWidth*_ZoomInput)/2;
_yCurrent -= (_CamHeight*_ZoomInput)/2;
}
#endregion
///Camera Shake
#region <Apply Camera Shake>
var _Shake = power(Shake,2)*ShakePower;
_xCurrent += random_range(-_Shake,_Shake);
_yCurrent += random_range(-_Shake,_Shake);
if (Shake > 0) Shake -= ShakeSpeed;
#endregion
///Apply Camera Transformations
#region <Apply Camera Transformations>
camera_set_view_pos(CameraID,_xCurrent,_yCurrent);
camera_set_view_size(CameraID,_CamWidth,_CamHeight);
camera_set_view_angle(CameraID,random_range(-_Shake,_Shake)*ShakeRotation);
#endregion