Skip to content

Commit

Permalink
refactor to use structs and prevent extra GetProperties at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Oct 1, 2023
1 parent 1ca0a9f commit eeca6cd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
67 changes: 47 additions & 20 deletions rellax_demo/rellax.asc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Rellax
// 0.3.1
// 0.3.2
// A module to provide smooth scrolling and parallax!
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Before starting, you must create the following Custom Properties
Expand Down Expand Up @@ -69,11 +69,18 @@ Character* _TargetCharacter;
Vec2* _TargetPos;

// Parallax objects
Object* _pxo[MAX_PARALLAX_OBJS];
int _pxoRoomStartX[MAX_PARALLAX_OBJS];
int _pxoRoomStartY[MAX_PARALLAX_OBJS];
int _pxoOriginX[MAX_PARALLAX_OBJS];
int _pxoOriginY[MAX_PARALLAX_OBJS];
struct ParallaxObject {
Object* Object;
int RoomStartX;
int RoomStartY;
int ObjectOriginX;
int ObjectOriginY;
bool HasParallax;
float ParallaxX;
float ParallaxY;
};

ParallaxObject _pxo[MAX_PARALLAX_OBJS];
int _pxo_count;

// Tween Stuff
Expand Down Expand Up @@ -429,12 +436,12 @@ void _r_do_object_parallax(){
int camy = FloatToInt(_next_cam_pos.y);

for(int i=0; i<_pxo_count; i++){
if(_pxo[i].GetProperty("PxPos") !=0 || _pxo[i].GetProperty("PyPos") != 0) {
float parallax_x = IntToFloat(_pxo[i].GetProperty("PxPos"))/100.0;
float parallax_y = IntToFloat(_pxo[i].GetProperty("PyPos"))/100.0;
if(_pxo[i].HasParallax) {
float parallax_x = _pxo[i].ParallaxX;
float parallax_y = _pxo[i].ParallaxY;

_pxo[i].X=_pxoOriginX[i]+FloatToInt(IntToFloat(camx)*parallax_x);
_pxo[i].Y=_pxoOriginY[i]+FloatToInt(IntToFloat(camy)*parallax_y);
_pxo[i].Object.X = _pxo[i].ObjectOriginX + FloatToInt(IntToFloat(camx)*parallax_x);
_pxo[i].Object.Y = _pxo[i].ObjectOriginY + FloatToInt(IntToFloat(camy)*parallax_y);
}
}
}
Expand Down Expand Up @@ -676,7 +683,7 @@ void set_TargetCharacter(this Rellax*, Character* target)

Character* get_TargetCharacter(this Rellax*)
{
return _TargetCharacter;
return _TargetCharacter;
}

void set_EasingType(this Rellax*, RellaxTweenEasingType value)
Expand Down Expand Up @@ -830,24 +837,44 @@ void doSetOrigins ()
float room_h = IntToFloat(Room.Height);

for(int i=0; i<Room.ObjectCount; i++){
if (object[i].GetProperty("PxPos")!=0) {
bool has_parallax = object[i].GetProperty("PxPos")!=0 || object[i].GetProperty("PyPos")!=0;

if (has_parallax) {
_pxo[_pxo_count].HasParallax = has_parallax;
// Store the object in the parallax object array
_pxo[_pxo_count] = object[i];
_pxo[_pxo_count].Object = object[i];
// Get the parallax values for the object, and it's position
float parallax_x = IntToFloat(object[i].GetProperty("PxPos")) / 100.0;
float parallax_y = IntToFloat(object[i].GetProperty("PyPos")) / 100.0;
float obj_x = IntToFloat(object[i].X);
float obj_y = IntToFloat(object[i].Y);

// store pre-calculated parallax_x and y
_pxo[_pxo_count].ParallaxX = parallax_x;
_pxo[_pxo_count].ParallaxY = parallax_y;

// Store the object's initial position for resetting later
_pxoRoomStartX[_pxo_count] = object[i].X;
_pxoRoomStartY[_pxo_count] = object[i].Y;
_pxo[_pxo_count].RoomStartX = object[i].X;
_pxo[_pxo_count].RoomStartY = object[i].Y;

// Calculate the origin position for the object
_pxoOriginX[_pxo_count] = object[i].X - FloatToInt(parallax_x * obj_x * (room_w - cam_w) / room_w);
_pxoOriginY[_pxo_count] = object[i].Y - FloatToInt(parallax_y * obj_y * (room_h - cam_h) / room_h);
_pxo[_pxo_count].ObjectOriginX = object[i].X - FloatToInt(parallax_x * obj_x * (room_w - cam_w) / room_w);
_pxo[_pxo_count].ObjectOriginY = object[i].Y - FloatToInt(parallax_y * obj_y * (room_h - cam_h) / room_h);

if(_pxo_count<MAX_PARALLAX_OBJS) _pxo_count++;
}
}

for(int i=_pxo_count; i<MAX_PARALLAX_OBJS; i++){
_pxo[i].Object = null;
_pxo[i].HasParallax = false;
_pxo[i].ParallaxX = 0.0;
_pxo[i].ParallaxY = 0.0;
_pxo[i].RoomStartX = 0;
_pxo[i].RoomStartY = 0;
_pxo[i].ObjectOriginX = 0;
_pxo[i].ObjectOriginY = 0;
}
// Apply the parallax effect to the objects
_r_do_object_parallax();
}
Expand Down Expand Up @@ -880,8 +907,8 @@ function on_event (EventType event, int data){
{ // player exits any room
// reset the parallax object positions
for(int i=0; i<_pxo_count; i++){
_pxo[i].X=_pxoRoomStartX[i];
_pxo[i].Y=_pxoRoomStartY[i];
_pxo[i].Object.X = _pxo[i].RoomStartX;
_pxo[i].Object.Y = _pxo[i].RoomStartY;
}
_roomSetupDone = false;
}
Expand Down
2 changes: 1 addition & 1 deletion rellax_demo/rellax.ash
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Rellax
// 0.3.1
// 0.3.2
// A module to provide smooth scrolling and parallax!
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Before starting, you must create the following Custom Properties
Expand Down

0 comments on commit eeca6cd

Please sign in to comment.