From ca29a047de3e23db1a7fae147a625da38745041f Mon Sep 17 00:00:00 2001 From: Simon <44922798+SimonFlapse@users.noreply.github.com> Date: Fri, 11 Oct 2019 00:28:40 +0200 Subject: [PATCH] [Snake] Added on_snake_player_died event (#995) * Added on_snake_player_died event and a snake_demo map * Added score to the event * Fixed entities being destructible Changed comment * Fixed cases where controller type wasn't god * Changed players controller to spectator when their snake dies * Changed start_game call away from remote.call * Needed new call to show the snake button * Changed start_game to use Snake.control * removed snake event from demo map * Update map_gen/maps/snake_demo.lua --- features/snake/game.lua | 31 +++++++++++++++++++++++++++++-- map_gen/maps/snake_demo.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 map_gen/maps/snake_demo.lua diff --git a/features/snake/game.lua b/features/snake/game.lua index adf8d641..948fb508 100644 --- a/features/snake/game.lua +++ b/features/snake/game.lua @@ -15,6 +15,8 @@ local queue_size = Queue.size local queue_pairs = Queue.pairs local pairs = pairs +local Public = {} + local snakes = {} -- player_index -> snake_data {is_marked_for_destroy:bool, queue :Queue of {entity, cord} } local board = { size = 0, @@ -61,7 +63,15 @@ local function destroy_snake(index, snake) return end - game.print({'snake.snake_destroyed', player.name, queue_size(snake.queue)}) + player.set_controller{type = defines.controllers.spectator} + + local score = queue_size(snake.queue) + game.print({'snake.snake_destroyed', player.name, score}) + + script.raise_event(Public.events.on_snake_player_died, { + player = player, + score = score + }) end local function destroy_dead_snakes() @@ -101,6 +111,7 @@ local function spawn_food() entity = surface.create_entity({name = 'compilatron', position = pos, force = 'neutral', direction = random(7)}) entity.active = false + entity.destructible = false food_count = food_count + 1 break @@ -214,6 +225,7 @@ local function tick_snake(index, snake) entity.character_running_speed_modifier = -1 entity.color = player.color entity.active = false + entity.destructible = false push_to_end(snake_queue, {entity = entity, cord = tail_cord}) board.food_count = board.food_count - 1 @@ -344,9 +356,11 @@ local function new_snake(player) end player.teleport(pos, board.surface) + player.set_controller{type = defines.controllers.god} player.create_character() character = player.character character.character_running_speed_modifier = -1 + character.destructible = false local queue = queue_new() push(queue, {entity = character, cord = cord}) @@ -374,7 +388,20 @@ local function new_game(surface, position, size, update_rate, max_food) Event.add_removable_nth_tick(board.update_rate, tick) end -local Public = {} +Public = { + events = { + --[[ + on_snake_player_died + Called when a player have died in a game of snake + Contains + name :: uint: Unique identifier of the event + tick :: uint: Tick the event was generated. + player :: LuaPlayer + score :: uint: Score reached + ]] + on_snake_player_died = Event.generate_event_name('on_snake_player_died') + } +} function Public.start_game(surface, top_left_position, size, update_rate, max_food) if board.is_running then diff --git a/map_gen/maps/snake_demo.lua b/map_gen/maps/snake_demo.lua new file mode 100644 index 00000000..0651d240 --- /dev/null +++ b/map_gen/maps/snake_demo.lua @@ -0,0 +1,27 @@ +local Event = require 'utils.event' +local Token = require 'utils.token' +local Task = require 'utils.task' +local b = require 'map_gen.shared.builders' +local RS = require 'map_gen.shared.redmew_surface' +local floor = math.floor +local Snake_Control = require 'features.snake.control' + + +local config = global.config +local size = 45 + +config.market.enabled = false + +local snake_generate = Token.register(function() + local position = {x = -floor(size), y = 5} + local max_food = 8 + local speed = 30 + Snake_Control.start_game(RS.get_surface(), position, size, speed, max_food) + -- An alternative is to use: remote.call('snake', 'start_game', RS.get_surface(), position, size, speed, max_food) +end) + +Event.on_init(function() + Task.set_timeout_in_ticks(60, snake_generate) +end) + +return b.change_tile(b.rectangle(4, 4), true, 'concrete')