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 / aabb_node.rb
Created January 30, 2012 18:13
2D Axis Aligned Bounding Box Tree Sample
# This is the primary element in an AABBTree.
# It acts as both containing elements and leaf elements. Leaves have an @object,
# containers have @a and @b nodes. All leaf nodes have a cached list of
# collisions that contain references to other nodes in the tree.
class AABBNode
include AABBNodeDebugHelpers
attr_accessor :bb, :a, :b, :parent, :object, :cached_collisions
def initialize(parent, object, bb)
@parent = parent
@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
@shawn42
shawn42 / group_by.rb
Created September 4, 2012 11:17
Piece Piece map, aggregate, group-by
# Using group-by
# no extra step definition is required
PiecePipe::Pipeline.new.
source([{region: region}]).
step(FetchPowerPlantsByRegion).
group_by(:region).
to_enum
@shawn42
shawn42 / behavior_dsl.rb
Created May 12, 2012 16:17
proposed behavior dsl
# CURRENT
has_behavior animated: {:frame_update_time => 900}
has_behavior collidable: {shape: :polygon, cw_local_points: [[4,4],[44,4],[44,44],[4,44]]}
has_behavior projectile: {speed: 0.01, direction: vec2(1,0)}
has_behavior :reversable_direction
has_behavior increasing_speed: { accel: 0.001 }
has_behavior drops: { fall_amount: 25}
has_behavior shooter: {shoots: :alien_missile, direction: vec2(0,-1)}