nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
src/doc: introduce option --no-render in HTML phase.
[nit.git]
/
examples
/
mnit_dino
/
src
/
game_logic.nit
diff --git
a/examples/mnit_dino/src/game_logic.nit
b/examples/mnit_dino/src/game_logic.nit
index
fb5418a
..
6fa0d3f
100644
(file)
--- a/
examples/mnit_dino/src/game_logic.nit
+++ b/
examples/mnit_dino/src/game_logic.nit
@@
-15,7
+15,7
@@
# limitations under the License.
# Entire game logic for the Dino game
# limitations under the License.
# Entire game logic for the Dino game
-# Depends only on Nit standard library
+# Depends only on Nit `core` library
module game_logic
interface Turnable
module game_logic
interface Turnable
@@
-28,20
+28,26
@@
class Game
var dino = new Dino
var cavemen = new Array[Caveman]
var javelins_in_air = new Array[Javelin]
var dino = new Dino
var cavemen = new Array[Caveman]
var javelins_in_air = new Array[Javelin]
- var javelins_on_ground = new Array[Javelin]
+ var items_on_ground = new Array[Entity]
var entities = new Array[Entity].with_items(dino)
var turn_nbr = 0
var over_since = 0
var entities = new Array[Entity].with_items(dino)
var turn_nbr = 0
var over_since = 0
+ var score: Ref[Int]
+
var random_radius_min = 200
var random_radius_max = 400
protected var random_radius_diff : Int =
random_radius_max - random_radius_min
var random_radius_min = 200
var random_radius_max = 400
protected var random_radius_diff : Int =
random_radius_max - random_radius_min
- init( cavemen_nbr : Int )
+ var entities_sorter = new EntitiesSorter
+
+ init( cavemen_nbr : Int, score: Ref[Int] )
do
do
- srand
+ srand_from(cavemen_nbr)
+
+ self.score = score
nbr_wanted_cavemen = cavemen_nbr
nbr_wanted_cavemen = cavemen_nbr
@@
-55,6
+61,13
@@
class Game
man.pos.x = ( angle.cos * radius ).to_i
man.pos.y = ( angle.sin * radius ).to_i
end
man.pos.x = ( angle.cos * radius ).to_i
man.pos.y = ( angle.sin * radius ).to_i
end
+
+ for b in 24.times do
+ var bush = new Bush
+ bush.pos = new GamePos([-300..300[.rand, [-400..400[.rand)
+ items_on_ground.add bush
+ entities.add bush
+ end
end
fun do_turn : Turn
end
fun do_turn : Turn
@@
-67,6
+80,7
@@
class Game
man.do_turn( turn )
if not man.is_alive then
cavemen.remove( man )
man.do_turn( turn )
if not man.is_alive then
cavemen.remove( man )
+ score.item += 1
end
end
end
end
@@
-77,7
+91,7
@@
class Game
entities.remove( j )
else if j.hit_ground then
javelins_in_air.remove( j )
entities.remove( j )
else if j.hit_ground then
javelins_in_air.remove( j )
- javelins_on_ground.add( j )
+ items_on_ground.add( j )
end
end
end
end
@@
-86,7
+100,7
@@
class Game
end
# sort for blitting, firsts and in the back
end
# sort for blitting, firsts and in the back
- entities.sort !cmp( a, b ) = b.pos.y <=> a.pos.y
+ entities_sorter.sort entities
return turn
end
return turn
end
@@
-145,18
+159,30
@@
end
class Entity
super Turnable
class Entity
super Turnable
+ fun run_over_distance_x: Int do return 50
+ fun run_over_distance_y: Int do return 16
+
var pos = new GamePos( 0, 0 )
fun squared_dist_with_dino( game : Game ) : Int
do
return pos.squared_dist_with( game.dino.pos )
end
var pos = new GamePos( 0, 0 )
fun squared_dist_with_dino( game : Game ) : Int
do
return pos.squared_dist_with( game.dino.pos )
end
+
+ fun under_dino(game: Game): Bool
+ do
+ var dy = pos.y - game.dino.pos.y
+ if dy.abs > run_over_distance_y then return false
+
+ var dx = pos.x - game.dino.pos.x
+ return dx.abs <= run_over_distance_x
+ end
end
class MovingEntity
super Entity
end
class MovingEntity
super Entity
- var going_to : nullable GamePos writable = null
+ var going_to : nullable GamePos = null is writable
fun speed : Int is abstract
fun speed : Int is abstract
@@
-216,6
+242,13
@@
class Dino
if is_alive then
super
end
if is_alive then
super
end
+
+ for i in t.game.items_on_ground do
+ if i.under_dino(t.game) then
+ t.game.items_on_ground.remove i
+ t.game.entities.remove i
+ end
+ end
end
end
end
end
@@
-229,31
+262,35
@@
class Caveman
var throw_distance : Int = 400*40+10.rand
var fear_distance : Int = 300*20+8.rand
var flee_distance : Int = 600*60+16.rand
var throw_distance : Int = 400*40+10.rand
var fear_distance : Int = 300*20+8.rand
var flee_distance : Int = 600*60+16.rand
- var run_over_distance = 500
var fear_duration : Int = 80+40.rand
var throw_period : Int = 40+8.rand
var fear_duration : Int = 80+40.rand
var throw_period : Int = 40+8.rand
+ var variance_angle: Float = 2.0*pi.rand
+ var variance_dist: Int = throw_distance.to_f.sqrt.to_i-4
+ var variance_x: Int = (variance_angle.cos*variance_dist.to_f).to_i
+ var variance_y: Int = (variance_angle.sin*variance_dist.to_f).to_i
+
redef var is_alive : Bool = true
redef var is_alive : Bool = true
- redef fun speed do return 4
+ redef var speed: Int = 3+3.rand
fun is_afraid( turn : Turn ) : Bool do return turn.nbr < afraid_until
fun can_throw( turn : Turn ) : Bool do return cannot_throw_until < turn.nbr
fun is_afraid( turn : Turn ) : Bool do return turn.nbr < afraid_until
fun can_throw( turn : Turn ) : Bool do return cannot_throw_until < turn.nbr
+ fun die(turn: Turn) do is_alive = false
redef fun do_turn( t )
do
if is_alive then
redef fun do_turn( t )
do
if is_alive then
- var dwd = squared_dist_with_dino( t.game )
-
- if dwd < run_over_distance then
- is_alive = false
+ if under_dino(t.game) then
+ if t.game.dino.is_alive then die(t)
return
else if is_afraid( t ) then
# going to destination
else if t.game.dino.life <= 0 then
# dino is dead, chill
else
return
else if is_afraid( t ) then
# going to destination
else if t.game.dino.life <= 0 then
# dino is dead, chill
else
+ var dwd = squared_dist_with_dino( t.game )
if dwd < fear_distance then
afraid_until = t.nbr + fear_duration
if dwd < fear_distance then
afraid_until = t.nbr + fear_duration
@@
-262,6
+299,7
@@
class Caveman
var dy = dino_pos.y - pos.y
var a = atan2( dy.to_f, dx.to_f )
a += pi # get opposite
var dy = dino_pos.y - pos.y
var a = atan2( dy.to_f, dx.to_f )
a += pi # get opposite
+ a += [-100..100[.rand.to_f*pi/3.0/100.0
var x = a.cos*flee_distance.to_f
var y = a.sin*flee_distance.to_f
going_to = new GamePos( x.to_i, y.to_i )
var x = a.cos*flee_distance.to_f
var y = a.sin*flee_distance.to_f
going_to = new GamePos( x.to_i, y.to_i )
@@
-274,7
+312,8
@@
class Caveman
end
else
# get closer to dino
end
else
# get closer to dino
- going_to = t.game.dino.pos
+ var dino_pos = t.game.dino.pos
+ going_to = new GamePos(dino_pos.x+variance_x, dino_pos.y+variance_y)
end
end
end
end
@@
-318,6
+357,12
@@
class Javelin
else
if z <= 0 then
hit_ground = true
else
if z <= 0 then
hit_ground = true
+ if thrown_angle_xy.cos > 0.0 then
+ angle = pi*5.0/8.0+(pi/4.0).rand
+ else
+ # left of the screen
+ angle = pi*9.0/8.0+(pi/4.0).rand
+ end
else
# in the air
speed_z += gravity
else
# in the air
speed_z += gravity
@@
-336,3
+381,13
@@
class Javelin
end
end
end
end
end
end
+
+class Bush super Entity end
+
+# Sort entities on screen in order of Y, entities in the back are drawn first
+class EntitiesSorter
+ super Comparator
+ redef type COMPARED: Entity
+
+ redef fun compare(a, b) do return b.pos.y <=> a.pos.y
+end