mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-18 03:21:47 +02:00
[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
This commit is contained in:
parent
c6fd8ec4ea
commit
ca29a047de
@ -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
|
||||
|
27
map_gen/maps/snake_demo.lua
Normal file
27
map_gen/maps/snake_demo.lua
Normal file
@ -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')
|
Loading…
Reference in New Issue
Block a user