trying to make the game

This commit is contained in:
whosit 2024-11-23 16:26:23 +03:00
parent b8ce1d1596
commit 57c41cddd0
4 changed files with 182 additions and 6 deletions

68
classic.lua Normal file
View File

@ -0,0 +1,68 @@
--
-- classic
--
-- Copyright (c) 2014, rxi
--
-- This module is free software; you can redistribute it and/or modify it under
-- the terms of the MIT license. See LICENSE for details.
--
local Object = {}
Object.__index = Object
function Object:new()
end
function Object:extend()
local cls = {}
for k, v in pairs(self) do
if k:find("__") == 1 then
cls[k] = v
end
end
cls.__index = cls
cls.super = self
setmetatable(cls, self)
return cls
end
function Object:implement(...)
for _, cls in pairs({...}) do
for k, v in pairs(cls) do
if self[k] == nil and type(v) == "function" then
self[k] = v
end
end
end
end
function Object:is(T)
local mt = getmetatable(self)
while mt do
if mt == T then
return true
end
mt = getmetatable(mt)
end
return false
end
function Object:__tostring()
return "Object"
end
function Object:__call(...)
local obj = setmetatable({}, self)
obj:new(...)
return obj
end
return Object

94
game.lua Normal file
View File

@ -0,0 +1,94 @@
local modname = "yl_snowball"
local modpath = core.get_modpath(modname)
local Object = dofile(modpath .. DIR_DELIM .. "classic.lua")
local snowgame = {}
local games_by_player = {}
local Game = Object:extend()
function Game:new()
self.time_started = core.get_gametime()
self.scores_by_player = {}
return self
end
function Game:add_player(player_name, score)
if not score then
score = {
hits_taken = 0,
hits_given = 0,
shots = 0,
}
end
self.scores_by_player[player_name] = score
games_by_player[player_name] = self
end
function Game:merge(other_game)
for name, score in pairs(other_game.scores_by_player) do
self:add_player(name, score)
end
end
function Game:register_hit(shooter_name, target_name)
local s_score = self.scores_by_player[shooter_name]
s_score.hits_given = s_score.hits_given + 1
local t_score = self.scores_by_player[target_name]
t_score.hits_taken = t_score.hits_taken + 1
print(shooter_name, dump(s_score))
print(target_name, dump(t_score))
end
function Game:register_shot(shooter_name)
local s_score = self.scores_by_player[shooter_name]
s_score.shots = s_score.shots + 1
end
function snowgame.register_hit(shooter_name, target_name)
print(shooter_name, target_name)
local s_game = games_by_player[shooter_name]
local t_game = games_by_player[target_name]
if s_game and t_game and (s_game ~= t_game) then
-- if both player but different games, merge
s_game:merge(t_game)
end
local game = s_game or t_game
if not game then
-- none are playing, make a new one
game = Game()
end
if not s_game then
-- add shooter to current
game:add_player(shooter_name)
game:register_shot(shooter_name)
end
if not t_game then
-- add target to current (if played diff game, was merged)
game:add_player(target_name)
end
game:register_hit(shooter_name, target_name)
end
function snowgame.register_shot(shooter_name)
local s_game = games_by_player[shooter_name]
if s_game then
s_game:register_shot(shooter_name)
end
end
yl_snowball.snowgame = snowgame
return snowgame

View File

@ -18,6 +18,7 @@ yl_snowball.information = {
dofile(MODPATH .. "internal.lua")
dofile(MODPATH .. "game.lua")
local mod_end_time = (core.get_us_time() - mod_start_time) / 1000000

View File

@ -38,7 +38,10 @@ local function spawn_snow_puff(pos, normal)
size = 2,
collisiondetection = true,
collision_removal = true,
texpool = { "yl_snowball_snowflake.png" },
texture = {
name = "yl_snowball_snowflake.png",
--blend = "clip", -- does not work in 5.9?!?
},
--glow = 2,
}
core.add_particlespawner(def)
@ -81,8 +84,9 @@ yl_snowball.on_use = function(itemstack, user, pointed_thing)
local player = user
local player_name = player:get_player_name()
local current_time = os.clock() --core.get_gametime()
local current_time = os.clock() --core.get_gametime() -- gametime seems to "lag"?
if current_time - (player_last_throw_time[player_name] or 0) < COOLDOWN then
-- too soon, don't shot, don't take items
return nil
end
@ -99,7 +103,10 @@ yl_snowball.on_use = function(itemstack, user, pointed_thing)
vel,
v_new(0, -8, 0),
player,
nil)
nil
)
yl_snowball.snowgame.register_shot(player_name)
if not core.check_player_privs(player, "creative") then
itemstack:take_item(1)
@ -169,7 +176,7 @@ local SNOWBALL_DEF = {
end,
on_hit_node = function(self, node_pos, node, above_pos, intersection_point, intersection_normal, box_id)
print(dump(node_pos), dump(node))
--print(dump(node_pos), dump(node))
ballistics.on_hit_node_hit_sound_play(self, node_pos, node, above_pos, intersection_point, intersection_normal, box_id)
self.object:remove()
spawn_snow_puff(intersection_point, intersection_normal)
@ -178,17 +185,23 @@ local SNOWBALL_DEF = {
on_hit_object = function(self, target, intersection_point, intersection_normal, box_id)
core.chat_send_all(('%s hit %s'):format(os.clock(), dump(target:get_entity_name())))
print(dump(target))
-- print(dump(self))
spawn_snow_puff(intersection_point, intersection_normal)
self.object:remove()
if target:is_player() then
local target_name = target:get_player_name()
yl_snowball.snowgame.register_hit(self._source_player_name, target_name)
end
return true
end
}
--core.register_entity("yl_snowball:snowball_ent", SNOWBALL_DEF)
ballistics.register_projectile("yl_snowball:snowball_ent", SNOWBALL_DEF)
core.register_craft({
type = "shapeless",
output = 'yl_snowball:snowball 3',