Skip to content

Instantly share code, notes, and snippets.

@joshuaskelly
Last active November 23, 2022 20:59
Show Gist options
  • Save joshuaskelly/15fe10fbaaa1bf87b341cba6e3ad2ebc to your computer and use it in GitHub Desktop.
Save joshuaskelly/15fe10fbaaa1bf87b341cba6e3ad2ebc to your computer and use it in GitHub Desktop.
A QuakeC misc_model Implementation
/*
* MIT License
*
* Copyright (c) 2022 Joshua Skelton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* math.qc
*
* Author: Joshua Skelton [email protected]
*
* A collection of helpful math functions.
*/
// Forward declarations
float(float value, float minValue, float maxValue) clamp;
float(float a, float b) mod;
float(float x) sign;
float(float value, float minValue, float maxValue) wrap;
/*
* clamp
*
* Limits the given value to the given range.
*
* value: A number
*
* minValue: The minimum value of the range
*
* maxValue: The maximum value of the range
*
* Returns: A number within the given range.
*/
float(float value, float minValue, float maxValue) clamp = {
if (value < minValue) {
return minValue;
}
else if (value > maxValue) {
return maxValue;
}
return value;
};
/*
* mod
*
* Returns the remainder after the division of a by n
*
* a: The dividend
*
* b: The divisor
*
* Returns: The remainder of a divided by n
*/
float(float a, float n) mod = {
return a - (n * floor(a / n));
};
/*
* sign
*
* Returns an indication of the sign of the given number.
*
* x: A number
*
* Returns: -1 if x < 0, 0 if x == 0, 1 if x > 0.
*/
float(float x) sign = {
if (x > 0) {
return 1;
}
else if (x < 0) {
return -1;
}
return 0;
};
/*
* wrap
*
* Limits the given value to the given range and will wrap the value to the
* the other end of the range if exceeded.
*
* value: A number
*
* minValue: The minimum value of the range
*
* maxValue: The maximum value of the range
*
* Returns: A number within the given range.
*/
float(float value, float minValue, float maxValue) wrap = {
local float range = maxValue - minValue;
return mod(value - minValue, range + 1) + minValue;
};
/*
* MIT License
*
* Copyright (c) 2022 Joshua Skelton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* misc_model.qc
*
* Author: Joshua Skelton [email protected]
*/
// The starting frame of the animation
.float first_frame;
// The ending frame of the animation
.float last_frame;
// Forward declarations
void() misc_model_think;
/*
* misc_model
*
* An entity for displaying models. A frame range can be given to animate the
* model.
*
* mdl: The model to display. Can be of type mdl, bsp, or spr.
*
* frame: The frame to display. Can be used to offset the animation.
*
* first_frame: The starting frame of the animation.
*
* last_frame: The last frame of the animation.
*/
void() misc_model = {
precache_model(self.mdl);
setmodel(self, self.mdl);
if (!self.frame) {
self.frame = self.first_frame;
}
// Only animate if given a frame range
if (!self.last_frame) {
makestatic(self);
return;
}
// Default animation speed to 10 fps
if (!self.speed) {
self.speed = 10 / 60;
}
self.nextthink = time + self.speed;
self.think = misc_model_think;
};
/*
* misc_model_think
*
* Handles animation for misc_model entity.
*/
void() misc_model_think = {
self.nextthink = time + fabs(self.speed);
self.frame = self.frame + sign(self.speed);
self.frame = wrap(self.frame, self.first_frame, self.last_frame);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment