-
Notifications
You must be signed in to change notification settings - Fork 4
One Way Platforms and Bouncy Platforms #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
219b99c
0781823
a92bd55
91cf8d1
7bf6d22
3635b90
c104226
f59d9f4
053a592
01d256a
98ce524
6d702f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| [remap] | ||
|
|
||
| importer="texture" | ||
| type="CompressedTexture2D" | ||
| uid="uid://gdcgbdiwbqkm" | ||
| path="res://.godot/imported/special_tiles.png-255ed16611815f9bd3f797a6f3943b03.ctex" | ||
| metadata={ | ||
| "vram_texture": false | ||
| } | ||
|
|
||
| [deps] | ||
|
|
||
| source_file="res://Assets/Images/Terrain/special_tiles.png" | ||
| dest_files=["res://.godot/imported/special_tiles.png-255ed16611815f9bd3f797a6f3943b03.ctex"] | ||
|
|
||
| [params] | ||
|
|
||
| compress/mode=0 | ||
| compress/high_quality=false | ||
| compress/lossy_quality=0.7 | ||
| compress/uastc_level=0 | ||
| compress/rdo_quality_loss=0.0 | ||
| compress/hdr_compression=1 | ||
| compress/normal_map=0 | ||
| compress/channel_pack=0 | ||
| mipmaps/generate=false | ||
| mipmaps/limit=-1 | ||
| roughness/mode=0 | ||
| roughness/src_normal="" | ||
| process/channel_remap/red=0 | ||
| process/channel_remap/green=1 | ||
| process/channel_remap/blue=2 | ||
| process/channel_remap/alpha=3 | ||
| process/fix_alpha_border=true | ||
| process/premult_alpha=false | ||
| process/normal_map_invert_y=false | ||
| process/hdr_as_srgb=false | ||
| process/hdr_clamp_exposure=false | ||
| process/size_limit=0 | ||
| detect_3d/compress_to=1 |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,14 @@ | ||
| class_name Player | ||
| extends CharacterBody2D | ||
|
|
||
| enum { | ||
| NULL, | ||
| DEFAULT_LAYER, | ||
| ONE_WAY_LAYER, | ||
| BOUNCY_LAYER | ||
| } | ||
|
|
||
|
|
||
| @export var animated_sprite_2d: AnimatedSprite2D | ||
| @export var player_name: Label | ||
|
|
||
|
|
@@ -31,6 +39,7 @@ var is_infected: bool = false | |
| @onready var wall_check_left: RayCast2D = $WallCheckLeft | ||
| @onready var wall_check_right: RayCast2D = $WallCheckRight | ||
| @onready var infection_area: Area2D = $InfectionArea | ||
| @onready var one_way_check: Area2D = $OneWayCheck | ||
| @onready var fart_sound: AudioStreamPlayer = $FartSound | ||
|
|
||
|
|
||
|
|
@@ -52,36 +61,86 @@ func _physics_process(delta: float) -> void: | |
| # Only process input for the player we control | ||
| if not is_multiplayer_authority(): | ||
| return | ||
|
|
||
| # Apply gravity | ||
| if not is_on_floor(): | ||
| velocity.y = min(velocity.y + get_gravity().y * gravity_scale * delta, max_fall_speed) | ||
|
|
||
| # Track floor state for jump resets | ||
| if is_on_floor() and not was_on_floor: | ||
| _on_landed() | ||
| was_on_floor = is_on_floor() | ||
|
|
||
| #Handle one-way platforms | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space after |
||
| _handle_one_way() | ||
|
|
||
| # Handle wall sliding | ||
| _handle_wall_slide() | ||
|
|
||
| # Handle jumping | ||
| _handle_jump() | ||
|
|
||
| # Handle horizontal movement | ||
| _handle_movement(delta) | ||
|
|
||
| # Apply movement | ||
| move_and_slide() | ||
| _apply_physics() | ||
|
|
||
| # Update animations | ||
| _update_animation() | ||
|
|
||
|
|
||
| func _apply_physics() -> void: | ||
| # store velocity and position and then call move_and_slide() | ||
| var vel: Vector2 = velocity | ||
| var pos1: Vector2 = global_position | ||
| move_and_slide() | ||
|
|
||
| var pos2: Vector2 = global_position | ||
|
|
||
| var col: KinematicCollision2D = get_last_slide_collision() | ||
| if !col: return | ||
|
|
||
| # If it detects a bouncy thing, bounce. | ||
| if !(PhysicsServer2D.body_get_collision_layer(col.get_collider_rid()) & BOUNCY_LAYER): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: I prefer using I frequently still use
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also a double negative? I'm not sure we need this here because then it's checking collision layer on every collision, not just bouncy collisions. |
||
| var new_vel: Vector2 = _bouncy_col_math(vel, col.get_normal()) | ||
| velocity = new_vel | ||
| move_and_slide() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this double move_and_slide() solution and will provide my solution in the PR review comment. |
||
| _bouncy_position_correction(pos1, pos2, col.get_normal()) | ||
|
|
||
| _on_landed() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential Bug: I think this should be wrapped in an |
||
|
|
||
|
|
||
| # when you call move_and_slide(), it steps the position forward in physics space. doing it a second time for the second move_and_slide() causes kinetic energy increases over time. | ||
| # youve gotta correct the second move_and_slide() call so you dont gain energy by just bouncing. this puts you at the same distance you had to the surface before colliding. | ||
| func _bouncy_position_correction(pos1: Vector2, pos2: Vector2, normal: Vector2) -> void: | ||
| global_position = pos2 - normal * (pos2 - pos1).dot(normal) | ||
|
|
||
|
|
||
| # Does the math for a bouncy collision against a static object. | ||
| func _bouncy_col_math(vel: Vector2, normal: Vector2) -> Vector2: | ||
| # First, project the vel vector onto the normal | ||
| # This shortened version of projection math only works cuz we know normal is a unit vector | ||
| var proj: Vector2 = normal * vel.dot(normal) | ||
|
|
||
| #Then, we take that component and minus it twice off the current velocity. boom. elastic collision against static wall. | ||
| return vel - 2 * proj | ||
|
|
||
|
|
||
| # Handles the one-way platform functionality. | ||
| func _handle_one_way() -> void: | ||
| # The only time that a one way platform should have collision is when: its detected by the check, the player is moving downwards or resting, and the down direction is not pressed. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if one_way_check.has_overlapping_bodies() && velocity.y >= 0 && !Input.is_action_pressed("fall_through"): | ||
| # We set the collision by modifying our own collision layers. | ||
| set_collision_mask_value(ONE_WAY_LAYER,true) | ||
| else: | ||
| set_collision_mask_value(ONE_WAY_LAYER,false) | ||
|
|
||
|
|
||
| # Handles horizontal movement with acceleration and friction | ||
| func _handle_movement(delta: float) -> void: | ||
| var input_direction: float = Input.get_axis("move_left", "move_right") | ||
|
|
||
| # Fallback to UI actions if custom actions don't exist | ||
| if input_direction == 0.0: | ||
| input_direction = Input.get_axis("ui_left", "ui_right") | ||
|
|
@@ -184,7 +243,7 @@ func _update_animation() -> void: | |
| func _on_infection_area_body_entered(body: Node2D) -> void: | ||
| if not multiplayer.is_server() or not is_infected: | ||
| return | ||
|
|
||
| if body is Player and not body.is_infected: | ||
| var game: Game = get_tree().get_first_node_in_group("game") | ||
| if game: | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like NULL being in an ENUM. NULL = Nothing, an ENUM defines something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bouncy layer is set to 4 in your bitwise math.
My solution was to implement