#37 ajout d'item outils ainsi que de visualisation de leur zone d'effet (avec la classe ActionArea
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://cd3re3552pt7m"]
|
||||
[gd_scene load_steps=2 format=3 uid="uid://b0ecjfdnvhjjf"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dedg615xudpoq" path="res://entities/interactables/item_object/script/item_object.gd" id="1_hxea8"]
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
@tool
|
||||
extends Interactable
|
||||
class_name ItemObject
|
||||
|
||||
@@ -21,13 +22,15 @@ func _ready():
|
||||
generate_collision(10)
|
||||
|
||||
func interact(player : Player) -> bool:
|
||||
if player.inventory.lenght() < player.inventory.max_items:
|
||||
player.inventory.add_item(item)
|
||||
pickup_animation(player)
|
||||
var swapped_item = player.inventory.get_item()
|
||||
|
||||
player.get_item(item)
|
||||
|
||||
if swapped_item:
|
||||
item = swapped_item
|
||||
else :
|
||||
var swaped_item = player.inventory.swap_items(item)
|
||||
item = swaped_item
|
||||
|
||||
pickup_animation(player)
|
||||
|
||||
return true
|
||||
|
||||
func pickup_animation(player : Player):
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
extends Node2D
|
||||
extends Area2D
|
||||
class_name Plant
|
||||
|
||||
const PLANT_AREA_WIDTH = 10
|
||||
@@ -6,61 +6,66 @@ const PLANT_SPRITE_SCALE = 0.15
|
||||
|
||||
enum State {PLANTED, GROWING, MATURE}
|
||||
|
||||
var plant_type : PlantType
|
||||
var planet : Planet
|
||||
@export var plant_type : PlantType
|
||||
@export var planet : Planet
|
||||
|
||||
var state : State = State.PLANTED : set = change_state
|
||||
var day : int = 0
|
||||
|
||||
@onready var plant_sprite : Sprite2D = generate_sprite()
|
||||
@onready var plant_area : Area2D = generate_area()
|
||||
@onready var collision_shape : CollisionShape2D = generate_collision_shape()
|
||||
|
||||
func _init(_plant_type, _planet):
|
||||
plant_type = _plant_type
|
||||
planet = _planet
|
||||
func _init(_plant_type = null, _planet = null):
|
||||
plant_type = _plant_type
|
||||
planet = _planet
|
||||
|
||||
func generate_sprite() -> Sprite2D:
|
||||
var sprite = Sprite2D.new()
|
||||
var sprite = Sprite2D.new()
|
||||
|
||||
add_child(sprite)
|
||||
sprite.texture = get_state_texture(state)
|
||||
sprite.scale = Vector2.ONE * PLANT_SPRITE_SCALE
|
||||
sprite.offset
|
||||
add_child(sprite)
|
||||
sprite.texture = get_state_texture(state)
|
||||
sprite.scale = Vector2.ONE * PLANT_SPRITE_SCALE
|
||||
sprite.offset
|
||||
|
||||
return sprite
|
||||
return sprite
|
||||
|
||||
func generate_area() -> Area2D:
|
||||
var area = Area2D.new()
|
||||
var collision = CollisionShape2D.new()
|
||||
var collision_shape = CircleShape2D.new()
|
||||
collision_shape.radius = PLANT_AREA_WIDTH
|
||||
func generate_collision_shape() -> CollisionShape2D:
|
||||
var collision = CollisionShape2D.new()
|
||||
var shape = CircleShape2D.new()
|
||||
shape.radius = PLANT_AREA_WIDTH
|
||||
|
||||
collision.shape = collision_shape
|
||||
area.add_child(collision)
|
||||
add_child(area)
|
||||
collision.shape = shape
|
||||
add_child(collision)
|
||||
|
||||
return area
|
||||
return collision
|
||||
|
||||
func pass_day():
|
||||
day += 1
|
||||
if day > plant_type.growing_time:
|
||||
change_state(State.MATURE)
|
||||
else:
|
||||
change_state(State.GROWING)
|
||||
day += 1
|
||||
if day > plant_type.growing_time:
|
||||
change_state(State.MATURE)
|
||||
else:
|
||||
change_state(State.GROWING)
|
||||
|
||||
func change_state(_state : State):
|
||||
state = _state
|
||||
plant_sprite.texture = get_state_texture(state)
|
||||
state = _state
|
||||
plant_sprite.texture = get_state_texture(state)
|
||||
|
||||
if state == State.MATURE and plant_type.mature_effect:
|
||||
plant_type.mature_effect.effect(self)
|
||||
if state == State.MATURE and plant_type.mature_effect:
|
||||
plant_type.mature_effect.effect(self)
|
||||
|
||||
func get_state_texture(s : State) -> Texture2D:
|
||||
match s:
|
||||
State.PLANTED:
|
||||
return plant_type.planted_texture
|
||||
State.GROWING:
|
||||
return plant_type.growing_texture
|
||||
State.MATURE:
|
||||
return plant_type.mature_texture
|
||||
return null
|
||||
match s:
|
||||
State.PLANTED:
|
||||
return plant_type.planted_texture
|
||||
State.GROWING:
|
||||
return plant_type.growing_texture
|
||||
State.MATURE:
|
||||
return plant_type.mature_texture
|
||||
return null
|
||||
|
||||
func harvest():
|
||||
day += 1
|
||||
if day > plant_type.growing_time:
|
||||
change_state(State.MATURE)
|
||||
else:
|
||||
change_state(State.GROWING)
|
||||
@@ -55,16 +55,16 @@ stream_0/stream = ExtResource("15_qpopc")
|
||||
script = ExtResource("1_abrql")
|
||||
|
||||
[node name="Sprite" type="Sprite2D" parent="."]
|
||||
position = Vector2(2, -55)
|
||||
position = Vector2(2, -46)
|
||||
scale = Vector2(0.084375, 0.084375)
|
||||
texture = ExtResource("1_symyc")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
position = Vector2(-2, -20)
|
||||
position = Vector2(-2, -18)
|
||||
shape = SubResource("CircleShape2D_sglur")
|
||||
|
||||
[node name="InteractArea2D" type="Area2D" parent="."]
|
||||
position = Vector2(0, -14)
|
||||
position = Vector2(0, -12)
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="InteractArea2D"]
|
||||
shape = SubResource("CircleShape2D_abrql")
|
||||
|
||||
44
entities/player/scripts/action_area.gd
Normal file
44
entities/player/scripts/action_area.gd
Normal file
@@ -0,0 +1,44 @@
|
||||
extends Area2D
|
||||
class_name ActionArea
|
||||
|
||||
const OPACITY = 0.3
|
||||
const ACTIVATED_COLOR = Color.TURQUOISE
|
||||
const DEACTIVATED_COLOR = Color.REBECCA_PURPLE
|
||||
|
||||
var collision_shape : CollisionShape2D = null
|
||||
var area_width : float = 40
|
||||
var area_distance : float = 80
|
||||
|
||||
var activated : bool = false :
|
||||
set(v):
|
||||
var old = activated
|
||||
activated = v
|
||||
if old != activated:
|
||||
queue_redraw()
|
||||
|
||||
func _init(
|
||||
_area_width : float = 40,
|
||||
_area_distance : float = 80
|
||||
):
|
||||
area_width = _area_width
|
||||
area_distance = _area_distance
|
||||
|
||||
func _ready():
|
||||
collision_shape = CollisionShape2D.new()
|
||||
collision_shape.position.x = area_distance
|
||||
collision_shape.shape = CircleShape2D.new()
|
||||
collision_shape.shape.radius = area_width
|
||||
add_child(collision_shape)
|
||||
|
||||
func _process(_delta):
|
||||
look_at(get_global_mouse_position())
|
||||
|
||||
func _draw():
|
||||
draw_circle(
|
||||
Vector2(
|
||||
area_distance,
|
||||
0
|
||||
),
|
||||
area_width,
|
||||
Color((ACTIVATED_COLOR if activated else DEACTIVATED_COLOR), OPACITY)
|
||||
)
|
||||
1
entities/player/scripts/action_area.gd.uid
Normal file
1
entities/player/scripts/action_area.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://btmwjrscevcb1
|
||||
@@ -31,6 +31,7 @@ var energy : int = max_energy :
|
||||
set(v):
|
||||
energy = v
|
||||
emit_signal("player_updated", self)
|
||||
var action_area : ActionArea = null
|
||||
|
||||
func _ready():
|
||||
emit_signal("player_updated", self)
|
||||
@@ -44,14 +45,15 @@ func get_input():
|
||||
var old_velocity=velocity
|
||||
calculate_direction()
|
||||
|
||||
can_use_item = inventory.lenght() != 0 and inventory.get_item().can_use(self)
|
||||
can_use_item = inventory.get_item() and inventory.get_item().can_use(self)
|
||||
if action_area:
|
||||
action_area.activated = can_use_item
|
||||
if Input.is_action_just_pressed("action"):
|
||||
use_item()
|
||||
try_use_item()
|
||||
if Input.is_action_just_pressed("interact") and closest_interactable:
|
||||
closest_interactable.interact(self)
|
||||
if Input.is_action_just_pressed("drop") and inventory.lenght() > 0:
|
||||
var item_to_drop = inventory.pop_item()
|
||||
planet.drop_item(item_to_drop, global_position)
|
||||
if Input.is_action_just_pressed("drop") and inventory.get_item():
|
||||
drop_item()
|
||||
if old_velocity.length()==0 and velocity.length()!=0:
|
||||
$Audio/AudioStreamPlayer_movement.play()
|
||||
|
||||
@@ -65,13 +67,29 @@ func try_use_item():
|
||||
if energy > 0 and can_use_item:
|
||||
use_item()
|
||||
|
||||
func get_item(item : Item):
|
||||
remove_action_area()
|
||||
inventory.set_item(item)
|
||||
if item is ToolItem:
|
||||
add_action_area(item.generate_action_area())
|
||||
|
||||
func drop_item():
|
||||
var item_to_drop = inventory.pop_item()
|
||||
planet.drop_item(item_to_drop, global_position)
|
||||
remove_action_area()
|
||||
|
||||
func delete_item():
|
||||
inventory.set_item(null)
|
||||
remove_action_area()
|
||||
|
||||
func use_item():
|
||||
var item = inventory.get_item()
|
||||
var is_item_used = item.use(self)
|
||||
if is_item_used:
|
||||
energy -= 1
|
||||
if item.is_one_time_use():
|
||||
inventory.pop_item()
|
||||
delete_item()
|
||||
|
||||
|
||||
func pass_day():
|
||||
energy = max_energy
|
||||
@@ -92,6 +110,14 @@ func detect_closest_interactable():
|
||||
else :
|
||||
closest_interactable = null
|
||||
|
||||
func add_action_area(area : ActionArea):
|
||||
action_area = area
|
||||
add_child(action_area)
|
||||
|
||||
func remove_action_area():
|
||||
if (action_area):
|
||||
remove_child(action_area)
|
||||
|
||||
func _process(_delta):
|
||||
get_input()
|
||||
move_and_slide()
|
||||
|
||||
Reference in New Issue
Block a user