Last active
November 21, 2022 03:12
-
-
Save zlrc/6261a487127c619c2229f93fd920061d to your computer and use it in GitHub Desktop.
A Godot Engine class that combines the CollisionPolygon2D and Polygon2D nodes together to allow textured collision polygons.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A CollisionPolygon2D node that supports textures and other Polygon2D-exclusive features. | |
# Compatible with Godot version 3.x | |
# Script Version: 0.1.0 | |
# Note(s): | |
# - For textures to appear in the editor, you must either reload the scene, | |
# or drag one of the vertices after the collision polygon has been drawn. | |
# - At the moment, the only way to hide collision boxes in the editor is to | |
# set the self_modulate opacity (alpha) to 0. | |
############################################################################### | |
# Copyright (c) 2022 Mike K. (https://github.com/zlrc) # | |
############################################################################### | |
# Boost Software License - Version 1.0 - August 17th, 2003 # | |
# # | |
# Permission is hereby granted, free of charge, to any person or organization # | |
# obtaining a copy of the software and accompanying documentation covered by # | |
# this license (the "Software") to use, reproduce, display, distribute, # | |
# execute, and transmit the Software, and to prepare derivative works of the # | |
# Software, and to permit third-parties to whom the Software is furnished to # | |
# do so, all subject to the following: # | |
# # | |
# The copyright notices in the Software and this entire statement, including # | |
# the above license grant, this restriction and the following disclaimer, # | |
# must be included in all copies of the Software, in whole or in part, and # | |
# all derivative works of the Software, unless such copies or derivative # | |
# works are solely in the form of machine-executable object code generated by # | |
# a source language processor. # | |
# # | |
# 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, TITLE AND NON-INFRINGEMENT. IN NO EVENT # | |
# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE # | |
# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, # | |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # | |
# DEALINGS IN THE SOFTWARE. # | |
############################################################################### | |
tool | |
extends CollisionPolygon2D | |
class_name TextureCollisionPoly2D | |
export(Texture) var TEXTURE setget set_texture | |
export(Vector2) var OFFSET = Vector2(0, 0) setget set_texture_offset | |
export(Vector2) var SCALE = Vector2(1, 1) setget set_texture_scale | |
export(float, -360, 360, 0.1) var ROTATION_DEGREES = 0 setget set_texture_rotation | |
export(bool) var INVERT = false setget invert_texture | |
export(float, 0.1, 16384, 0.1) var BORDER = 100 setget set_invert_border | |
export(bool) var ANTIALIASED = false setget set_antialias | |
var TEXTURE_NODE : Polygon2D | |
# Make sure there is a child node for the texture polygon | |
func _init(): | |
for node in self.get_children(): # clear any existing children first | |
self.remove_child(node) | |
node.queue_free() | |
_assert_texture() | |
set_texture_polygon(self.get_polygon()) | |
set_texture_light_mask(self.get_light_mask()) | |
# Override the built-in setter function to detect when polygon is modified | |
func _set(property : String, value): | |
if property == "polygon": | |
set_texture_polygon(value) | |
# Creates a texture node (Polygon2D) child if one is missing | |
func _assert_texture(): | |
TEXTURE_NODE = self.get_node("Texture") if self.has_node("Texture") else null | |
if(!TEXTURE_NODE): | |
TEXTURE_NODE = Polygon2D.new() | |
TEXTURE_NODE.name = "Texture" | |
self.add_child(TEXTURE_NODE) | |
TEXTURE_NODE.set_owner(self) | |
########### | |
# Setters # | |
########### | |
func set_texture_property(property : String, value): | |
_assert_texture() | |
TEXTURE_NODE[property] = value | |
func set_texture_polygon(value : PoolVector2Array): | |
set_texture_property("polygon", value) | |
func set_texture_light_mask(value : int): | |
set_texture_property("light_mask", value) | |
func set_texture(value : Texture): | |
TEXTURE = value | |
set_texture_property("texture", value) | |
func set_texture_offset(value : Vector2): | |
OFFSET = value | |
set_texture_property("texture_offset", value) | |
func set_texture_scale(value : Vector2): | |
SCALE = value | |
set_texture_property("texture_scale", value) | |
func set_texture_rotation(degrees : float): | |
ROTATION_DEGREES = degrees | |
set_texture_property("texture_rotation_degrees", degrees) | |
func invert_texture(enabled : bool): | |
INVERT = enabled | |
set_texture_property("invert_enable", enabled) | |
func set_invert_border(value : float): | |
BORDER = value | |
set_texture_property("invert_border", value) | |
func set_antialias(value : bool): | |
ANTIALIASED = value | |
set_texture_property("antialiased", value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo.mp4