Skip to content

Commit

Permalink
game objects refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
WermooZ committed Jul 27, 2017
1 parent c0eaf90 commit e9ffc6f
Show file tree
Hide file tree
Showing 32 changed files with 539 additions and 56 deletions.
4 changes: 2 additions & 2 deletions buildings/airport.xscn
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resource_file type="PackedScene" subresource_count="4" version="1.1" version_name="Godot Engine v1.1.stable.custom_build">
<ext_resource path="res://assets/buildings/military.png" type="Texture"></ext_resource>
<ext_resource path="res://scripts/building.gd" type="Script"></ext_resource>
<ext_resource path="res://scripts/objects/building.gd" type="Script"></ext_resource>
<ext_resource path="res://gui/flag.xscn" type="PackedScene"></ext_resource>
<main_resource>
<dictionary name="_bundled" shared="false">
Expand Down Expand Up @@ -42,7 +42,7 @@
<int> 4 </int>
<bool> True </bool>
<rect2> 0, 128, 64, 64 </rect2>
<resource resource_type="Script" path="res://scripts/building.gd"> </resource>
<resource resource_type="Script" path="res://scripts/objects/building.gd"> </resource>
<dictionary shared="false">
<string> "__editor_plugin_screen__" </string>
<string> "2D" </string>
Expand Down
4 changes: 2 additions & 2 deletions buildings/barrack.xscn
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resource_file type="PackedScene" subresource_count="4" version="1.1" version_name="Godot Engine v1.1.stable.custom_build">
<ext_resource path="res://assets/buildings/military.png" type="Texture"></ext_resource>
<ext_resource path="res://scripts/building.gd" type="Script"></ext_resource>
<ext_resource path="res://scripts/objects/building.gd" type="Script"></ext_resource>
<ext_resource path="res://gui/flag.xscn" type="PackedScene"></ext_resource>
<main_resource>
<dictionary name="_bundled" shared="false">
Expand Down Expand Up @@ -39,7 +39,7 @@
<bool> True </bool>
<resource resource_type="Texture" path="res://assets/buildings/military.png"> </resource>
<rect2> 0, 0, 64, 64 </rect2>
<resource resource_type="Script" path="res://scripts/building.gd"> </resource>
<resource resource_type="Script" path="res://scripts/objects/building.gd"> </resource>
<dictionary shared="false">
<string> "__editor_plugin_screen__" </string>
<string> "2D" </string>
Expand Down
2 changes: 1 addition & 1 deletion buildings/bunker_blue.xscn
Expand Up @@ -2,7 +2,7 @@
<resource_file type="PackedScene" subresource_count="4" version="2.1" version_name="Godot Engine v2.1.3.stable.official">
<ext_resource path="res://gui/flag.xscn" type="PackedScene" index="2"></ext_resource>
<ext_resource path="res://assets/buildings/military.png" type="Texture" index="0"></ext_resource>
<ext_resource path="res://scripts/building.gd" type="Script" index="1"></ext_resource>
<ext_resource path="res://scripts/objects/building.gd" type="Script" index="1"></ext_resource>
<main_resource>
<dictionary name="_bundled" shared="false">
<string> "conn_count" </string>
Expand Down
2 changes: 1 addition & 1 deletion buildings/bunker_red.xscn
Expand Up @@ -2,7 +2,7 @@
<resource_file type="PackedScene" subresource_count="4" version="2.1" version_name="Godot Engine v2.1.3.stable.official">
<ext_resource path="res://gui/flag.xscn" type="PackedScene" index="2"></ext_resource>
<ext_resource path="res://assets/buildings/military.png" type="Texture" index="0"></ext_resource>
<ext_resource path="res://scripts/building.gd" type="Script" index="1"></ext_resource>
<ext_resource path="res://scripts/objects/building.gd" type="Script" index="1"></ext_resource>
<main_resource>
<dictionary name="_bundled" shared="false">
<string> "conn_count" </string>
Expand Down
4 changes: 2 additions & 2 deletions buildings/factory.xscn
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resource_file type="PackedScene" subresource_count="4" version="1.1" version_name="Godot Engine v1.1.stable.custom_build">
<ext_resource path="res://assets/buildings/military.png" type="Texture"></ext_resource>
<ext_resource path="res://scripts/building.gd" type="Script"></ext_resource>
<ext_resource path="res://scripts/objects/building.gd" type="Script"></ext_resource>
<ext_resource path="res://gui/flag.xscn" type="PackedScene"></ext_resource>
<main_resource>
<dictionary name="_bundled" shared="false">
Expand Down Expand Up @@ -38,7 +38,7 @@
<resource resource_type="Texture" path="res://assets/buildings/military.png"> </resource>
<bool> True </bool>
<rect2> 0, 64, 64, 64 </rect2>
<resource resource_type="Script" path="res://scripts/building.gd"> </resource>
<resource resource_type="Script" path="res://scripts/objects/building.gd"> </resource>
<dictionary shared="false">
<string> "__editor_plugin_screen__" </string>
<string> "2D" </string>
Expand Down
4 changes: 2 additions & 2 deletions buildings/tower.xscn
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resource_file type="PackedScene" subresource_count="4" version="1.1" version_name="Godot Engine v1.1.stable.custom_build">
<ext_resource path="res://assets/buildings/military.png" type="Texture"></ext_resource>
<ext_resource path="res://scripts/building.gd" type="Script"></ext_resource>
<ext_resource path="res://scripts/objects/building.gd" type="Script"></ext_resource>
<ext_resource path="res://gui/flag.xscn" type="PackedScene"></ext_resource>
<main_resource>
<dictionary name="_bundled" shared="false">
Expand Down Expand Up @@ -38,7 +38,7 @@
<resource resource_type="Texture" path="res://assets/buildings/military.png"> </resource>
<bool> True </bool>
<rect2> 0, 256, 64, 64 </rect2>
<resource resource_type="Script" path="res://scripts/building.gd"> </resource>
<resource resource_type="Script" path="res://scripts/objects/building.gd"> </resource>
<dictionary shared="false">
<string> "__editor_plugin_screen__" </string>
<string> "2D" </string>
Expand Down
2 changes: 1 addition & 1 deletion particle/smoke.xscn
Expand Up @@ -119,7 +119,7 @@
<int> 0 </int>
<string> "sources" </string>
<array len="2" shared="false">
<string> "res://scripts/building.gd" </string>
<string> "res://scripts/objects/building.gd" </string>
<string> "res://scripts/terrain.gd" </string>
</array>
</dictionary>
Expand Down
6 changes: 5 additions & 1 deletion scripts/ai/actions/estimators/unit/estimator.gd
Expand Up @@ -39,10 +39,14 @@ func can_move(action):

func get_waypoint_value(action):
var object = self.bag.abstract_map.get_field(action.point_of_interest.position_on_map).object
var value = 0
if object == null:
return 0
value = self.waypoint_value[object.type]
#TODO - stub for waypoint handling
return self.waypoint_value[object.type]
if action.destination.group == 'waypoint' and action.destination.subtype == action.destination.TYPE_SPAWN_POINT:
value = value + 1
return value

func enemies_in_sight(action):
var nearby_tiles = self.bag.positions.get_nearby_tiles(action.path[0], 4)
Expand Down
31 changes: 23 additions & 8 deletions scripts/ai/actions_handler.gd
Expand Up @@ -2,7 +2,7 @@ extends "res://scripts/bag_aware.gd"

var actions = []
var action = preload('actions/action.gd')
var waypoint = preload('../waypoint.gd')
var waypoint = preload('../objects/waypoint.gd')

const ACTION_OLD_THRESHOLD = 100
const ACTION_UNUSED_OLD_THRESHOLD = 15
Expand All @@ -14,18 +14,33 @@ func add_waypoint_action(unit, destination, ttl = self.action.DEFAULT_TTL):
var waypoint_obj = null
var destination_field
var nearby_tiles = self.bag.positions.get_nearby_tiles(destination.position_on_map, 1)
var subtype = null
var spawn_point = null
if destination.group == 'building':
spawn_point = destination.spawn_point

for move_destination in nearby_tiles:
if unit.position_on_map == move_destination:
self.actions.append(self.create_action(unit, destination, destination, ttl))
return


for move_destination in nearby_tiles:
destination_field = self.bag.abstract_map.get_field(move_destination)
if destination_field.is_passable():
waypoint_obj = self.waypoint.new(move_destination)
if not self.get_action(unit, waypoint_obj):
self.actions.append(self.create_action(unit, waypoint_obj, destination, ttl))
if unit.type == 0:
for move_destination in nearby_tiles:
destination_field = self.bag.abstract_map.get_field(move_destination)
if destination_field.is_passable():
waypoint_obj = self.waypoint.new(move_destination)
if move_destination == spawn_point:
subtype = waypoint.TYPE_SPAWN_POINT
else:
subtype = waypoint.TYPE_BULDING_AREA

waypoint_obj = self.waypoint.new(move_destination, subtype)
if not self.get_action(unit, waypoint_obj):
self.actions.append(self.create_action(unit, waypoint_obj, destination, ttl))
else:
waypoint_obj = self.waypoint.new(destination.spawn_point, waypoint.TYPE_SPAWN_POINT)
if not self.get_action(unit, waypoint_obj):
self.actions.append(self.create_action(unit, waypoint_obj, destination, ttl))

func add_action(unit, destination, ttl = self.action.DEFAULT_TTL):
if self.get_action(unit, destination):
Expand Down
6 changes: 3 additions & 3 deletions scripts/ai/ai.gd
Expand Up @@ -95,9 +95,9 @@ func __gather_destinations(unit):
destinations = destinations + self.bag.positions.get_nearby_enemy_buildings(nearby_tiles, self.player)
destinations = destinations + self.bag.positions.get_nearby_empty_buldings(nearby_tiles)
else:
for building in self.bag.positions.get_nearby_enemy_buildings(nearby_tiles, self.player):
#destinations.append(building.get_spawn_point_pos()) # TODO - create dummy obj for spawn
pass
for building in self.bag.positions.get_nearby_enemy_buildings(nearby_tiles, self.player):
destinations = destinations + self.bag.positions.get_nearby_enemy_buildings(nearby_tiles, self.player)
pass

if destinations.size() > self.MIN_DESTINATION_PER_UNIT:
return destinations
Expand Down
5 changes: 2 additions & 3 deletions scripts/ai/perform.gd
@@ -1,7 +1,7 @@
extends "res://scripts/bag_aware.gd"

const INTERVAL = 0.15
const SKIP_INTERVAL = 1
const INTERVAL = 0.015
const SKIP_INTERVAL = 0.1
var running = false

func do_ai_stuff():
Expand All @@ -20,7 +20,6 @@ func do_ai_stuff():
self.bag.controllers.action_controller.end_turn()
return


self.__execute_with_interval(self.INTERVAL)

func start_ai_timer():
Expand Down
132 changes: 132 additions & 0 deletions scripts/objects/building.gd
@@ -0,0 +1,132 @@
extends Sprite

export var position_on_map = Vector2(0,0)
export var type = 0
export var player = -1
export var bonus_ap = 1
export var can_spawn = true

var current_map
var group = 'building'
var spawn_point = Vector2(0, 0)
var spawn_field = null
export var spawn_point_position = Vector2(0, 1)
var flag
var turn_claimed = -1

var object_factory = preload('../object_factory.gd').new()

var floating_ap_template = preload('res://particle/hit_points.xscn')
var floating_ap

var TYPE_BUNKER = 0
var TYPE_BARRACKS = 1
var TYPE_FACTORY = 2
var TYPE_AIRPORT = 3
var TYPE_TOWER = 4

const HAS_SAME_TYPE_OF_UNIT_MODIFIER = 3

func get_pos_map():
return position_on_map

func get_spawn_point_pos():
return spawn_point

func get_initial_pos():
position_on_map = current_map.world_to_map(self.get_pos())
spawn_point = Vector2(position_on_map) + spawn_point_position
return position_on_map

func set_pos_map(new_position):
self.set_pos(current_map.map_to_world(new_position))
position_on_map = new_position
spawn_point = Vector2(position_on_map) + spawn_point_position

func claim(new_player, turn):
if new_player == -1:
self.set_frame(0)
if new_player == 0:
self.set_frame(1)
if new_player == 1:
self.set_frame(2)

player = new_player
self.turn_claimed = turn
flag.change_flag(new_player)

func get_player():
return player

func set_frame(number):
var current_frame = get_region_rect()
var new_frame = Rect2(number * 64, current_frame.pos.y, 64, 64)
set_region_rect(new_frame)

func get_spawn_type():
if type == TYPE_BUNKER || type == TYPE_BARRACKS:
return 0
if type == TYPE_FACTORY:
return 1
if type == TYPE_AIRPORT:
return 2

func spawn_unit(player):
var unit_type = self.get_spawn_type()
if unit_type != null:
return object_factory.build_unit(unit_type, player)

return null

func get_required_ap():
if type == TYPE_BARRACKS:
return 25
if type == TYPE_FACTORY:
return 50
if type == TYPE_AIRPORT:
return 70
if type == TYPE_BUNKER:
return 40

return 0

func get_building_name():
if type == TYPE_BUNKER:
return "HQ"
if type == TYPE_BARRACKS:
return "BARRACKS"
if type == TYPE_FACTORY:
return "FACTORY"
if type == TYPE_AIRPORT:
return "AIRPORT"
if type == TYPE_TOWER:
return "GSM TOWER"

func get_cost():
return get_required_ap()

func show_floating_ap():
floating_ap = floating_ap_template.instance()
floating_ap.set_text(str(bonus_ap))
floating_ap.unit = self
floating_ap.show_ap_icon()
self.add_child(floating_ap)

func clear_floating_damage():
self.remove_child(floating_ap)
floating_ap.queue_free()

func can_spawn_units():
if self.type == 4:
return false

return true

func _ready():
add_to_group("buildings")
if get_node("/root/game"):
current_map = get_node("/root/game").current_map_terrain
flag = get_node('flag')
pass


21 changes: 21 additions & 0 deletions scripts/objects/units/helicopter.gd
@@ -0,0 +1,21 @@
extends "unit.gd"

func _init():
type = 2
type_name = 'heli'
type_name_label = 'LABEL_WORKSHOP_HELI'

life = 10
max_life = 10
attack = 8
max_ap = 8
limited_ap = 6
attack_ap = 1
max_attacks_number = 1
ap = 8
attacks_number = 1
visibility = 4

func can_capture_building(building):
return false

7 changes: 7 additions & 0 deletions scripts/objects/units/randomizing_animation_for_civilians.gd
@@ -0,0 +1,7 @@
func _ready():
randomize()
get_node("anim").seek(randf())
if randf() < 0.5:
self.set_flip_h(true)

get_node("anim").set_speed(rand_range(0.5, 1.5))
24 changes: 24 additions & 0 deletions scripts/objects/units/soldier.gd
@@ -0,0 +1,24 @@
extends "unit.gd"

func _init():
type = 0
type_name = 'soldier'
type_name_label = 'LABEL_WORKSHOP_INFANTRY'

life = 10
max_life = 10
attack = 5
max_ap = 4
limited_ap = 3
attack_ap = 1
max_attacks_number = 1
ap = 4
attacks_number = 1
visibility = 3

func can_capture_building(building):
if building.player == player:
return false

return true;

0 comments on commit e9ffc6f

Please sign in to comment.