Skip to content

Instantly share code, notes, and snippets.

View shawn42's full-sized avatar

Shawn Anderson shawn42

  • Cisco
  • Holland, MI
View GitHub Profile
@shawn42
shawn42 / dep_inj.py
Last active July 28, 2023 22:27
Simple DI in Python
from functools import wraps
from typing import Any, Callable, TypeVar, Type, cast, get_type_hints
T = TypeVar('T')
class AppContext:
def __init__(self) -> None:
self.dependencies: dict[type, dict[Any, Any]] = {}
self.cache: dict[type, Any] = {}
require 'rainbow'
RULES = {
coast: [:land, :coast, :sea],
sea: [:sea, :coast],
land: [:land, :coast],
}
DISPLAY = {
land: Rainbow("-").bg(:orange),
# Ideal choice of fixed-point equivalent to 1.0 that can almost perfectly represent sqrt(2) and (sqrt(2) - 1) in whole numbers
# 1.000000000 = 2378
# 0.414213624 = 985 / 2378
# 1.414213625 = 3363 / 2378
# 1.414213562 = Actual sqrt(2)
# 0.00000006252 = Difference between actual sqrt(2) and fixed-point sqrt(2)
COST_STRAIGHT = 2378
COST_DIAG = 3363
class Node
$: << '../lib'
require_relative '../lib/game_ecs'
require 'pry'
require 'gosu'
include Gosu
Q = GameEcs::Query
# Components
class GenerateNewCoinEvent; end
module GameEcs
class EcsSystem
def self.system_for_query(query, &block)
new(query, &block)
end
def initialize(query=Query.none, &block)
@query = query
@system_block = block
end
@shawn42
shawn42 / demo_stage.rb
Created January 9, 2015 20:07
Shadow effect in Gamebox
define_behavior :faded do
requires :timer_manager
setup do
actor.has_attributes shadows: [], shadows_to_render: opts[:shadows_to_render]
timer_manager.add_timer timer_name, 100 do
new_shadow = {x: actor.x, y: actor.y}
actor.shadows << new_shadow
actor.shadows.shift if actor.shadows.size > actor.shadows_to_render
end
@shawn42
shawn42 / demo_stage.rb
Created January 6, 2015 02:14
tween usage in gamebox
define_stage :demo do
requires :tween_manager
curtain_up do
@player = spawn :player, x: 10, y:30
tween = tween_manager.tween_properties @player, {x: 600, y:750}, 6_000, Tween::Sine::InOut
# tweens can be canceled if need be, or they will clean themselves up when finished
timer_manager.add_timer :foo, 3_000, false do
tween.cancel!
end
@shawn42
shawn42 / gamebox_fade.rb
Created January 3, 2015 23:05
fading text in gamebox
define_behavior :fades do
requires :director
setup do # |opts|
fade_out_time = 3_000
actor.has_attributes total_fade_time: fade_out_time, fade_time: fade_out_time
director.when :update do |time_ms, secs|
actor.fade_time -= time_ms
alpha = 255 * actor.fade_time / actor.total_fade_time
actor.remove if actor.fade_time <= 0
@shawn42
shawn42 / gist:4444621
Created January 3, 2013 16:17
JS attribute calling a function.
Foo = {}
Object.defineProperty(Foo, 'blah', {get: function(){ return 'yo'}});
Foo.blah
=> 'yo'
@shawn42
shawn42 / remote_foxy.rb
Created October 9, 2012 18:31
REPL in Foxy game
Pry-remote:
[7] pry(#<GameboxApp>)> f = actors(:foxy).first;
[31:0:736875] [debug] found 2 actors
[8] pry(#<GameboxApp>)> f.x += 10
=> 120.0
[9] pry(#<GameboxApp>)> watch :time do Time.now end