#!/usr/bin/perl use strict; use warnings; use Combat; use Peasant; my $henry = Peasant->new(name => "Henry"); my $joe = Peasant->new(name => "Joe"); my $combat = Combat->new(name => "combat object"); $combat->initiative($joe, $henry); $combat->combat($joe, $henry); ------------------------------------------------------------ package Combat; use strict; use warnings; sub new { my ($class, %arg) = @_; my $objref = { _turns => $arg{name} || 0, _name => $arg{name} || "unknown" }; bless $objref, $class; return $objref; } sub combat { my ($combatref, $self, $self2) = @_; $self->attack($self2); $self2->attack($self); } sub initiative { my ($combatref) = shift @_; my @combatants = @_; foreach(@combatants) { #check initiative code here } } 1; ------------------------------------------------------------ package Peasant; use strict; use warnings; sub new { my ($class, %arg) = @_; my $objref = { _name => $arg{name} || "unknown", _dex => $arg{dex} || int(rand(10)) + 2, _hp => $arg{hp} || int(rand(10)) + 2, _gold => $arg{gold} || int(rand(30)) + 2, _title => $arg{title} || "farmboy" }; bless $objref, $class; return $objref; } sub attack { my ($self,$target) = @_; my $amount = int(rand(10)) + 2; print "$self->{_name} attacks $target->{_name} for $amount!\n"; $target->damage( $amount, $self ); } sub acquiregold { my ($self, $amount) = @_; $self->{_gold} += $amount; } sub damage { my ($self, $amount, $attacker) = @_; $self->{_hp} -= $amount; if ( $self->{_hp} <= 0 ) { print "$attacker->{_name} has killed $self->{_name}!!!\n"; print "$attacker->{_name} gained $self->{_gold} gold!!!\n"; $attacker->acquiregold($self->{_gold}); } } sub hitpoints { my ($self, $hits) = @_; print "$self->{_name} HP $self->{_hp}\n"; } 1;