export var mouseSensitivity := 0.25 export var deadPosition := 800.0
var _isPicked := false # 判断当前刚体是否被鼠标拖拽
func _input_event(viewport, event, shape_idx): # 右键按下时拖拽箱子 var e : InputEventMouseButton = event as InputEventMouseButton if e && e.button_index == BUTTON_RIGHT && e.pressed: pickup()
func _unhandled_input(event): # 右键松开时抛掉箱子 var e : InputEventMouseButton = event as InputEventMouseButton if e && e.button_index == BUTTON_RIGHT && ! e.pressed: # 传入鼠标的移动速度 var v := Input.get_last_mouse_speed() * mouseSensitivity drop(v)
func _physics_process(delta): # 更新拖拽盒子的位置,跟随鼠标移动 if _isPicked: self.global_transform.origin = self.get_global_mouse_position()
# 盒子掉出地图之外删除 if self.position.y > deadPosition: self.queue_free()
# 引起爆炸的物体分组名集合,这里为玩家和子弹 export(Array, String) var triggerGroups := ['player', 'bullet']
func _on_Area2D_area_or_body_entered(area_or_body): for group in triggerGroups: if area_or_body.is_in_group(group): $Explode.explode() $Area2D.queue_free() return
# 随机删除一部分房间,把房间的位置全部添加到数组,注意时 Vector3 类型 var allPoints : Array = [] for room in _roomContainer.get_children(): if randf() < cullTolerance: room.queue_free() else: room.mode = RigidBody2D.MODE_STATIC allPoints.append(Vector3(room.position.x, room.position.y, 0.0)) print('Step 2 is done.') # 第二步完成
# 创建新的AStar算法,添加第一个点 _astarPath = AStar.new() _astarPath.add_point(_astarPath.get_available_point_id(), allPoints.pop_front()) # 循环所有【未添加的点】,循环所有AStar中【已添加的点】 # 找出【未添加点】与【已添加点】的距离中,【最短】的距离点,并添加到AStar中 # 同时将该点从【未添加点集合】中删除 while allPoints: var minDistance : float = INF var minDistancePosition : Vector3 var minDistancePositionIndex : int var currentPointId :int = -1 for point in _astarPath.get_points(): for index in range(allPoints.size()): var pos = allPoints[index] var distance = _astarPath.get_point_position(point).distance_to(pos) if distance < minDistance: minDistance = distance minDistancePosition = pos minDistancePositionIndex = index currentPointId = point var id = _astarPath.get_available_point_id() _astarPath.add_point(id, minDistancePosition) _astarPath.connect_points(currentPointId, id) allPoints.remove(minDistancePositionIndex) print('Step 3 is done.') # 第三步完成
# 等待一帧的时间,用于等待被删除的房间被彻底移除 yield(self.get_tree(), 'idle_frame') if _roomContainer.get_child_count() == 0: return
# 找出所有房间最左上角和最右下角的两个坐标,确定摄像机的缩放和位移 var minPos := Vector2(_roomContainer.get_child(0).position.x, _roomContainer.get_child(0).position.y) var maxPos := minPos for room in _roomContainer.get_children(): var rect := room.getRect() as Rect2 if rect.position.x < minPos.x: minPos.x = rect.position.x if rect.end.x > maxPos.x: maxPos.x = rect.end.x if rect.position.y < minPos.y: minPos.y = rect.position.y if rect.end.y > maxPos.y: maxPos.y = rect.end.y _zoom = Vector2.ONE * ceil(max((maxPos.x - minPos.x) / _windowSize.x, (maxPos.y - minPos.y) / _windowSize.y)) _offset = (maxPos + minPos) / 2 print('Step 4 is done.') # 第四步完成