Framework for 2D management of game elements

TODO: collision framework (with quad tree?)

Introduced classes

class LiveGroup[E: LiveObject]

scene2d :: LiveGroup

Organizational class to manage groups of sprites and other live objects.
abstract class LiveObject

scene2d :: LiveObject

The root class of the living objects (sprites, group of sprites, etc.)
class Scene

scene2d :: Scene

A state in the game logic
class Sprite

scene2d :: Sprite

The basic atomic living and moving object.
interface View

scene2d :: View

Abstract view do draw sprites

All class definitions

class LiveGroup[E: LiveObject]

scene2d $ LiveGroup

Organizational class to manage groups of sprites and other live objects.
abstract class LiveObject

scene2d $ LiveObject

The root class of the living objects (sprites, group of sprites, etc.)
class Scene

scene2d $ Scene

A state in the game logic
class Sprite

scene2d $ Sprite

The basic atomic living and moving object.
interface View

scene2d $ View

Abstract view do draw sprites
package_diagram scene2d::scene2d scene2d core core scene2d::scene2d->core a_star-m a_star-m a_star-m->scene2d::scene2d

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.

Children

module a_star-m

a_star-m

# Framework for 2D management of game elements
#
# TODO: collision framework (with quad tree?)
module scene2d

# The root class of the living objects (sprites, group of sprites, etc.)
abstract class LiveObject
	# Compute the position, state and appearance.
	fun update do end

	# Controls whether `update' and `draw' are automatically called by `LiveGroup'
	var exists = true is writable

	# Redefine this method to asks how to draw on a view
	fun draw(view: View) is abstract
end


# The basic atomic living and moving object.
#
# A sprite has a position and a velocity
class Sprite
	super LiveObject

	# x coordinate of the center point
	var x: Int = 0 is writable

	# y coordinate of the center point
	var y: Int = 0 is writable

	# width of the sprite
	var width: Int = 100 is writable

	# height of the sprite
	var height: Int = 100 is writable

	# X coordinate of left side.
	fun left: Int do return x - width/2

	# X coordinate of right side.
	fun right: Int do return x + width/2

	# Y coordinate of top.
	fun top: Int do return y - height/2

	# Y coordinate of bottom.
	fun bottom: Int do return y + height/2

	# x velocity (applied by `update')
	var vx: Int = 0 is writable

	# y velocity (applied by `update')
	var vy: Int = 0 is writable

	redef fun update
	do
		self.x += self.vx
		self.y += self.vy
	end

	redef fun draw(view) do view.draw_sprite(self)

	# Is self overlaps (or contains) an other sprite
	# `x', `y', `width', and `height' of both sprites are considered
	fun overlaps(other: Sprite): Bool
	do
		return self.right > other.left and self.left < other.right and self.bottom > other.top and self.top < other.bottom
	end

	# Return the current angle of velocity
	# Often used to rotate the displayed image with the correct angle
	fun velocity_angle: Float
	do
		return atan2(self.vx.to_f, -self.vy.to_f)
	end

	# Return the angle to target an other sprite
	fun angle_to(target: Sprite): Float
	do
		return atan2((target.x-self.x).to_f, (self.y-target.y).to_f)
	end

	# Update of vx and vy toward a given angle and magnitude
	fun set_velocity(angle: Float, maginude: Int)
	do
		var magf = maginude.to_f
		self.vx = (angle.sin * magf).to_i
		self.vy = (angle.cos * -magf).to_i
	end

end

# Organizational class to manage groups of sprites and other live objects.
class LiveGroup[E: LiveObject]
	super LiveObject
	super List[E]

	# Recursively update each live objects that `exists'
	redef fun update
	do
		for x in self do if x.exists then x.update
	end

	# Remove all live Objects that do not exists
	# Call this to cleanup the live group
	fun gc
	do
		var i = self.iterator
		while i.is_ok do
			var e = i.item
			if not e.exists then
				i.delete
			else if e isa LiveGroup[LiveObject] then
				e.gc
			end
			i.next
		end
	end

	# Recursively draw each live objects that `exists'
	redef fun draw(view)
	do
		for x in self do if x.exists then x.draw(view)
	end
end

# A state in the game logic
# A scene manage a bunch of live objects
class Scene
	super LiveObject
end

# Abstract view do draw sprites
#
# Concrete views are specific for each back-end.
# View can also be used to implements camera and other fun things.
interface View
	# Draw a specific sprite on the view
	#
	# This method must be implemented for each specific view.
	# A traditional way of implementation is to use a double-dispatch mechanism
	#
	#     class MyView
	#         super View
	#         redef fun draw_sprite(s) do s.draw_on_myview(self)
	#     end
	#     redef class Sprite
	#         # How to draw a sprite on my specific view
	#         fun draw_on_myview(myview: MyView) is abstract
	#     end
	fun draw_sprite(s: Sprite) is abstract
end
lib/scene2d/scene2d.nit:15,1--166,3